串联/不间断插座编程

时间:2016-10-23 09:58:07

标签: java c sockets hp-nonstop tandem

我是套接字编程的新手,我正在尝试使用TCP实现客户端服务器。客户端在Windows中用Java编写,服务器在C语言中用Tandem / Hp-NonStop编写。我能够连接并向服务器发送请求。

但是我无法在服务器运行时从客户端向客户端发送响应。只有当我停止服务器时,它才会将响应发送给客户端。

任何形式的例子或解释或引用都将不胜感激。

服务器正在运行I / O.以下是我的服务器代码:

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 */

以下是我发送和接收请求和响应的客户端代码。

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;
    }
}

2 个答案:

答案 0 :(得分:0)

您的服务器端代码完全错误。它真的接受没有错误吗?您不需要创建第二个套接字,您应该在第一个套接字上调用accept。

答案 1 :(得分:0)

尝试使用telnet实用程序发送请求,您使用的客户端可能正在缓冲可能使服务器看起来没有响应的数据。