Welcome to our deep dive into the C Connecting function! This lesson is designed to guide both beginners and intermediates in understanding the connect() function, a crucial tool in network programming in C. Let's get started!
In network programming, establishing a connection between a client and a server is essential. The connect() function helps us achieve this.
The connect() function is used to establish a connection with a remote server using a specific address and port. Here's a simple example:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
int main(int argc, char *argv[]) {
int sockfd, portno, n;
struct sockaddr_in server_addr;
char buffer[256];
if (argc < 3) {
printf("Usage: %s <IP> <Port>\n", argv[0]);
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
inet_pton(AF_INET, argv[1], &server_addr.sin_addr);
if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("ERROR connecting to server");
exit(1);
}
// Your code here to interact with the server
close(sockfd);
return 0;
}This program takes an IP address and a port number as command-line arguments and connects to the specified server.
š Note: The AF_INET macro represents the Address Family for Internet Protocol version 4. SOCK_STREAM specifies a sequenced, reliable, two-way connection between sockets.
The connect() function connects a socket to a specified address and port. Here's what happens:
socket() function.sockaddr_in structure is filled with the IP address and port number.inet_pton().connect() function is called, which attempts to connect the socket to the specified address and port.Let's create a simple chat server and client using connect().
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char* message = "Welcome to the chat server!";
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("ERROR opening socket");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("ERROR setting options");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8888);
if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
perror("ERROR on binding");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("ERROR on listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr*)NULL, (socklen_t*)NULL)) < 0) {
perror("ERROR on accept");
exit(EXIT_FAILURE);
}
send(new_socket, message, strlen(message), 0);
// Your code here to receive and send messages
close(new_socket);
close(server_fd);
return 0;
}#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[]) {
int sockfd, portno;
struct sockaddr_in server_addr;
char buffer[256];
if (argc < 3) {
printf("Usage: %s <IP> <Port>\n", argv[0]);
exit(1);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(portno);
inet_pton(AF_INET, argv[1], &server_addr.sin_addr);
if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("ERROR connecting to server");
exit(1);
}
// Your code here to send and receive messages
close(sockfd);
return 0;
}š” Pro Tip: Use the connect() function to establish a connection with a server in your network programs. This will help you create applications like chat clients and servers, FTP clients, and more.
Which function is used to establish a connection between a client and a server in C?