Sunday, 21 December 2014

CLIENT SERVER COMMUNICATION USING TCP SOCKET

Client server system that uses the socket interface to send messages over TCP connection


ALGORITHM:

SERVER

· Start the program.

· Inside the main function, create an instance of server.

· Create a socket using socket function. Pass the domain AF_INET to indicate IPV4 protocol and the type as SOCK_STREAM to indicate stream socket. On success, the socket function returns a small non-negative int value called socket descriptor that is stored on integer variable.

· Set the values of address structure as zero using bzero(). Initialize family as AF_INET, the IP address using htonl() function and port using htons(). Use bind() function to bind the socket to server address.

· Use the listen() function to define how many connections can be pending on socket.

· Use the accept() function which is called by a TCP server to return the next completed connection from the front of completed connection queue. The client address and address returns the protocol address of the connected peer processes.

· Issue a write protocol to send the message to the client.

CLIENT

· Check if the number of arguments is equal to 3.

· Create a socket using socket function with family as AF_INET and type as SOCK_STREAM.

· Set the values of address structure as zero using bzero().

· Initialize family as AF_INET and the port number that is passed as an argument.

· Use the connect() function to set up connection with server. The address parameter refers to the remote address.

· Obtain the message entered by the user with the help of scanf() function. Write this using write() function.

· Read() function is then used to read the message from the server and print it.

· Stop the program.



PROGRAM:

SERVER

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<arpa/inet.h>

#include<sys/types.h>

int main()

{

int socketmain,socketclient,child,port=5050,len=81;

struct sockaddr_in serv,clientaddr;

socklen_t clientlen;

if((socketmain=socket(AF_INET, SOCK_STREAM,0))<0)

{

printf("Server cannot open socket \n");

exit(0);

}

bzero(&serv,sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_addr.s_addr=htonl(INADDR_ANY);

serv.sin_port=htons(port);

if((bind(socketmain,(struct sockaddr*)&serv, sizeof(serv)))<0)

{

printf("Server bind failed \n");

exit(1);

}

listen(socketmain,5);

if((socketclient=accept(socketmain, (struct sockaddr *)&clientaddr, &clientlen))<0)

{

printf("Client is bad \n");

exit(0);

}

char buf[len];

bzero(buf,len);

int n=read(socketclient,buf,100);

printf("Message is %s \n",buf);

strcpy(buf,"bye");

write(socketclient,buf,100);

}















CLIENT

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<unistd.h>

#include<netdb.h>

#include<string.h>

#include<arpa/inet.h>

#include<sys/types.h>

int main(int argc,char **argv)

{

int sockfd,n,port=4500;

struct sockaddr_in serv;

char buf[100],msg[100];

if(argc!=3)

{

printf("Invalid format");

exit(0);

}

if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)

{

printf("\n Socket cannot be opened");

exit(0);

}

bzero(&serv, sizeof(serv));

serv.sin_family=AF_INET;

serv.sin_port=htons(atoi(argv[2]));

if(connect(sockfd,(struct sockaddr*)&serv,sizeof(serv))<0)

{

printf("\n Connection failed");

exit(0);

}

printf("Connected \n");

printf("Enter message to server");

scanf("%s",buf);

write(sockfd,buf,100);

n=read(sockfd,msg,100);

printf("Server message is %s \n",msg);

}






OUTPUT:

./server

Message is hi

bye

./client 127.0.0.1 5050

Connected

Enter message to server hi

Server message is bye

IMPLEMENTATION OF CYCLIC REDUNDANCY CHECK


C program to perform CYCLIC REDUNDANCY CHECK (CRC).


ALGORITHM:

1. 1.Get the input string and the key from the user.

2. Add 0’s to the end of the string based on the length of the key.

3. Perform Binary Division with the string and the key.

4. The reminder is the required CRC which replaces the 0’s in the input string.

5. Transmit the code to the receiver.

6. At the receiving end the key using binary division divides the received message.

7. If reminder is 0 then the received message is correct, otherwise, display the error message.

PROGRAM:

#include<stdio.h>

#include<math.h>

#include<string.h>

char str[20],key[20],str1[20];

void func()

{ int i,j;

char rem[20];

for(i=0;i<strlen(key);i++)

rem[i]=str[i];

while(i<strlen(str))

{ if(rem[0]==key[0])

{ rem[0]='0';

for(j=1;j<strlen(key);j++)

{ if(rem[j]!=key[j])

rem[j]='1';

else

rem[j]='0';

}

}

for(j=0;j<strlen(key)-1;j++)

rem[j]=rem[j+1];

rem[j]=str[i];

rem[j+1]='\0';

i++;

}

if(rem[0]==key[0])

{ rem[0]='0';

for(j=1;j<strlen(key);j++)

{ if(rem[j]!=key[j])

rem[j]='1';

else

rem[j]='0';

}

}

for(j=0;j<strlen(key);j++)

rem[j]=rem[j+1];

rem[j]='\0';

strcat(str1,rem);

printf("\n Decoded string is: %s",str1);

}

main()

{ int i;

printf("\n Enter the coded string:");

scanf("%s",str);

printf("\n Enter the key");

scanf("%s",key);

strcpy(str1,str);

for(i=1;i<strlen(key);i++)

strcat(str,"0");

if(strlen(str)>=strlen(key))

func();

else

printf("\n Invalid key");

}




OUTPUT:

Enter coded string : 11001101

Enter the key : 1101

Decoded string is : 11001101001