Networking in C Language
Introduction
Networking in C involves using sockets to create connections between two or more devices. This tutorial will guide you through the basics of networking in C, including setting up a server and a client to communicate with each other.
Understanding Sockets
A socket is an endpoint for communication between two machines. Sockets are created using the socket() function, which returns a socket descriptor. This socket descriptor is then used to connect, send, and receive data.
Example of creating a socket:
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Error opening socket");
exit(1);
}
Setting Up a Server
To set up a server, we need to create a socket, bind it to an address and port, and then listen for incoming connections. The server will use the accept() function to accept incoming client connections.
Example of setting up a server:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Error opening socket");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 5001;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("Error on binding");
exit(1);
}
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) {
perror("Error on accept");
exit(1);
}
bzero(buffer, 256);
n = read(newsockfd, buffer, 255);
if (n < 0) {
perror("Error reading from socket");
exit(1);
}
printf("Here is the message: %s\n", buffer);
n = write(newsockfd, "I got your message", 18);
if (n < 0) {
perror("Error writing to socket");
exit(1);
}
close(newsockfd);
close(sockfd);
return 0;
}
Setting Up a Client
To set up a client, we need to create a socket and connect it to the server's address and port. The client can then send and receive data using the socket.
Example of setting up a client:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
portno = 5001;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Error opening socket");
exit(1);
}
server = gethostbyname("localhost");
if (server == NULL) {
fprintf(stderr, "Error, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("Error connecting");
exit(1);
}
printf("Please enter the message: ");
bzero(buffer, 256);
fgets(buffer, 255, stdin);
n = write(sockfd, buffer, strlen(buffer));
if (n < 0) {
perror("Error writing to socket");
exit(1);
}
bzero(buffer, 256);
n = read(sockfd, buffer, 255);
if (n < 0) {
perror("Error reading from socket");
exit(1);
}
printf("%s\n", buffer);
close(sockfd);
return 0;
}
Compiling and Running
To compile and run the server and client programs, use the following commands:
gcc -o server server.c
gcc -o client client.c
./server
./client
