0% found this document useful (0 votes)
7 views4 pages

CNPRGM

The document contains code for a UDP server and client that checks if a number is a palindrome, as well as a TCP server and client for a chat application. The UDP server receives a number from the client, checks if it is a palindrome, and sends back the result. The TCP chat application allows clients to send messages to the server until they type 'exit' to terminate the connection.

Uploaded by

harikrishnaarun5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

CNPRGM

The document contains code for a UDP server and client that checks if a number is a palindrome, as well as a TCP server and client for a chat application. The UDP server receives a number from the client, checks if it is a palindrome, and sends back the result. The TCP chat application allows clients to send messages to the server until they type 'exit' to terminate the connection.

Uploaded by

harikrishnaarun5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Udp

Server
// UDP Server(palindrome)

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>

main() {

struct sockaddr_in client,server; int lfd,n;

char rBuf[100]="",sBuf[100]="";

lfd=socket(AF_INET,SOCK_DGRAM,0);

server.sin_family=AF_INET;

server.sin_port=2001;

server.sin_addr.s_addr=inet_addr("127.0.0.1");

bind(lfd,(struct sockaddr *)&server,sizeof server);

printf("\nServer ready,waiting for client....\n");

n=sizeof client;

recvfrom(lfd,rBuf,sizeof rBuf,0,(struct sockaddr *)&client,&n);

printf("\nClient:%s",rBuf);

int num=atoi(rBuf);
int rev=0,rem,copy=num;

while(num!=0){
rem=num%10;
rev=rev*10+rem;
num=num/10;
}

if (copy == rev)
strcpy(sBuf,"palindrome");

else
strcpy(sBuf,"not palindrome");

printf("\nServer: %s\n",sBuf);

sendto(lfd,sBuf,sizeof sBuf,0,(struct sockaddr *)&client,n);

close(lfd);
}
Client
// UDP Client
(For palindrome )

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

main() {

struct
sockaddr_in server;

int lfd,n;

char rBuf[100]="",sBuf[100]="";

lfd=socket(AF_INET,SOCK_DGRAM,0);

server.sin_family=AF_INET;
server.sin_port=2001;

server.sin_addr.s_addr=inet_addr("127.0.0.1");

printf("\nClient ready....\n");
n=sizeof server;

printf("\nClient:");

gets(sBuf);

sendto(lfd,sBuf,sizeof sBuf,0,(struct sockaddr *)&server,n);

recvfrom(lfd,rBuf,sizeof rBuf,0,(struct sockaddr *)&server,&n);

printf("\nServer:%s",rBuf);

close(lfd);

}
Chat(any no of messages can be sent ,terminate by sending “exit” from client )

TCP
SERVER
// TCP Chat : Server(chat program)
//type "exit" from client side to terminate

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>

main()
{

struct sockaddr_in client,server;


int lfd,n,confd;
char rBuf[100]="",sBuf[100]="";

lfd=socket(AF_INET,SOCK_STREAM,0);

server.sin_family=AF_INET;

server.sin_port=2000;

server.sin_addr.s_addr=inet_addr("127.0.0.1");

bind(lfd,(struct sockaddr *)&server,sizeof server);


listen(lfd,1);

printf("\nServer ready,waiting for client....\n");

n=sizeof client; confd=accept(lfd,(struct sockaddr *)&client,&n);

while(1){
recv(confd,rBuf,sizeof rBuf,0);

printf("\nClient:%s",rBuf);

if(strcmp(rBuf,"exit")==0)
break;

printf("\nServer: ");
gets(sBuf);S

send(confd,sBuf,sizeof sBuf,0);

printf("\n");
}
close(confd);

close(lfd);

}
CLIENT

// TCP Chat : Client(for chat)

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

main() {

struct sockaddr_in server;


int lfd;
char rBuf[100]="",sBuf[100]="";

lfd=socket(AF_INET,SOCK_STREAM,0);

server.sin_family=AF_INET;
server.sin_port=2000;

server.sin_addr.s_addr=inet_addr("127.0.0.1");

printf("\nClient ready....\n");
connect(lfd,(struct sockaddr *)&server,sizeof server);

while(1){
printf("\nClient:");

gets(sBuf);

send(lfd,sBuf,sizeof sBuf,0);

if(strcmp(sBuf,"exit")==0)
break;

recv(lfd,rBuf,sizeof rBuf,0);

printf("\nServer:%s",rBuf);

printf("\n");
}
close(lfd);

You might also like