Socket programming in Linux with GCC

Linux is the paradise of programmers. It facilitates programmers with the numerous tools and flexibility.

Programmers easily can develop and test their code in Linux freely. Linux has many compilers and programming languages which majority of them are freely available with good documentation, by contrast of Windows.

This post demonstrates of how to write simple chat application with C programming language.

Fortunately, Linux contains necessary header files and needed libraries for communication and make connection. The following header files in Linux socket programming are needed for both client and server applications.

#include <sys/types.h>

#include <netinet/in.h>

#include <string.h>

#include <sys/socket.h>

#include <stdlib.h>

#include <unistd.h>

The chat application should be written as two separate applications. The first is client application and the other is server application and each can run in separate computer.

Bear in mind that you need to compile and run the application in Linux terminal or console. If you need more information about how to compile C application with GCC compiler in Linux click here.

The following section reflects the server side application,

/*Server Side*/

#include <stdio.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <string.h>

#include <sys/socket.h>

#include <stdlib.h>

#include <unistd.h>

main()

{

int sd,i,len,bi,nsd,port;

char content[255];

struct sockaddr_in ser,cli;

if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)

{

printf("\nSocket problem");

return 0;

}

printf("\nSocket created\n");

bzero((char*)&cli,sizeof(ser));

printf("ENTER PORT NO:\n");

scanf("%d",&port);

printf("\nPort Address is %d\n:",port);

ser.sin_family=AF_INET;

ser.sin_port=htons(port);

ser.sin_addr.s_addr=htonl(INADDR_ANY);

bi=bind(sd,(struct sockaddr *)&ser,sizeof(ser));

if(bi==-1)

{

printf("\nBind error, Port busy, Plz change port in client and server");

return 0;

}

i=sizeof(cli);

listen(sd,5);

nsd = accept(sd,((struct sockaddr *)&cli),&i);

if(nsd==-1)

{

printf("\nCheck the description parameter\n");

return 0;

}

printf("\nConnection accepted!\n");

if(fork())

{

printf("Enter the data to be send type exit for stop:\n\e[1;33m");

gets(content);

while(strcmp(content,"exit")!=0)

{

gets(content);

send(nsd,content,255,0);

}

send(nsd,"exit",5,0);

}

else

{

i = recv(nsd,content,255,0);

while(strcmp(content,"exit")!=0)

{

printf("\e[1;34mClient: %s\e[1;33m\n",content);

i=recv(nsd,content,255,0);

}

}

printf("\nBye");

send(nsd,"Offline",10,0);

close(sd);

close(nsd);

printf("\e[0m");

return 0;

}

In server side, at first server application tries to make a TCP connection. If the application faces problem during creating TCP connection it will shows message and exit. Then in the next step if will ask user to set IP address and port. When user entered set the parameters it will check the availability of the port and IP address. If either is in use then server applications prompts “Busy” message. Otherwise, it will show success message and listen to the port for client to connect to server.

After client application connected to the server successfully, both applications can start communication easily. Finally, the connection will be closed if the server user types “exit” in message part.

The code of client application is like below,

/*Client Side*/

#include <stdio.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <string.h>

#include <sys/socket.h>

#include <stdlib.h>

#include <unistd.h>

int main()

{

int sd,con,port,i,Res;

char content[255];

struct sockaddr_in cli;

if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)

{

printf("\nSocket problem");

return 0;

}

bzero((char*)&cli,sizeof(cli));

cli.sin_family = AF_INET;

printf("ENTER PORT NO:\n");

scanf("%d",&port);

cli.sin_port=htons(port);

cli.sin_addr.s_addr=htonl(INADDR_ANY);

con=connect(sd,(struct sockaddr*)&cli,sizeof(cli));

if(con==-1)

{

printf("\nConnection error");

return 0;

}

if(fork())

{

printf("\nEnter the data to be send type exit for stop:\n\e[1;33m");

//printf("\e[1;33mHHHHH");

gets(content);

while(strcmp(content,"exit")!=0)

{

gets(content);

send(sd,content,255,0);

}

send(sd,"exit",5,0);

}

else

{

i=recv(sd,content,255,0);

while(strcmp(content,"exit")!=0)

{

printf("\e[1;34mServer: %s\e[1;33m\n",content);

i=recv(sd,content,255,0);

}

send(sd,"exit",5,0);

}

close(sd);

printf("\e[0m");

return 0;

}

The client application is different from server application. In the client no TCP connection is created and just IP address and port which server listens to them need to be set in the client. If any problem will be happened, the application terminates. Otherwise, the client connects to server and the communication will be started. Like server program, if client user types exit in message part, the connection will be terminated.

Finally, for downloading the source code of chat application click here.

send your idea and information to kasra.madadipouya@geeksweb.tk

Leave a Reply