I am learning to work with C sockets, and have created very basic client and server programs for the same
Client Program
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> // contains the type definitions related to sockets
#include <sys/socket.h> // contains the socket API
#include <netinet/in.h> // used to store the address information
int main() {
int network_socket; // the socket is represented by an integer value, which can be used later to access the socket
network_socket = socket(AF_INET, SOCK_STREAM, 0); // opening a socket, defining the domain of the socket, type of socket, and protocol used
struct sockaddr_in server_address; // creating an address structure for the server
server_address.sin_family = AF_INET; // specifying the type of socket as internet
server_address.sin_port = htons(9002); // specifying the port number
server_address.sin_addr.s_addr = INADDR_ANY; // specifying the address
int connection_status = connect(network_socket, (struct sockaddr*) &server_address, sizeof(server_address)); // The function to establish a connection
if(connection_status == -1) {
printf("Connetion Failed");
}
char server_response[256];
recv(network_socket, &server_response, sizeof(server_response), 0);
printf("The server sent the data: %s", server_response);
return 0;
}
Server Program
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
char server_message[256] = "You have reached the server\n";
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address));
listen(server_socket, 5); // 5 indicates the backlog, i.e. how many sockets can be waiting for this connection at a given point of time
int client_socket;
client_socket = accept(server_socket, NULL, NULL);
send(client_socket, server_message, sizeof(server_message), 0);
return 0;
}
The given programs work well for the first time that I compile and run both of them, and I get the desired response upon running the client program
The server sent the data: You have reached the server
But when I try running the client program once again, I get the following message
Connection FailedThe server sent the data:
Even when I terminate the running server program, re-compile it and run it again, the same thing happens
&server_response
is a pointer to the array itself, and will have the typechar (*)[256]
. Usually you pass a pointer to the first element of the array, which is plainserver_response
.send
call might need multiplerecv
calls to be able to receive all the data.TIME_WAIT
state. That will last for a couple of minutes. During that time you can't create a socket bound to the same interface and port again.TIME_WAIT
only if the server closes its end of the connection first. As neither party is explicitly closing their sockets, whether the server entersTIME_WAIT
is a timing issue dependent on which process actually terminates first.