【发布时间】:2016-10-22 08:22:24
【问题描述】:
我一直在关注多个教程,通过套接字将 java 代码连接到 python。
使用 json 数组从 java 发送到 python 效果很好。但是,我似乎无法用 java 接收东西。我真的不明白应该如何进行听力。现在我只听一个 15 秒的 while 循环(python 应该在收到输入后立即发送),但我觉得我做错了什么。也许有人有想法?
client.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");
server.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从来没有收到过。我觉得我错过了一些简单的东西让它听...感谢您的帮助。
【问题讨论】:
-
如果你正在使用资源尝试为什么要关闭clientSocket?
-
你是对的,对不起,当删除 close() 时它仍然有同样的问题。
-
问:“Java”是Android,对吗?问:如果用集合代替数组会发生什么?
-
不是安卓,只是java