【发布时间】:2018-11-17 04:37:07
【问题描述】:
我有一个用 C 编写的 TCP/IP 服务器程序和一个用 Java 编写的相应客户端:
C 服务器
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT 0
int main(int argc, char *argv[]) {
printf("ISV_APP: Initiating TCP/IP networking.\n");
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
// Creating socket file descriptor.
if((server_fd = socket(AF_INET, SOCK_STREAM,0)) == 0)
{
printf("TCP/IP: Initiation of server socket file descriptor failed.\n" );
return -1;
}
printf("TCP/IP: Initializing server socket.\n" );
// Forcefully attaching socket to the connection port.
if(setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
{
printf("TCP/IP: Error with setsockopt call occured.\n" );
return -1;
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
printf("TCP/IP: Socket options have been set.\n" );
// Binding the socket.
if(bind(server_fd, (struct sockaddr *)&address, sizeof(address)) <0){
printf("TCP/IP: Failed to bind socket.\n" );
return -1;
}
printf("TCP/IP: Socket bound to port.\n" );
// Listen for connections
if(listen(server_fd, 3) < 0){
printf("TCP/IP: Failed to listen for incoming connections.\n" );
return -1;
}
// Print the assigned port and address so that client knows how to connect.
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
if (getsockname(server_fd, (struct sockaddr *)&sin, &len) == -1){
printf("Failed to read port number.\n" );
}
else{
// Print port
unsigned int my_port = ntohs(sin.sin_port);
char my_port_str[6];
sprintf(my_port_str, "%d", my_port);
printf("\nINFO: Socket bound to port: " );
printf(my_port_str);
printf("\n");
// Print address
char my_ip[16];
inet_ntop(AF_INET, &sin.sin_addr, my_ip, sizeof(my_ip));
printf("INFO: Server IP Address: ");
printf(my_ip);
printf("\n");
printf("\nServer is listening for incoming connections...\n" );
}
// Accept connection
if((new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen))<0){
printf("TCP/IP: Failed to accept incoming connection.\n" );
return -1;
}
printf("TCP/IP: Connection successfully established.\n" );
printf("\n");
fflush(stdout);
// Connection successful. You can send and receive data now.
// Get message from client.
printf("Waiting for message from client...\n");
valread = read(new_socket, buffer, 1024);
printf("FROM CLIENT: %s\n", buffer);
// Pass message to client.
printf("Sending message to client...\n");
char *hello = "serversayshello";
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
// Close file descriptors and end program.
close(new_socket);
close(server_fd);
printf("End of program.");
exit(EXIT_SUCCESS);
}
Java 客户端
import java.io.*;
import java.net.*;
public class MobileClientPrototype {
public static void main(String[] args) throws IOException{
// Used to get user input.
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter IP Address:");
String host = inFromUser.readLine();
System.out.print("Please enter port number:");
int port = Integer.parseInt(inFromUser.readLine());
// Initialize Socket
Socket clientSocket = new Socket(host, port);
// Streams for communication with server
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// Ask user for string message which will be sent to server
String sentence;
System.out.print("\nPlease enter message:\n");
sentence = inFromUser.readLine();
// Send message to server
outToServer.writeBytes(sentence + '\n');
System.out.print("Message sent!\n\n");
// Wait for response message from server
String responseSentence;
System.out.print("Awaiting response...\n");
responseSentence = inFromServer.readLine();
// Print response
System.out.println("RESPONSE FROM SERVER:\n" + responseSentence);
// Close socket and end program
System.out.print("\nTerminating connection...");
clientSocket.close();
System.out.print("\nEnd of program.");
}
}
场景
我在本地机器上同时运行服务器和客户端程序。对于客户端,我输入127.0.0.1 作为 IP 和服务器打印的端口号。建立连接后,您可以向客户端输入一条消息,该消息将发送到服务器。之后,服务器以 serversayshello 响应。
它适用于一些短字符串,例如,我可以传输 hello:
但是,对于一些较长的字符串,客户端一收到服务器的响应就会抛出SocketException: Connection reset。
在这里,我正在尝试从客户端发送 hello 到服务器。这就是发生的事情:
同样值得注意的是,服务器显然没有收到完整的消息,因为它只打印了 hello from。
错误
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at MobileClientPrototype.main(MobileClientPrototype.java:38)
传输似乎适用于“hello”、“123456”、“foo”、“bar”、“g g”等短字符串。
一些不起作用的字符串是: “Hello”(带有大写 H)、“来自客户端的 hello”、“foobarfoobarfoobar”。
除此之外,我还没有想出一个模式。
是什么导致客户端在传输这些较长的消息时抛出异常?
【问题讨论】:
-
read()返回ssize_t,而不是int。他们不一样。read()也不会NUL-终止字符串值。 -
'send(new_socket, hello, strlen(hello), 0);'.......'responseSentence = inFromServer.readLine();'。什么线路?