UDP CLIENT SERVER PROGRAMS
CHAT APPLICATION
Server code:
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
int main()
{
int sd;
char buf[25];
struct sockaddr_in sadd,cadd;
//Create a UDP socket
sd=socket(AF_INET,SOCK_DGRAM,0);
//Construct the address for use with sendto/recvfrom... */
sadd.sin_family=AF_INET;
sadd.sin_addr.s_addr=inet_addr("10.29.30.207");//**
sadd.sin_port=htons(9704);
int result=bind(sd,(struct sockaddr *)&sadd,sizeof(sadd));
int len=sizeof(cadd);
while(1)
{
int m=recvfrom(sd,buf,sizeof(buf),0,(struct sockaddr *)&cadd,&len);
printf("the message recieved from client\n");
puts(buf);
printf("enter message to be sent to client\n");
gets(buf);
int n=sendto(sd,buf,sizeof(buf),0,(struct sockaddr *)&cadd,len);
}
return 0;
}
Client code:
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
int main()
{
int sd;
struct sockaddr_in address;
sd=socket(AF_INET,SOCK_DGRAM,0);
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("10.29.30.207");//**
address.sin_port=htons(9704);
char buf[25],buf1[25];
while(1)
{
printf("enter message to be sent to server\n");
fgets(buf, sizeof(buf), stdin);
int len=sizeof(address);
int m=sendto(sd,buf,sizeof(buf),0,(struct sockaddr *)&address, len);
int n=recvfrom(sd,buf,sizeof(buf),0,(struct sockaddr *)&address,&len);
printf("the message recieved from server\n");
puts(buf);
}
return 0;
}
CALCULATOR
Server code:
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
int main()
{
int sd;
int buf[25];
struct sockaddr_in sadd,cadd;
//Create a UDP socket
sd=socket(AF_INET,SOCK_DGRAM,0);
//Construct the address for use with sendto/recvfrom... */
sadd.sin_family=AF_INET;
sadd.sin_addr.s_addr=inet_addr("172.20.32.64");//**
sadd.sin_port=htons(9704);
int result=bind(sd,(struct sockaddr *)&sadd,sizeof(sadd));
int len=sizeof(cadd);
int num1,num2;
while(1)
{
int m=recvfrom(sd,buf,sizeof(buf),0,(struct sockaddr *)&cadd,&len);
num1 = buf[0];
num2 = buf[1];
char buf1[] = "Enter 1 for addition,2 for subtraction, 3 for multiplication, 4 for division";
int n=sendto(sd,buf1,sizeof(buf1),0,(struct sockaddr *)&cadd,len);
int o = recvfrom(sd,buf,sizeof(buf),0,(struct sockaddr *)&cadd,&len);
int ch = buf[0],ans;
switch(ch)
{
case 1: ans = num1 + num2;
break;
case 2: ans = num1 - num2;
break;
case 3: ans = num1*num2;
break;
case 4: ans = num1/num2;
break;
default: ans = -1;
break;
}
int an[25];
an[0] = ans;
int p = sendto(sd,an,sizeof(an),0,(struct sockaddr *)&cadd,len);
}
return 0;
}
Client code:
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
int main()
{
int sd;
struct sockaddr_in address;
sd=socket(AF_INET,SOCK_DGRAM,0);
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("172.20.32.64");//**
address.sin_port=htons(9704);
int buf[25],buf1[25];
char mess[1000];
while(1)
{
printf("\nenter first number\n");
scanf("%d",&buf[0]);
printf("\nenter second number\n");
scanf("%d",&buf[1]);
int len=sizeof(address);
int m=sendto(sd,buf,sizeof(buf),0,(struct sockaddr *)&address, len);
int n=recvfrom(sd,mess,sizeof(mess),0,(struct sockaddr *)&address,&len);
printf("message from server:%s\n",mess);
int choice;
scanf("%d",&choice);
buf[0] = choice;
buf[1] = 0;
int o =sendto(sd,buf,sizeof(buf),0,(struct sockaddr *)&address, len);
int p = recvfrom(sd,buf1,sizeof(buf1),0,(struct sockaddr *)&address,&len);
printf("answer is:%d\n",buf1[0]);
}
return 0;
}
MATRIX
Server code:
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
int main()
{
int sd;
int buf[25],buf1[25];
struct sockaddr_in sadd,cadd;
//Create a UDP socket
sd=socket(AF_INET,SOCK_DGRAM,0);
//Construct the address for use with sendto/recvfrom... */
sadd.sin_family=AF_INET;
sadd.sin_addr.s_addr=inet_addr("10.29.30.207");//**
sadd.sin_port=htons(9704);
int result=bind(sd,(struct sockaddr *)&sadd,sizeof(sadd));
int len=sizeof(cadd);
int m=recvfrom(sd,buf,sizeof(buf),0,(struct sockaddr *)&cadd,&len);
int n1 = buf[0],k=1;
int matrix[20][20];
for(int i=1;i<=n1;i++)
{
for(int j=1;j<=n1;j++)
{ int a = buf[k++];
matrix[i][j] = a;
printf("%d\t",a);
}
printf("\n");
}
int n=sendto(sd,matrix,sizeof(matrix),0,(struct sockaddr *)&cadd,len);
return 0;
}
Client code:
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
int main()
{
int sd;
struct sockaddr_in address;
sd=socket(AF_INET,SOCK_DGRAM,0);
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("10.29.30.207");//**
address.sin_port=htons(9704);
int buf[25],buf1[20][20];
printf("enter no of rows for the square matrix:");
scanf("%d",&buf[0]);
for(int i=1;i<=buf[0]*buf[0];i++)
{
printf("enter element %d:",i);
scanf("%d",&buf[i]);
}
int len=sizeof(address);
int m=sendto(sd,buf,sizeof(buf),0,(struct sockaddr *)&address, len);
int n=recvfrom(sd,buf1,sizeof(buf1),0,(struct sockaddr *)&address,&len);
printf("the matrix recieved from server:\n");
for(int i=1;i<=buf[0];i++)
{
for(int j=1;j<=buf[0];j++)
{
printf("%d\t",buf1[i][j]);
}
printf("\n");
}
return 0;
}
TCP CLIENT SERVER PROGRAMS
Echo program
Server code:
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define PORTNO 10200
int main()
{
int sockfd,newsockfd,portno,clilen,n=1;
struct sockaddr_in seraddr,cliaddr;
int i,value;
// create an unnamed socket for the server
sockfd = socket(AF_INET,SOCK_STREAM,0);
//Name the socket
seraddr.sin_family = AF_INET;
seraddr.sin_addr.s_addr = inet_addr("172.20.32.64");// **
seraddr.sin_port = htons(PORTNO);
bind(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr));
//Create a connection queue and wait for clients
listen(sockfd,5);
while (1) {
char buf[256];
printf("server waiting");
//Accept a connection
clilen = sizeof(clilen);
newsockfd=accept(sockfd,(struct sockaddr *)&cliaddr,&clilen);
//Read and write to client on client_sockfd (Logic for problem mentioned here)
n = read(newsockfd,buf,sizeof(buf));
printf(" \nMessage from Client %s \n",buf);
n = write(newsockfd,buf,sizeof(buf));
}
}
Client code:
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int len,result,sockfd,n=1;
struct sockaddr_in address;
char ch[256],buf[256];
//Create a socket for the client
sockfd = socket(AF_INET, SOCK_STREAM, 0);
//Name the socket as agreed with the server
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("172.20.32.64");
address.sin_port=htons(10200);
len = sizeof(address);
//Connect your socket to the server’s socket
result=connect(sockfd,(struct sockaddr *)&address,len);
if(result==-1)
{
perror("\nCLIENT ERROR");
exit(1);
}
//You can now read and write via sockfd (Logic for problem mentioned here)
printf("\nENTER STRING\t");
gets(ch);
ch[strlen(ch)]='\0';
write(sockfd,ch,strlen(ch));
printf("STRING SENT BACK FROM SERVER IS ..... ");
while(n){
n=read(sockfd,buf,sizeof(buf));
puts(buf);
}
}
CURRENT TIME PROGRAM
Server code:
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <time.h>
#define PORTNO 10200
int main()
{
int sockfd,newsockfd,portno,clilen,n=1,n1=1;
struct sockaddr_in seraddr,cliaddr;
int i,value;
// create an unnamed socket for the server
sockfd = socket(AF_INET,SOCK_STREAM,0);
//Name the socket
seraddr.sin_family = AF_INET;
seraddr.sin_addr.s_addr = inet_addr("10.29.30.207");// **
seraddr.sin_port = htons(PORTNO);
bind(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr));
//Create a connection queue and wait for clients
listen(sockfd,5);
time_t mytime = time(NULL);
char * time_str = ctime(&mytime);
time_str[strlen(time_str)-1] = '\0';
char buf[256];
//Accept a connection
clilen = sizeof(clilen);
newsockfd=accept(sockfd,(struct sockaddr *)&cliaddr,&clilen);
//Read and write to client on client_sockfd (Logic for problem mentioned here)
//char buf1[] = time_str;
n = write(newsockfd,time_str,sizeof(buf));
printf("%d",getpid());
int pid = getpid();
n1 = write(newsockfd,&pid,sizeof(buf));
}
Client code:
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int len,result,sockfd,n=1,n1=1;
struct sockaddr_in address;
char ch[256],buf[256];
//Create a socket for the client
sockfd = socket(AF_INET, SOCK_STREAM, 0);
//Name the socket as agreed with the server
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("10.29.30.207");
address.sin_port=htons(10200);
len = sizeof(address);
//Connect your socket to the server’s socket
result=connect(sockfd,(struct sockaddr *)&address,len);
if(result==-1)
{
perror("\nCLIENT ERROR");
exit(1);
}
//You can now read and write via sockfd (Logic for problem mentioned here)
printf("TIME SENT BACK FROM SERVER IS ..... ");
n=read(sockfd,buf,sizeof(buf));
puts(buf);
int pid;
n1=read(sockfd,&pid,sizeof(buf));
printf("process id of server:%d",pid);
}
CONVERTING SENTENCE TO UPPER CASE
Client code:
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int len,result,sockfd,n=1,n1=1;
struct sockaddr_in address;
char ch[256],buf[256];
//Create a socket for the client
sockfd = socket(AF_INET, SOCK_STREAM, 0);
//Name the socket as agreed with the server
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("10.29.30.207");
address.sin_port=htons(10200);
len = sizeof(address);
//Connect your socket to the server’s socket
result=connect(sockfd,(struct sockaddr *)&address,len);
if(result==-1)
{
perror("\nCLIENT ERROR");
exit(1);
}
//You can now read and write via sockfd (Logic for problem mentioned here)
printf("\nENTER STRING\t");
gets(ch);
ch[strlen(ch)]='\0';
write(sockfd,ch,strlen(ch));
printf("STRING SENT BACK FROM SERVER IS ..... ");
n=read(sockfd,buf,sizeof(buf));
puts(buf);
Server code:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define PORTNO 10200
int main()
{
int sockfd,newsockfd,portno,clilen,n=1,n1=1,n2=1;
struct sockaddr_in seraddr,cliaddr;
int i,value;
// create an unnamed socket for the server
sockfd = socket(AF_INET,SOCK_STREAM,0);
//Name the socket
seraddr.sin_family = AF_INET;
seraddr.sin_addr.s_addr = inet_addr("10.29.30.207");// **
seraddr.sin_port = htons(PORTNO);
bind(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr));
//Create a connection queue and wait for clients
listen(sockfd,5);
while (1) {
char buf[256];
printf("server waiting");
//Accept a connection
clilen = sizeof(clilen);
newsockfd=accept(sockfd,(struct sockaddr *)&cliaddr,&clilen);
//Read and write to client on client_sockfd (Logic for problem mentioned here)
n = read(newsockfd,buf,sizeof(buf));
printf(" \nMessage from Client %s \n",buf);
char buf1[256];
int j=0;
while(buf[j]!='\0')
{
buf1[j] = toupper(buf[j]);
j++;
}
n = write(newsockfd,buf1,sizeof(buf));
printf("port address:%d",10200);
//printf("ip address of client:%d",cliaddr.sin_addr.s_addr);
}
}