Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program-1 : OSI Model Simulation
- Code :
- #include<iostream>
- #include<string.h>
- #include<stdio.h>
- using namespace std;
- void transmitter( char* str1)
- { char a[]="AH",b[]="PH",c[]="SH",d[]="TH",e[]="NH",f[]="DH";
- strcat(a,str1);
- cout<<"Application Layer : "<<a<<"\n";
- strcat(b,a);
- cout<<"Presentation Layer : "<<b<<"\n";
- strcat(c,b);
- cout<<"Session Layer : "<<c<<"\n";
- strcat(d,c);
- cout<<"Transport Layer : "<<d<<"\n";
- strcat(e,d);
- cout<<"Network Layer : "<<e<<"\n";
- strcat(f,e);
- cout<<"Datalink Layer : "<<f<<"\n";
- cout<<"\nMESSAGE ENTERED INTO PHYSICAL LAYER AND TRANSMITTED.\n";
- cout<<"\nRECIEVER\n\n";
- cout<<"MESSAGE ENTERED INTO PHYSICAL LAYER\n\n";
- cout<<"Datalink Layer : "<<f<<"\n";
- cout<<"Network Layer : ";
- for(int i=2;i<strlen(f);i++)
- cout<<f[i];
- cout<<"\nTransport Layer : ";
- for(int i=4;i<strlen(f);i++)
- cout<<f[i];
- cout<<"\nSession Layer : ";
- for(int i=6;i<strlen(f);i++)
- cout<<f[i];
- cout<<"\nPresentation Layer : ";
- for(int i=8;i<strlen(f);i++)
- cout<<f[i];
- cout<<"\nApplication Layer : ";
- for(int i=10;i<strlen(f);i++)
- cout<<f[i];
- }
- main()
- { char str[10],add[10];
- cout<<"Input String : ";
- cin>>str;
- cout<<"Input Address : ";
- cin>>add;
- cout<<"\nTRANSMITTER\n\n";
- transmitter(str);
- }
- PROGRAM-2 : Data link layer framing methods
- 1. Character count
- 2. Character stuffing
- 3. Bit stuffing
- 1. Character Count :
- Code:
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- void main(){
- int size;
- int total=0;
- int i=1;
- int j,k=0;
- int fsize;
- printf("Enter the no. of characters: ");
- scanf("%d",&size);
- char str[size];
- printf("\nEnter the characters: ");
- scanf("%s",&str);
- while(k<size)
- {
- printf("Enter the frame %d size: ",i);
- scanf("%d",&fsize);
- char frame[fsize+1];
- i++;
- total+=fsize;
- frame[0]=(char)(fsize+48);
- for(j=1;j<fsize;j++)
- {
- if(k<size)
- {
- printf("%c\n",str[k]);
- frame[j]=str[k];
- k++;
- }
- }
- frame[fsize]='\0';
- printf("%s\n",frame);
- }
- getch();
- }
- 2. Character Stuffing
- Code:
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- void charc(void);
- void main()
- {i
- nt choice;
- while(1)
- {p
- rintf("\n\n\n1.character stuffing");
- printf("\n\n2.exit");
- printf("\n\n\nenter choice");
- scanf("%d",&choice);
- printf("%d",choice);
- if(choice>2)
- printf("\n\n invalid option....please renter");
- switch(choice)
- {
- case 1:
- charc();
- break;
- case 2:
- exit(0);
- }}}
- void charc(void)
- { char c[50],d[50],t[50];
- int i,m,j;
- clrscr();
- printf("enter the number of characters\n");
- scanf("%d",&m);
- printf("\n enter the characters\n");
- for(i=0;i<m+1;i++)
- {
- scanf("%c",&c[i]);
- }p
- rintf("\n original data\n");
- for(i=0;i<m+1;i++)
- printf("%c",c[i]);
- d[0]='d';
- d[1]='l';
- d[2]='e';
- d[3]='s';
- d[4]='t';
- d[5]='x';
- for(i=0,j=6;i<m+1;i++,j++)
- {
- if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
- {d[j]='d';
- j++;
- d[j]='l';
- j++;
- d[j]='e';
- j++;
- m=m+3;
- }d
- [j]=c[i];
- }
- m=m+6;
- m++;
- d[m]='d';
- m++;
- d[m]='l';
- m++;
- d[m]='e';
- m++;
- d[m]='e';
- m++;
- d[m]='t';
- m++;
- d[m]='x';
- m++;
- printf("\n\n transmitted data: \n");
- for(i=0;i<m;i++)
- {
- printf("%c",d[i]);
- }f
- or(i=6,j=0;i<m-6;i++,j++)
- {
- if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l'&&d[i+5]=='e
- ')
- i=i+3;
- t[j]=d[i];
- }p
- rintf("\n\nreceived data:");
- for(i=0;i<j;i++)
- {printf("%c",t[i]);
- }}
- Output :
- 3.Bit Stuffing
- Code:
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- void main() {
- int i, j,count=0,nl;
- char str[100];
- clrscr();
- printf("enter the bit string: ");
- gets(str);
- for (i=0;i<strlen(str);i++) {
- count=0;
- for (j=i;j<=(i+5);j++) {
- if(str[j]=='1') {
- count++;
- }
- }
- if(count==6) {
- nl=strlen(str)+2;
- for (;nl>=(i+5);nl--) {
- str[nl]=str[nl-1];
- }
- str[i+5]='0';
- i=i+7;
- }}
- puts(str);
- getch();
- }
- Output:
- PROGRAM-3 : To Calculate the Checksum
- Code:
- #include<stdio.h>
- #include<conio.h>
- #include<math.h>
- int sender(int b[10],int k)
- {
- intchecksum,sum=0,i;
- printf("\n****SENDER****\n");
- for(i=0;i<k;i++)
- sum+=b[i];
- printf("SUM IS: %d",sum);
- checksum=~sum;
- printf("\nSENDER's CHECKSUM IS:%d",checksum);
- return checksum;
- }
- int receiver(int c[10],intk,intscheck)
- {
- intchecksum,sum=0,i;
- printf("\n\n****RECEIVER****\n");
- for(i=0;i<k;i++)
- sum+=c[i];
- printf(" RECEIVER SUM IS:%d",sum);
- sum=sum+scheck;
- checksum=~sum;
- printf("\nRECEIVER's CHECKSUM IS:%d",checksum);
- return checksum;
- }
- main()
- {
- int a[10],i,m,scheck,rcheck;
- clrscr();
- printf("\nENTER SIZE OF THE STRING:");
- scanf("%d",&m);
- printf("\nENTER THE ELEMENTS OF THE ARRAY:");
- for(i=0;i<m;i++)
- scanf("%d",&a[i]);
- scheck=sender(a,m);
- rcheck=receiver(a,m,scheck);
- if(rcheck==0)
- printf("\n\nNO ERROR IN TRANSMISSION\n\n");
- else
- printf("\n\nERROR DETECTED");
- getch();
- }
- Program-4: Generation and checking of CRC
- Code:
- #include<stdio.h>
- #include<conio.h>
- #define N strlen(g)
- char t[128], cs[128], g[]="100010000";
- int a, e, c;
- void xor() {
- for(c=1;c}
- void crc() {
- for(e=0;e do {
- if(cs[0]=='1') xor();
- for(c=0;c cs[c]=t[e++];
- }while(e<=a+N-1);
- }
- void main() {
- clrscr();
- printf("\nEnter poly : "); scanf("%s",t);
- printf("\nGenerating Polynomial is : %s",g);
- a=strlen(t);
- for(e=a;e
- printf("\nModified t[u] is : %s",t);
- crc();
- printf("\nChecksum is : %s",cs);
- for(e=a;e printf("\nFinalCodeword is : %s",t);
- printf("\nTest Error detection 0(yes) 1(no) ? : ");
- scanf("%d",&e);
- if(e==0) {
- printf("Enter position where error is to inserted : ");
- scanf("%d",&e);
- t[e]=(t[e]=='0')?'1':'0';
- printf("Errorneous data : %s\n",t);
- }
- crc();
- for (e=0;(e<n-1)&&(cs[e]!='1');e++);
- if(e
- else printf("No Error Detected.");
- getch();
- }
- Program-5: Generation of hamming code
- Code:
- #include<stdio.h>
- #include<conio.h>
- void main() {
- int data[7],rec[7],i,c1,c2,c3,c;
- printf("this works for message of 4bits in size \nenter message bit one by one: ");
- scanf("%d%d%d%d",&data[0],&data[1],&data[2],&data[4]);
- data[6]=data[0]^data[2]^data[4];
- data[5]=data[0]^data[1]^data[4];
- data[3]=data[0]^data[1]^data[2];
- printf("\nthe encoded bits are given below: \n");
- for (i=0;i<7;i++) {
- printf("%d ",data[i]);
- }
- printf("\nenter the received data bits one by one: ");
- for (i=0;i<7;i++) {
- scanf("%d",&rec[i]);
- }
- c1=rec[6]^rec[4]^rec[2]^rec[0];
- c2=rec[5]^rec[4]^rec[1]^rec[0];
- c3=rec[3]^rec[2]^rec[1]^rec[0];
- c=c3*4+c2*2+c1 ;
- if(c==0) {
- printf("\ncongratulations there is no error: ");
- } else {
- printf("\nerron on the postion: %d\nthe correct message is \n",c);
- if(rec[7-c]==0)
- rec[7-c]=1; else
- rec[7-c]=0;
- for (i=0;i<7;i++) {
- printf("%d ",rec[i]);
- }
- }
- getch();
- }
- Output:
- Program 6: Sliding Window Protocol
- Code:
- #include<stdio.h>
- #include<stdlib.h>
- void main()
- {
- char sender[50],receiver[50];
- inti,winsize;
- printf("\n ENTER THE WINDOWS SIZE : ");
- scanf("%d",&winsize);
- printf("\n SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW \n");
- printf("\n ENTER THE DATA TO BE SENT: ");
- scanf("%s",sender);
- for(i=0;i<winsize;i++)
- receiver[i]=sender[i];
- receiver[i]='\0';
- printf("\n MESSAGE SEND BY THE SENDER:\n");
- printf("%s",sender);
- printf("\n WINDOW SIZE OF RECEIVER IS EXPANDED\n");
- printf("\n ACKNOWLEDGEMENT FROM RECEIVER \n");
- for(i=0;i<winsize;i++);
- printf("\n ACK:%d",i);
- printf("\n MESSAGE RECEIVED BY RECEIVER IS : ");
- printf("%s",receiver);
- printf("\n WINDOW SIZE OF RECEIVER IS SHRINKED \n");
- }
- Program 7: Chat Application using TCP/IP
- Program:
- // Server Program - Communicating to single client multiple number of times
- #include<stdio.h>
- #include<netinet/in.h>
- #include<netdb.h>
- #include<string.h>
- #define SERV_TCP_PORT 5035
- int main(intargc,char**argv)
- {
- intsockfd,newsockfd,clength;
- structsockaddr_inserv_addr,cli_addr;
- char buffer[4096];
- //creating a socket
- sockfd=socket(AF_INET,SOCK_STREAM,0);
- serv_addr.sin_family=AF_INET;
- serv_addr.sin_addr.s_addr=INADDR_ANY;
- serv_addr.sin_port=htons(SERV_TCP_PORT);
- printf("\nStart");
- //bind the socket to port address and any local interface
- bind(sockfd,(structsockaddr*)&serv_addr,sizeof(serv_addr));
- printf("\nListening...");
- printf("\n");
- listen(sockfd,5);
- clength=sizeof(cli_addr);
- newsockfd=accept(sockfd,(structsockaddr*)&cli_addr,&clength);
- //newsockfd=accept(sockfd,(structsockaddr*)NULL,NULL);.
- printf("\nAccepted");
- printf("\n");
- while(1)
- {
- bzero(buffer,4096);
- read(newsockfd,buffer,4096);
- printf("\nClient message:%s",buffer);
- write(newsockfd,buffer,strlen(buffer)+1);
- printf("\n");
- }
- close(sockfd);
- return 0;
- }
- Client Side:
- Client Program - Multiple communications to server
- #include<stdio.h>
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<netinet/in.h>
- #include<netdb.h>
- #define SERV_TCP_PORT 5035
- int main(intargc,char*argv[])
- {
- intsockfd;
- structsockaddr_inserv_addr;
- structhostent *server;
- char buffer[4096];
- int k=5,cnt=0;
- sockfd=socket(AF_INET,SOCK_STREAM,0);
- serv_addr.sin_family=AF_INET;
- serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
- serv_addr.sin_port=htons(SERV_TCP_PORT);
- printf("\nReady for sending...");
- connect(sockfd,(structsockaddr*)&serv_addr,sizeof(serv_addr));
- while(cnt<= k)
- {
- printf("\nEnter the message to send\n");
- printf("\nClient: ");
- fgets(buffer,4096,stdin);
- write(sockfd,buffer,4096);
- printf("Serverecho:%s",buffer);
- printf("\n");
- cnt++;
- }
- close(sockfd);
- return 0;
- }
- Screenshot:
- 8.TCP Client/server application for transferring a text file from client to server.
- Fileser.c
- #include<sys/socket.h>
- #include<netinet/in.h>
- #include<arpa/inet.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<errno.h>
- #include<string.h>
- #include<sys/types.h>
- int main()
- {
- int listenfd=0;int connfd=0;
- struct sockaddr_in serv_addr;
- char sendbuff[1025];
- int numrv;
- listenfd=socket(AF_INET,SOCK_STREAM,0);
- printf("socket retrieve success\n");
- memset(&serv_addr,'0',sizeof(serv_addr));
- memset(sendbuff,'0',sizeof(sendbuff));
- serv_addr.sin_family=AF_INET;
- serv_addr.sin_addr.s_addr=htons(INADDR_ANY);
- serv_addr.sin_port=htons(5000);
- bind(listenfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
- if(listen(listenfd,10)==-1){
- printf("failed to listen\n");
- return -1;
- }
- while(1)
- {
- connfd=accept(listenfd,(struct sockaddr*)NULL,NULL);
- FILE *fp=fopen("sample_file.txt","rb");
- if(fp==NULL)
- {
- printf("file open error");
- return 1;
- }
- while(1)
- {
- unsigned char buff[256]={0};
- int nread=fread(buff,1,256,fp);
- printf("bytes read %d \n",nread);
- if(nread>0)
- {
- printf("sending\n");
- write(connfd,buff,nread);
- }
- if(nread<256)
- {
- if(feof(fp))
- printf("end of file\n");
- if(ferror(fp))
- printf("error reading");
- break;
- }
- }}
- close(connfd);
- sleep(1);
- }
- fclient.c
- #include<sys/socket.h>
- #include<netinet/in.h>
- #include<arpa/inet.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<errno.h>
- #include<string.h>
- #include<sys/types.h>
- int main(){
- int sockfd=0;
- int bytesreceived=0;
- char recvbuff[256];
- memset(recvbuff,'0',sizeof(recvbuff));
- struct sockaddr_in serv_addr;
- if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
- {
- printf("\nerror:could not create socket\n");
- return 1;
- }
- serv_addr.sin_family=AF_INET;
- serv_addr.sin_port=htons(5000);
- serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
- if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
- {
- printf("\nerror:connect failed\n");
- return 1;
- }
- FILE *fp;
- fp=fopen("sample_file.txt","ab");
- if(fp==NULL)
- {
- printf("error opening file");
- return 1;
- }
- while((bytesreceived=read(sockfd,recvbuff,256))>0)
- {
- printf("bytes received %d \n",bytesreceived);
- recvbuff[bytesreceived]=0;
- fwrite(recvbuff,1,bytesreceived,fp);
- printf("%s\n",recvbuff);
- }
- if(bytesreceived<0)
- {
- printf("\nread error");
- }
- return 0;
- }
- 9. TCP based server program in C to authenticate the client’s Username and Password
- SERVER :
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #define ANSI_COLOR_GREEN "\x1b[32m"
- #define ANSI_COLOR_RESET "\x1b[0m"
- struct user {
- char *username;
- char *password;
- };
- int main() {
- /*login details*/
- struct user singleUser;
- singleUser.username = "omijn";
- singleUser.password = "qwerty";
- /*messages*/
- char * userOK_enterPass = "Please enter your password : ";
- char * userNotFound = "This username does not exist.";
- char * userAuthenticated = "User authenticated!";
- char * invalidPassword = "Invalid password.";
- /*errors*/
- int bindError = 0;
- int serverSocketFD = 0, clientFD = 0, n = 0, auth = 0;
- char receiveBuffer[1024] = "", sendBuffer[1024] = "";
- struct sockaddr_in serverAddress;
- system("clear");
- //create socket
- if((serverSocketFD = socket(AF_INET, SOCK_STREAM, 0)) == -1)
- printf("> Socket creation failed.\n");
- else
- printf("> Socket created.\n");
- //clear memory
- memset(receiveBuffer, '0', sizeof(receiveBuffer));
- memset(sendBuffer, '0', sizeof(sendBuffer));
- memset(&serverAddress, '0', sizeof(serverAddress));
- //server address
- serverAddress.sin_family = AF_INET;
- serverAddress.sin_port = htons(2884);
- serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
- printf("> Address created.\n");
- //assign name to server
- if((bind(serverSocketFD, (struct sockaddr*)&serverAddress,
- sizeof(serverAddress))) == -1) {
- printf("> Bind error.\n");
- bindError = errno;
- printf("%s\n", strerror(bindError));
- return -1;
- }
- else
- printf("> Binding successful.\n");
- //listen
- if((listen(serverSocketFD, 5)) == -1)
- printf("> Listen error.\n");
- else
- printf("> Server listening.\n");
- //accept connection
- if((clientFD = accept(serverSocketFD, (struct sockaddr *)NULL,
- NULL)) != 0)
- printf(ANSI_COLOR_GREEN "> Client connected!\n"
- ANSI_COLOR_RESET);
- else
- printf("> Client connect error.\n");
- while(!auth) {
- /* receive
- -------------------------------------------------------------------
- */
- while((n = read(clientFD, receiveBuffer,
- sizeof(receiveBuffer) - 1)) > 0) {
- if(n == -1)
- printf("> Read error.\n");
- receiveBuffer[n] = 0;
- break;
- }
- if(strcmp(receiveBuffer, singleUser.username) == 0) {
- write(clientFD, userOK_enterPass, strlen(userOK_enterPass));
- auth = 0;
- while(!auth) {
- while((n = read(clientFD, receiveBuffer, sizeof(receiveBuffer) - 1))
- > 0) {
- if(n == -1)
- printf("> Read error.\n");
- receiveBuffer[n] = 0;
- break;
- }
- if(strcmp(receiveBuffer, singleUser.password) == 0) {
- /*user authenticated*/
- auth = 1;
- printf("\n%s", userAuthenticated);
- write(clientFD, userAuthenticated,
- strlen(userAuthenticated));
- }
- else {
- auth = 0;
- printf("\n%s", invalidPassword);
- write(clientFD, invalidPassword,
- strlen(invalidPassword));
- }
- }
- }
- else {
- printf("\n%s", userNotFound);
- write(clientFD, userNotFound, strlen(userNotFound));
- }
- printf(ANSI_COLOR_GREEN "\n%s" ANSI_COLOR_RESET,
- receiveBuffer);
- //clear receive buffer for next use
- strcpy(receiveBuffer, "");
- //clear send buffer for next use
- strcpy(sendBuffer, "");
- }
- printf("\n");
- return 0;
- }
- CLIENT :
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #define ANSI_COLOR_GREEN "\x1b[32m"
- #define ANSI_COLOR_RESET "\x1b[0m"
- struct user {
- char *username;
- char *password;
- };
- int main() {
- /*login details*/
- struct user singleUser;
- singleUser.username = "omijn";
- singleUser.password = "qwerty";
- /*messages*/
- char * userOK_enterPass = "Please enter your password : ";
- char * userNotFound = "This username does not exist.";
- char * userAuthenticated = "User authenticated!";
- char * invalidPassword = "Invalid password.";
- /*errors*/
- int bindError = 0;
- int serverSocketFD = 0, clientFD = 0, n = 0, auth = 0;
- char receiveBuffer[1024] = "", sendBuffer[1024] = "";
- struct sockaddr_in serverAddress;
- system("clear");
- //create socket
- if((serverSocketFD = socket(AF_INET, SOCK_STREAM, 0)) == -1)
- printf("> Socket creation failed.\n");
- else
- printf("> Socket created.\n");
- //clear memory
- memset(receiveBuffer, '0', sizeof(receiveBuffer));
- memset(sendBuffer, '0', sizeof(sendBuffer));
- memset(&serverAddress, '0', sizeof(serverAddress));
- //server address
- serverAddress.sin_family = AF_INET;
- serverAddress.sin_port = htons(2884);
- serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
- printf("> Address created.\n");
- //assign name to server
- if((bind(serverSocketFD, (struct sockaddr*)&serverAddress,
- sizeof(serverAddress))) == -1) {
- printf("> Bind error.\n");
- bindError = errno;
- printf("%s\n", strerror(bindError));
- return -1;
- }
- else
- printf("> Binding successful.\n");
- //listen
- if((listen(serverSocketFD, 5)) == -1)
- printf("> Listen error.\n");
- else
- printf("> Server listening.\n");
- //accept connection
- if((clientFD = accept(serverSocketFD, (struct sockaddr *)NULL,
- NULL)) != 0)
- printf(ANSI_COLOR_GREEN "> Client connected!\n"
- ANSI_COLOR_RESET);
- Else
- printf("> Client connect error.\n");
- while(!auth) {
- /* receive
- -------------------------------------------------------------------
- */
- while((n = read(clientFD, receiveBuffer,
- sizeof(receiveBuffer) - 1)) > 0) {
- if(n == -1)
- printf("> Read error.\n");
- receiveBuffer[n] = 0;
- break;
- }
- if(strcmp(receiveBuffer, singleUser.username) == 0) {
- write(clientFD, userOK_enterPass, strlen(userOK_enterPass));
- auth = 0;
- while(!auth) {
- while((n = read(clientFD, receiveBuffer, sizeof(receiveBuffer) - 1)) > 0) {
- if(n == -1)
- printf("> Read error.\n");
- receiveBuffer[n] = 0;
- break;
- }
- if(strcmp(receiveBuffer, singleUser.password) == 0) {
- /*user authenticated*/
- auth = 1;
- printf("\n%s", userAuthenticated);
- write(clientFD, userAuthenticated,
- strlen(userAuthenticated));
- }
- else {
- auth = 0;
- printf("\n%s", invalidPassword);
- write(clientFD, invalidPassword,
- strlen(invalidPassword));
- }
- }
- }
- else {
- printf("\n%s", userNotFound);
- write(clientFD, userNotFound, strlen(userNotFound));
- }
- printf(ANSI_COLOR_GREEN "\n%s" ANSI_COLOR_RESET,
- receiveBuffer);
- //clear receive buffer for next use
- strcpy(receiveBuffer, "");
- //clear send buffer for next use
- strcpy(sendBuffer, "");
- }
- printf("\n");
- return 0;
- }
- Ques 10:--- Implement a message transfer from client to server process using UDP
- Code:--
- Server:-
- #include <stdio.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <stdlib.h>
- int main(){
- int udpSocket, nBytes;
- char buffer[1024];
- struct sockaddr_in serverAddr, clientAddr;
- struct sockaddr_storage serverStorage;
- socklen_t addr_size, client_addr_size;
- int i;
- /*Create UDP socket*/
- udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
- /*Configure settings in address struct*/
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_port = htons(7891);
- serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
- /*Bind socket with address struct*/
- bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
- /*Initialize size variable to be used later on*/
- addr_size = sizeof serverStorage;
- while(1){
- /* Try to receive any incoming UDP datagram. Address and port of
- requesting client will be stored on serverStorage variable */
- nBytes = recvfrom(udpSocket,buffer,1024,0,(struct sockaddr *)&serverStorage, &addr_size);
- /*Convert message received to uppercase*/
- for(i=0;i<nBytes-1;i++)
- buffer[i] = toupper(buffer[i]);
- /*Send uppercase message back to client, using serverStorage as the address*/
- sendto(udpSocket,buffer,nBytes,0,(struct sockaddr *)&serverStorage,addr_size);
- }
- return 0;
- }
- Output
- Server
- Code:-
- Client :-
- #include <stdio.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <string.h>
- int main(){
- int clientSocket, portNum, nBytes;
- char buffer[1024];
- struct sockaddr_in serverAddr;
- socklen_t addr_size;
- /*Create UDP socket*/
- clientSocket = socket(PF_INET, SOCK_DGRAM, 0);
- /*Configure settings in address struct*/
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_port = htons(7891);
- serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
- /*Initialize size variable to be used later on*/
- addr_size = sizeof serverAddr;
- while(1){
- printf("Type a sentence to send to server:\n");
- fgets(buffer,1024,stdin);
- printf("You typed: %s",buffer);
- nBytes = strlen(buffer) + 1;
- /*Send message to server*/
- sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr,addr_size);
- /*Receive message from server*/
- nBytes = recvfrom(clientSocket,buffer,1024,0,NULL, NULL);
- printf("Received from server: %s\n",buffer);
- }
- return 0;
- }
- Output
- AIM: To write a program for UDP echo client server.
- CODE :
- SERVER:
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<netinet/in.h>
- #include<unistd.h>
- #include<netdb.h>
- #include<stdio.h>
- #include<string.h>
- #include<arpa/inet.h>
- #define MAXLINE 1024
- int main(int argc,char **argv)
- {
- int sockfd;
- int n;
- socklen_t len;
- char msg[1024];
- struct sockaddr_in servaddr,cliaddr;
- sockfd=socket(AF_INET,SOCK_DGRAM,0);
- bzero(&servaddr,sizeof(servaddr));
- servaddr.sin_family=AF_INET;
- servaddr.sin_addr.s_addr=INADDR_ANY;
- servaddr.sin_port=htons(5035);
- printf("\n\n Binded");
- bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
- printf("\n\n Listening...");
- for(;;)
- {
- printf("\n ");
- len=sizeof(cliaddr);
- n=recvfrom(sockfd,msg,MAXLINE,0,(struct
- sockaddr*)&cliaddr,&len);
- printf("\n Client's Message : %s\n",msg);
- if(n<6)
- perror("send error");
- sendto(sockfd,msg,n,0,(struct sockaddr*)&cliaddr,len);
- }
- return 0;
- }
- CLIENT:
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<netinet/in.h>
- #include<string.h>
- #include<arpa/inet.h>
- #include<string.h>
- #include<arpa/inet.h>
- #include<stdio.h>
- #define MAXLINE 1024
- int main(int argc,char* argv[])
- {
- int sockfd;
- int n;
- socklen_t len;
- char sendline[1024],recvline[1024];
- struct sockaddr_in servaddr;
- strcpy(sendline,"");
- printf("\n Enter the message : ");
- scanf("%s",sendline);
- sockfd=socket(AF_INET,SOCK_DGRAM,0);
- bzero(&servaddr,sizeof(servaddr));
- servaddr.sin_family=AF_INET;
- servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
- servaddr.sin_port=htons(5035);
- connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
- len=sizeof(servaddr);
- sendto(sockfd,sendline,MAXLINE,0,(struct sockaddr*)&servaddr,len);
- n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL);
- recvline[n]=0;
- printf("\n Server's Echo : %s\n\n",recvline);
- return 0;
- }
- OUTPUT
Add Comment
Please, Sign In to add comment