Note that there are some explanatory texts on larger screens.

plurals
  1. POServer not able to receive message from client
    primarykey
    data
    text
    <p>I am using Ubuntu 12.05.I have been trying to implement Stop and wait protocol through C socket programming.I have created two programs,one featuring the server and other one the client. Expected working of the code is explained through comments</p> <p>serversocket.c</p> <pre><code> #include &lt;sys/socket.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;netinet/in.h&gt; #include &lt;arpa/inet.h&gt; #include &lt;unistd.h&gt; #include &lt;error.h&gt; #include &lt;string.h&gt; #include &lt;sys/types.h&gt; #include &lt;time.h&gt; 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 for listening to connection requests memset(&amp;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 *)&amp;serv,sizeof(serv)); //binding the socket to a port listen(listenfd,2); connfd=accept(listenfd,(struct sockaddr *)&amp;dest,&amp;socksize); //another socket for sending and receiving on the previously built socket while(connfd){ printf("Incoming connection from %s-sending a hi...\n",inet_ntoa(dest.sin_addr)); send(connfd,sendBuff,strlen(sendBuff),0); //at first my server sends a hi sleep(3); n=recv(connfd,sendBuff,1024,0); //if hi received by the client,then server should receive "Message Received" from the client sendBuff[n]='\0'; printf("%s",sendBuff); connfd=accept(listenfd,(struct sockaddr *)&amp;dest,&amp;socksize); } return 0; } </code></pre> <p>clientsocket.c</p> <pre><code>#include &lt;sys/socket.h&gt; #include &lt;sys/types.h&gt; #include &lt;netinet/in.h&gt; #include &lt;netdb.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;errno.h&gt; #include &lt;arpa/inet.h&gt; 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 &lt;ip of server&gt; \n",argv[0]); return 1; } memset(recvBuff, '0',sizeof(recvBuff)); if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0) { printf("\n Error : Could not create socket \n"); return 1; } memset(&amp;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], &amp;serv_addr.sin_addr)&lt;=0) { printf("\n inet_pton error occured\n"); return 1; } if( connect(sockfd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) &lt; 0) { printf("\n Error : Connect Failed \n"); return 1; } while(m--){ //m is 2 initially,I want that sending and receiving should be done 2 times n = recv(sockfd,recvBuff,1024,0); recvBuff[n]='\0'; //"hi" is received printf("%s",recvBuff); if(recvBuff=="hi\n") send(sockfd,"Message Received",strlen("Message Received\n"),0);} //sending "Message received" return 0; } </code></pre> <p>Now sending messages from server to client works fine, but client to server(Message received) is creating problems.Neither it is giving an error, nor correct results,it just gives a blank screen. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload