2

I am new to socket programming and I am trying to implement a client server using TCP. The client is written in Java in Windows and the server is written in C in Tandem/Hp-NonStop. I am able to connect and send request to the server.

But I am not able to send a response back to the client from server while it is running. Only when I stop the server it send the response to the client.

Any kind of example or explanation or references would be appreciated.

The server is running in nowaited I/O. Below is my server code:

while (1) {
/* Accept a connection on this socket. The accept call places the
client's address in the sockaddr_in structure named clientaddr.*/
    clientaddrlen = sizeof(clientaddr);
    if( accept_nw(s, (struct sockaddr *)&clientaddr, &clientaddrlen, tag) <0) {
        perror("accept");
        exit(3);
    }
    if( fe = IOCheck(acceptWait) ) { /* initially, wait -1; maybe change afterwards? */
        if( fe == 40 ) {
            printf( "Timed out after %ld secs wtg Client connect. Terminating.\n",acceptWait/100 );
            FILE_CLOSE_((short)s);
            exit(0);
        } else {
            printf( "AWAITIO error %d from accept_nw\n",fe );
            exit(3);
        }
    }
    /* Need a new socket for the data transfer
    Resembles the earlier call */
    if ((new_s = socket_nw(AF_INET, SOCK_STREAM,0,2,0)) < 0) {
        perror ("Socket 2 create failed.");
        exit (4);
    }
    /* Make the connection */
    if ( accept_nw2(new_s, (struct sockaddr *)&clientaddr, tag2) < 0) {
        perror ("2nd Accept failed.");
        exit (5);
    }
    if( fe = IOCheck(-1) ) {
        printf( "AWAITIO error %d, tag %ld from 2nd
        accept_nw\n",fe,tagBack );
        exit(4);
    }
    /* Receive data from the client.
    recv_nw() - awaitio() should be in a loop until a logical record
    has been received. In this example, we expect the short messages
    to be completed in a single recv_nw() */
    if( recv_nw(new_s, databuf, sizeof(databuf), 0, tag2) < 0 ) {
        if( errno == ESHUTDOWN || errno == ETIMEDOUT || errno == ECONNRESET ) {
            FILE_CLOSE_((short)new_s);
            continue;
        } else {
            perror( "recv_nw error" );
            exit( 6 );
        }
    }
    if( fe = IOCheck(timeout) ) {
        if( fe == 40 ) { /* abandon and start over */
            FILE_CLOSE_((short)new_s);
            continue;
        } else {
            printf( "AWAITIO error %d from recv_nw\n",fe );
            exit(6);
        }
    }
    databuf[dcount] = '\0'; /* dcount set by IOCheck */
    /* Retrieve the client name using the address in the sockaddr_in
    structure named clientaddr. A call to gethostbyaddr expects an
    IPv4 address as input. */
    hp = gethostbyaddr((char *)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET);
    /* Convert the client's 32-bit IPv4 address to a dot-formatted
    Internet address text string. A call to inet_ntoa expects an
    IPv4 address as input. */
    ap = inet_ntoa(clientaddr.sin_addr);
    port = ntohs(clientaddr.sin_port);
    printf("Request received from");
    if (hp != NULL) printf(" %s", hp->h_name);
    if (ap != NULL) printf(" (%s)", ap);
    printf(" port %d\n\"%s\"\n", port, databuf);
    /* Send a response to the client. */
    if (send_nw2(new_s, response, (int)strlen(response), 0, tag2) < 0) {
        perror("send_nw2");
        FILE_CLOSE_((short)new_s);
        continue;
    }
    if( fe = IOCheck( -1 ) ) {
        FILE_CLOSE_((short)new_s);
        continue;
    }
} /* while */

Below is my client code for sending and receiving request and response.

private String writeToAndReadFromSocket(Socket socket, String writeTo) throws Exception
{
    try 
    {
      // write text to the socket
      BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
      bufferedWriter.write(writeTo);
      bufferedWriter.flush();

      // read text from the socket
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      StringBuilder sb = new StringBuilder();
      String str;
      while ((str = bufferedReader.readLine()) != null)
      {    
        sb.append(str + "\n");
      }

      // close the reader, and return the results as a String
      bufferedReader.close();
      return sb.toString();
    } 
    catch (IOException e) 
    {
      e.printStackTrace();
      throw e;
    }
}
Ajitav Dutta
  • 99
  • 1
  • 11

2 Answers2

0

Your server side code is completely wrong. Does it really accept without errors? You don't need to create the second socket, and you should call accept on the first socket.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • The server code is a sample program provided by Hp in their NonStop TCP/IP Programming Manual. Yes, the connection is accepted without errors. For the 1st time I am calling accept in the first socket. Even If I do not create a second socket, I am facing the same issue i.e. the server is not sending the response back while it is running only when the server is stopped or the socket is closed the server send the response. – Ajitav Dutta Oct 23 '16 at 10:14
  • Bizarre. Unlike every other TCP API I have ever seen in nearly 30 years. You are reading lines but you aren't writing lines. – user207421 Oct 23 '16 at 10:58
0

Try sending request using telnet utility, the client you are using might be buffering the data that could make it seem like server is not responding..

Pras
  • 4,047
  • 10
  • 20