gpt4 book ai didi

java - 串联/不间断套接字编程

转载 作者:太空宇宙 更新时间:2023-11-04 04:27:20 25 4
gpt4 key购买 nike

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

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

任何类型的示例或解释或引用将不胜感激。

服务器正在 nowaited 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;
}
}

最佳答案

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

关于java - 串联/不间断套接字编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40201656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com