© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies


CPP Program For Simple Message Passing:

Server Program:- #include #include #include include #include #include #include #include #include #include #defineMAX 100 int cwork(int); intmain() { int sockmain,sockclient,child; struct sockaddr_in serv; char str[100]; int port=4056; if((sockmain=socket(AF_INET,SOCK_STREAM,0))<0) { cout<<"server cannot open main socket!"; 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(sockmain,struct sockaddr*)&serv,sizeof(serv)))<0) { cout<<"server bind failed!", exit(0); listen(sockmain,15); for(;;) { if((sockclient=accept(sockmain,struct sockaddr*)NULL,0))<0) { cout<<"bad client socket"; exit(1); } if((child=fork())<0) { cout<<"failed to create child"; exit(1); } else if (child==0) { close(sockmain); cwork(sockclient); close(sockclient); exit(0); } } #define LEN 81 int cwork(int sockclient) { char buf[LEN]; int msglen; bzero(buf,LEN); if(msglen=recv(sockclient,buf,LEN,0))<0) { cout<<"Bad recv by chile"; exit(1); } cout<<"\nsocket used:"< #include #include #include #include #include #include #include #include #include #include #include #define MAX 100 main(int argc,char**argv) { int sockfd,n; char str[100]; struct sockaddr_inserv; if(argc!=3) { printf("\nError! usage:./a.out\n"); exit(0); } if(sockfd=socket(AF_INET

SAMPLE INPUT OUTPUT:

Server Side

[cseb58@localhost cseb58]$ c++server.cpp
[cseb58@localhost cseb58] $./a.out
[cseb58@localhost cseb58]$./a.out
socket used:4
message:Helloserver

Client Side:

[cseb58@localhost cseb58]$ c++client.cpp]
[cseb58@localhost cseb58]$./a.out 127.0.0.1 5852
connected
enter message to server:Helloserver
from server:message received...
[cseb58@localhost cseb58]$

 
SLogix Student Projects
bottom