I have created a common C file for a basic client server. It works fine on my Linux setup. Please provide your comments/suggestions on how the code can be optimized/enhanced.
After compiling we need to give parameters "s" or "c" to executable from the command line to run it as a server or client respectively. The client connects to the server at the default IP of 192.168.7.2.
#include"stdio.h"
#include"stdlib.h"
#include"sys/types.h"
#include"sys/socket.h"
#include"string.h"
#include"netinet/in.h"
#include"pthread.h"
#include"netdb.h"
#include"errno.h"
#include"signal.h"
#define PORT 4444
#define BUF_SIZE 2000
#define CLADDR_LEN 100
#define MAX_ALLOWED_CONNECTIONS 5
char * whoAmI;
int sockfd, len, ret, newsockfd;
void * receiveMessage(void * socket) {
int sockfd, ret;
char buffer[BUF_SIZE];
sockfd = (int) socket;
memset(buffer, 0, BUF_SIZE);
for (;;) {
memset(buffer, 0, BUF_SIZE);
ret = recvfrom(sockfd, buffer, BUF_SIZE, 0, NULL, NULL);
if (ret < 0) {
printf("Error receiving data!\n");
} else {
if(*whoAmI == "c") {
printf("server: ");
}
else {
printf("client: ");
}
fputs(buffer, stdout);
//printf("\n");
}
}
}
void INThandler(int sig)
{
printf("Closig all sockets");
close(newsockfd);
close(sockfd);
exit(0);
}
void main(int argc, char**argv) {
struct sockaddr_in addr, cl_addr;
char buffer[BUF_SIZE];
pid_t childpid;
char clientAddr[CLADDR_LEN];
pthread_t rThread;
/* install signal handler to close sockets on ctrl+c from user */
signal(SIGINT, INThandler);
if (argc < 2) {
printf("Needs arguments: 0==Server; 1==client\n");
exit(1);
}
whoAmI = argv[1];
printf("whoami = %s \n", whoAmI);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("Error creating socket!\n");
exit(1);
}
printf("Socket created...\n");
memset(&addr, 0, sizeof(addr));
// start of server
if ((strcmp(whoAmI, "s") == 0) ) {
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = PORT;
/* Naming the socket */
//strcpy(addr.sun_path,"server_socket");
ret = bind(sockfd, (struct sockaddr *) &addr, sizeof(addr));
if (ret < 0) {
printf("%s",strerror(errno));
printf("Error binding!\n");
exit(1);
}
printf("Binding done...\n");
printf("Waiting for a connection...\n");
listen(sockfd, MAX_ALLOWED_CONNECTIONS);
len = sizeof(cl_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cl_addr, &len);
if (newsockfd < 0) {
printf("Error accepting connection!\n");
exit(1);
}
inet_ntop(AF_INET, &(cl_addr.sin_addr), clientAddr, CLADDR_LEN);
printf("Connection accepted from %s...\n", clientAddr);
}//end of server
else {
//start of client
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("192.168.7.2");
addr.sin_port = PORT;
ret = connect(sockfd, (struct sockaddr *) &addr, sizeof(addr));
if (ret < 0) {
printf("Error connecting to the server!\n");
exit(1);
}
printf("Connected to the server...\n");
//very important step
newsockfd = sockfd;
// end of client
}
memset(buffer, 0, BUF_SIZE);
printf("Enter your messages one by one and press return key!\n");
//creating a new thread for receiving messages from the client
ret = pthread_create(&rThread, NULL, receiveMessage, (void *) newsockfd);
if (ret) {
printf("ERROR: Return Code from pthread_create() is %d\n", ret);
exit(1);
}
while (fgets(buffer, BUF_SIZE, stdin) != NULL) {
ret = sendto(newsockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &cl_addr, len);
if (ret < 0) {
printf("Error sending data!\n");
exit(1);
}
}
close(newsockfd);
close(sockfd);
pthread_exit(NULL);
return;
}