首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tandem/不间断套接字编程

Tandem/不间断套接字编程
EN

Stack Overflow用户
提问于 2016-10-23 17:58:07
回答 2查看 1.1K关注 0票数 2

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

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

任何类型的例子、解释或参考都将不胜感激。

服务器正在未等待的I/O中运行。以下是我的服务器代码:

代码语言:javascript
复制
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 */

下面是我的客户端代码,用于发送和接收请求和响应。

代码语言:javascript
复制
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;
    }
}
EN

回答 2

Stack Overflow用户

发布于 2016-10-23 18:08:05

你的服务器端代码是完全错误的。它真的可以毫无错误地接受吗?您不需要创建第二个套接字,并且应该在第一个套接字上调用accept。

票数 0
EN

Stack Overflow用户

发布于 2016-12-06 19:20:38

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40201656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档