c - Server not able to receive message from client -


i using ubuntu 12.05.i have been trying implement stop , wait protocol through c socket programming.i have created 2 programs,one featuring server , other 1 client. expected working of code explained through comments

serversocket.c

    #include <sys/socket.h>     #include <stdio.h>     #include <stdlib.h>     #include <netinet/in.h>     #include <arpa/inet.h>     #include <unistd.h>     #include <error.h>     #include <string.h>     #include <sys/types.h>     #include <time.h>  int main(int argc,char *argv[]) {     int listenfd = 0,qlength=10,connfd = 0,t=5;     int n;     struct sockaddr_in serv;         struct sockaddr_in dest;         socklen_t socksize = sizeof(struct sockaddr_in);     char sendbuff[1024]="hi\n";     //time_t ticks;      listenfd = socket(af_inet,sock_stream,0); //socket listening connection requests       memset(&serv,'0',sizeof(serv));      serv.sin_family = af_inet;      serv.sin_addr.s_addr = htonl(inaddr_any);      serv.sin_port=htons(5000);       bind(listenfd,(struct sockaddr *)&serv,sizeof(serv)); //binding socket port      listen(listenfd,2);          connfd=accept(listenfd,(struct sockaddr *)&dest,&socksize);  //another socket sending , receiving on built socket          while(connfd){          printf("incoming connection %s-sending hi...\n",inet_ntoa(dest.sin_addr));          send(connfd,sendbuff,strlen(sendbuff),0);         //at first server sends hi          sleep(3);          n=recv(connfd,sendbuff,1024,0);                  //if hi received client,then server should receive "message received" client          sendbuff[n]='\0';          printf("%s",sendbuff);         connfd=accept(listenfd,(struct sockaddr *)&dest,&socksize); }      return 0; } 

clientsocket.c

#include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <arpa/inet.h>   int main(int argc, char *argv[]) {     int sockfd = 0, n = 0,m=2;     char recvbuff[1024];     struct sockaddr_in serv_addr;       if(argc != 2)     {         printf("\n usage: %s <ip of server> \n",argv[0]);         return 1;     }       memset(recvbuff, '0',sizeof(recvbuff));     if((sockfd = socket(af_inet, sock_stream, 0)) < 0)     {         printf("\n error : not create socket \n");         return 1;     }       memset(&serv_addr, '0', sizeof(serv_addr));       serv_addr.sin_family = af_inet;     serv_addr.sin_port = htons(5000);       if(inet_pton(af_inet, argv[1], &serv_addr.sin_addr)<=0)     {         printf("\n inet_pton error occured\n");         return 1;     }       if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)     {        printf("\n error : connect failed \n");        return 1;     }     while(m--){                                      //m 2 initially,i want sending , receiving should done 2 times     n = recv(sockfd,recvbuff,1024,0);     recvbuff[n]='\0';                               //"hi" received     printf("%s",recvbuff);                                 if(recvbuff=="hi\n")       send(sockfd,"message received",strlen("message received\n"),0);} //sending "message received"       return 0; } 

now sending messages server client works fine, client server(message received) creating problems.neither giving error, nor correct results,it gives blank screen.

you cannot compare strings using "==" operator.

you need use strcmp()

so....

if( strcmp( recvbuff, "hi\n" ) == 0 )     send(sockfd,"message received",strlen("message received\n"),0); else     printf( "[%s] != [hi\n]\n", recvbuff ); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -