我在java中有简单的TCP服务器。
// while(true), try catch etc.
serverSocket = new ServerSocket(4444); // Server socket
clientSocket = serverSocket.accept();
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
message = bufferedReader.readLine();
OutputStream os = clientSocket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("test");
bw.flush();
inputStreamReader.close();
clientSocket.close();
bufferedReader.close();
os.close();
osw.close();
我在PHP中构建客户端,但它依赖于获取响应,当我用Java构建相同的简单客户端时,它运行良好。如何解决这个问题?
PHP客户端:
$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_connect($socket, $address, $port);
$message = 'test';
$status = socket_sendto($socket, $message, strlen($message), MSG_EOF, $address, $port);
if($status !== FALSE) {
$message = '';
$next = '';
while ($next = socket_read($socket, 1)) {
$message .= $next;
}
var_dump($message);
} else {
echo "Failed";
}
socket_close($socket);
答案 0 :(得分:0)
我找到了解决这个问题的方法。
当我发送消息时,我必须在字符串的末尾做“\ n”。我不知道为什么。也许有人知道为什么?