Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically, you have three different issue in your code:</p> <ol> <li>the string comparison (fixed by K Scott Piel). </li> <li>The print of the received message are not coming out. You need to add <code>'\n'</code> in all your <code>printf</code> functions.</li> <li>This is the most important issue: after the first communication between the server and the client, you are calling the <code>accept</code> function in the server code. From the <a href="http://linux.die.net/man/3/accept" rel="nofollow">man <code>accept</code> page</a>, we can read </li> </ol> <blockquote> <p>The <code>accept()</code> function shall extract the first connection on the queue of pending connections, create a new socket with the same socket type protocol and address family as the specified socket, and allocate a new file descriptor for that socket.</p> </blockquote> <p>So, after the first communication, your server is waiting for a <em>new connection</em> and your client is waiting for a message from the server. The second communication will never happen.</p> <p>To solve this issue, you can use <a href="http://linux.die.net/man/3/fork" rel="nofollow"><code>fork()</code></a> API.</p> <p>Here is a fix proposal using the <code>fork()</code>:</p> <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; } //m is 2 initially,I want that sending and receiving should be done 2 times while(m--) { printf("Waiting from the server ...\n"); n = recv(sockfd,recvBuff,1024,0); recvBuff[n]= '\0'; //"hi" is received printf("%s\n", recvBuff); if(!strncmp(recvBuff, "hi\n", strlen("hi\n"))) { printf("Send ACK\n"); n = send(sockfd,"Message Received",strlen("Message Received\n"),0); printf("%d Sent ... \n", n); } } //sending "Message received" return 0; } </code></pre> <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"; pid_t pid; int m = 2; //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 printf("Server started ...\n"); 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 &gt; 0) { printf("Incoming connection from %s-sending a hi...\n",inet_ntoa(dest.sin_addr)); pid = fork(); if(pid) { /* shall continue to listen to new client */ printf("Parent shall continue listening ... \n"); connfd=accept(listenfd,(struct sockaddr *)&amp;dest,&amp;socksize); } else { printf("Chlid process: Communication with the client ... \n"); while(m--) { printf("try %d - Sending %s ... \n", m, sendBuff); 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("Data received: [%s]\n", sendBuff); /* need to add '\n' so the print will be displayed */ strcpy(sendBuff, "hi\n"); } printf("Child process will exit\n"); return 0; } } return 0; } </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      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