gpt4 book ai didi

java - 使用java通过套接字接收python json

转载 作者:行者123 更新时间:2023-11-30 08:01:13 27 4
gpt4 key购买 nike

我一直在学习通过套接字将 Java 代码连接到 Python 的多个教程。

使用 json 数组从 java 发送到 python 效果很好。但是,我似乎无法用java接收东西。我真的不明白听力应该如何进行。现在我只听 15 秒的 while 循环(python 应该在收到输入后立即发送),但我觉得我做错了很多事。也许有人有想法?

客户端.py:

import socket
import sys
import numpy as np
import json

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', 10004)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

def mysum(x):
return np.sum(x)

while True:
# Wait for a connection
print >>sys.stderr, 'waiting for a connection'
connection, client_address = sock.accept()
infile = sock.makefile();

try:
print >>sys.stderr, 'connection from', client_address

# Receive the data in small chunks and retransmit it
data = b''
while True:
new_data = connection.recv(16)
if new_data:
# connection.sendall(data)
data += new_data
else:
data += new_data[:]
print >>sys.stderr, 'no more data from', client_address
break
data= data.strip('\n');
print("data well received!: ")
print(data,)
print(np.array(json.loads(data)));

#send a new array back

sendArray = np.array( [ (1.5,2,3), (4,5,6) ] );
print("Preparing to send this:");
print(sendArray);
connection.send(json.dumps(sendArray.tolist()));

except Exception as e:
print(e)
connection.close()
print("closed");
finally:
# Clean up the connection
connection.close()
print("closed");

服务器.java:

import java.io.*;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import org.json.*;

import java.net.ServerSocket;

public class SocketTest {

public static void main(String[] args) throws IOException {


String hostName = "localhost";
int portNumber = 10004;

try (

//open a socket
Socket clientSocket = new Socket(hostName, portNumber);

BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);


) {

System.out.println("Connected");
Double[][] test2 = new Double[5][2];
test2[1][1] = 0.1;
test2[1][0] = 0.2;
test2[2][1] = 0.2;
test2[2][0] = 0.2;
test2[3][1] = 0.1;
test2[3][0] = 0.2;
test2[4][1] = 0.2;
test2[4][0] = 0.2;
test2[0][1] = 0.2;
test2[0][0] = 0.2;


System.out.println("A");
out.println(new JSONArray(test2).toString());
System.out.println("B");


long t = System.currentTimeMillis();
long end = t + 15000;


while (System.currentTimeMillis() < end) {
String response;
while ((response = in.readLine()) != null) {
System.out.println("receiving");
System.out.println( response );

}


}


//listen for input continuously?


//clientSocket.close();
} catch (JSONException e) {
e.printStackTrace();
}


}


}

python 的输出是:

data well received!: 
('[[0.2,0.2],[0.2,0.1],[0.2,0.2],[0.2,0.1],[0.2,0.2]]',)
[[ 0.2 0.2]
[ 0.2 0.1]
[ 0.2 0.2]
[ 0.2 0.1]
[ 0.2 0.2]]
Preparing to send this:
[[ 1.5 2. 3. ]
[ 4. 5. 6. ]]
closed
waiting for a connection
connection from ('127.0.0.1', 40074)

Java 的输出:

A
B

问题是 sendArray = np.array( [ (1.5,2,3), (4,5,6) ] ); java 永远不会收到。我觉得我缺少一些简单的东西来让它听...感谢您的帮助。

最佳答案

发生这种情况是因为您的 Java 代码正在阻塞。看看 B 是如何不打印到您的日志中的?这是因为它没有执行,因为这部分正在等待刷新命令:out.println(new JSONArray(test2).toString());。你需要做的是 out.flush(); 所以它会继续。

关于java - 使用java通过套接字接收python json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37933360/

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