gpt4 book ai didi

java - java和php之间简单的客户端服务器通信

转载 作者:可可西里 更新时间:2023-11-01 12:17:23 24 4
gpt4 key购买 nike

我需要将信息从 php 客户端发送到 java 服务器,但服务器端没有接收到任何信息,尽管在服务器上成功执行了一个打印语句,来自客户端的文本无法在服务器端接收。以下是代码:

Java 服务器:

import java.io.BufferedReader;
import java.net.*;
import java.io.*;

public class javaphp2 {
private static ServerSocket socket;

private static Socket connection;
private static String command = new String();
private static String responseStr = new String();

private static int port = 4309;

public static void main(String args[]) {
System.out.println("Signal Server is running.");

try {
socket = new ServerSocket(port);

while (true) {
connection = socket.accept();

InputStreamReader inputStream = new InputStreamReader(connection.getInputStream());
DataOutputStream response = new DataOutputStream(connection.getOutputStream());
BufferedReader input = new BufferedReader(inputStream);

command = input.readLine();
//System.out.println("The input is" + command);
response.writeBytes(responseStr);
response.flush();
//response.close();

System.out.println("Running");
}
} catch (IOException e) {
System.out.println("Fail!: " + e.toString());
}

System.out.println("Closing...");
}
}

PHP 客户端:

#!/usr/local/bin/php -q
<?php
$address = '132.119.90.165';
$port = 4309;

$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_connect($socket, $address, $port);

$message = 'Apple';
$len = strlen($message);

$status = socket_sendto($socket, $message, $len, 0, $address, $port);
if($status !== FALSE)
{
$message = '';
$next = '';
while ($next = socket_read($socket, 4096))
{
$message .= $next;
}

echo $message;
}
else
{
echo "Failed";
}

socket_close($socket);
?>

最佳答案

明白了!

我们需要添加 $message = "Apple\n"; 而不是 $message = 'Apple\n';

关于java - java和php之间简单的客户端服务器通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14118371/

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