TCP Server-Client : Echo string from client

September 4th, 2008  |  Published in Linux Sample Programs  |  2 Comments








// Author : Amit Sahrawat

// Client Program
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>

#define BUFFSIZE 256
#define SERVER_PORT 6401

int main(int argc, char** argv)
{
int sock;
struct sockaddr_in echoserver;
char buffer[BUFFSIZE]={0};
unsigned int echolen;

int received =0;

if(argc<3)
{
printf(“Usage : cli <serverip> <String> \n”);
exit(1);
}
if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror(“Failed to create socket \n”);
}

memset(&echoserver, 0, sizeof(echoserver));
echoserver.sin_family = AF_INET;
echoserver.sin_addr.s_addr = inet_addr(argv[1]);
echoserver.sin_port = htons(SERVER_PORT);
if(connect(sock, (struct sockaddr *) &echoserver, sizeof(echoserver)) < 0) {
perror(“Failed to connect with the server \n”);
}

memcpy(buffer,argv[2],sizeof(buffer)-1);
echolen = strlen(buffer);
if(send(sock,buffer,echolen, 0) != echolen) {
perror(“Mistmatch in bytes sent \n”);
}
close(sock);
}

// Author : Amit Sahrawat

// Server Program
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define BUFFSIZE 256
#define MAX_PENDING 10
#define MAX_CLIENTS MAX_PENDING

pthread_t clients[MAX_CLIENTS];
#define SERVER_PORT 6401

void *ClientData(void* arg)
{
int sock = *(int*)arg;
char buffer[BUFFSIZE] = {0};
int received = -1;

fd_set fds;
int maxfd;
int nready;

maxfd = sock + 1;

while(1)
{
FD_ZERO(&fds);
FD_SET(sock,&fds);

nready = select(maxfd,&fds,0,0,0);
if(FD_ISSET(sock,&fds))
{
if(( received = recv(sock,buffer,BUFFSIZE-1, 0)) <0 ) {
perror(“Failed to receive additional bytes from client \n”);
}
if(received >0){
printf(“Received Buffer : %s \n”,buffer);
}
if(received < 1)
{
printf(“Closing this client\n”);
close(sock);
pthread_exit(NULL);
}
memset(buffer,”,sizeof(buffer));
}
}
close(sock);
}

int main()
{
int threadCount =0;

int sock,clientsock;
struct sockaddr_in echoserver,echoclient;
char buffer[BUFFSIZE] = {0};

if(( sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0 ){
perror(“Failed to created socket \n”);
}

memset(&echoserver, 0, sizeof(echoserver));
echoserver.sin_family = AF_INET;
echoserver.sin_addr.s_addr = htonl(INADDR_ANY);
echoserver.sin_port = htons(SERVER_PORT);

if(bind(sock,(struct sockaddr *) &echoserver, sizeof(echoserver)) < 0) {
perror(“Failed to bind the socket \n”);
}
if(listen(sock, MAX_PENDING) < 0) {
perror(“Failed to listen on server socket \n”);
}

while(1)
{
unsigned int clientlen = sizeof(echoclient);

if(( clientsock = accept(sock, (struct sockaddr*) &echoclient, &clientlen)) < 0) {
perror(“Failed to accept client connection \n”);
}
fprintf(stdout,”Client Connected : %s \n”,inet_ntoa(echoclient.sin_addr));

if(clientsock > -1)
{
if(threadCount<MAX_CLIENTS)
pthread_create(&clients[threadCount++],NULL,ClientData,(void *) &clientsock);
}
}
}


// Makefile for compilation of server/client

GCC=gcc
LIBS=-lpthread
CFLAGS=-g
all:server client
server:
$(GCC) server.c -o server $(CFLAGS) $(LIBS)
client:
$(GCC) client.c -o client $(CFLAGS) $(LIBS)

clean:
rm -f client server

Subscribe

Get articles in your inbox.

Enter your email address:

Join Us

Twitter Chatter


Recommendations

Archives

Categories