0% found this document useful (0 votes)
75 views5 pages

Lab 3: Socket Program

The document describes a socket programming lab where a message is encoded on the server by replacing vowels and consonants and sent back to the client. Server and client code is provided to send a message from client to server, encode it, and send it back.

Uploaded by

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

Lab 3: Socket Program

The document describes a socket programming lab where a message is encoded on the server by replacing vowels and consonants and sent back to the client. Server and client code is provided to send a message from client to server, encode it, and send it back.

Uploaded by

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

LAB 3: SOCKET PROGRAM

NAME: SAI SILESH N.K

REG NO: 20BCE1113

QUESTION:

Send the following statement from the client and do the


message encoding on the server side and display it on the client
side. “Please Switch Off the Lights and Fan When Not in Use”
replace the vowels with their next vowel for example “a” with “e”,
“e” with “i” and so on. Similarly replace the consonants with the
next consonant in order. Retain the Capital letters even after
encoding.

AIM:

To send the statement from client and do the message encoding


on the server side from the condition given in the question.

SERVER CODE:

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

int main()

int sd,sd2,nsd,clilen,sport,len;

int port;

char sendmsg[100],rcvmsg[100]; struct sockaddr_in servaddr,cliaddr; printf("Enter the server


port:\n"); scanf("%d",&sport);

printf("Port: %d\n",sport); sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0) printf("Can't create \n");


else printf(" Socket is created\n"); servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(sport);

sd2=bind(sd,(struct sockaddr*) &servaddr,sizeof(servaddr)); if(sd2<0) printf("Can't bind\n");

else printf("Binded \n");

listen(sd,5);

clilen=sizeof(cliaddr);

nsd=accept(sd,(struct sockaddr *)&cliaddr,&clilen); if(nsd<0) printf("Can't accept\n");

else printf("Accepted\n"); recv(nsd,sendmsg,100,0);

printf("Message recieved: %s\n",sendmsg);

for(int i=0;i<strlen(sendmsg);i++)

if(sendmsg[i]>=65 && sendmsg[i]<=90) {

if(sendmsg[i]==90) sendmsg[i]='B';

else if(sendmsg[i]=='A') sendmsg[i]='E'; else if(sendmsg[i]=='E') sendmsg[i]='I'; else


if(sendmsg[i]=='I') sendmsg[i]='O'; else if(sendmsg[i]=='O') sendmsg[i]='U'; else if(sendmsg[i]=='U')
sendmsg[i]='A';

else if(sendmsg[i]=='D'||sendmsg[i]=='H'||sendmsg[i]=='N'||sendmsg[i]=='T')

sendmsg[i]+=2;

else{ sendmsg[i]+=1;}

else if(sendmsg[i]>=97 && sendmsg[i]<=122) {

if(sendmsg[i]==122) sendmsg[i]='b'; else if(sendmsg[i]=='a') sendmsg[i]='e'; else if(sendmsg[i]=='e')


sendmsg[i]='i'; else if(sendmsg[i]=='i') sendmsg[i]='o'; else if(sendmsg[i]=='o') sendmsg[i]='u'; else
if(sendmsg[i]=='u') sendmsg[i]='a';

else if(sendmsg[i]=='d'||sendmsg[i]=='h'||sendmsg[i]=='n'||sendmsg[i]=='t')

sendmsg[i]+=2;

else{sendmsg[i]+=1;}

}
}

send(nsd,sendmsg,100,0);

printf("Output : %s\n",sendmsg);

return 1;

CLIENT CODE:

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

#include<arpa/inet.h>

int main() {

int csd,cport,len;

char revmsg[100],sendmsg[100],check[10]="bye";

struct sockaddr_in servaddr;

printf("Enter the port: \n");

scanf("%d",&cport);

printf("Port: %d\n",cport);

csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0) printf("Can't create\n");

else printf(" Socket is created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(cport);

if(connect(csd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)


printf("Can't connect\n");

else

printf("Connected sucessfully\n");

do{

printf("enter the message \n");

scanf("%s",sendmsg);

send(csd,sendmsg,40,0);

recv(csd,revmsg,40,0);

printf("output : %s\n",revmsg);

}while(strcmp(sendmsg,check)!=0);

printf("connection lost");

return 0;

SERVER SCREEN OUTPUT:


CLIENT SCREEN OUTPUT:

You might also like