在python服务器上,我计划从java客户端接收参数,通过神经网络运行它们并将结果发回。消息将是numpy数组转换为字符串以进行通信过程。 然而,我并没有那么远,试图从客户端传递一些虚拟字符串只是为了调用服务器例程,当我只是发回一个字符串时一切都很好。但是,当我调用mod.predict(arr)INSIDE循环或不将接收到的数据连接到回复时,服务器没有做出反应。有谁知道我怎么能做到这一点?
服务器 - Python:
HOST = 'localhost'
PORT = 7777
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(10)
while True:
conn, addr = s.accept()
data = conn.recv(1024)+"\r\n"
pred = mod.predict(arr)
reply = 'Answer..' + pred + data # +'\r\n'
if not data:
break
conn.send(reply)
conn.close()
客户端 - Java:
Socket socket = new Socket("localhost", 7777);
OutputStream out = socket.getOutputStream();
PrintStream ps = new PrintStream(out, true);
ps.println("Hello Python!");
InputStream in = socket.getInputStream();
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
while (buff.ready()) {
System.out.println(buff.readLine());
}
socket.close();
答案 0 :(得分:0)
服务器代码:
import socket
sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
sock.bind(("localhost", 5000))
sock.listen()
while True:
try:
sock.listen()
conn, addr = sock.accept()
print(conn.recv(1024))
conn.send(b"Hello world from Python")
conn.close()
except:
break
sock.close()
这是正确的,因为cURL这样说。
➜ ~ curl -v localhost:5000
* Rebuilt URL to: localhost:5000/
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 5000 (#0)
> GET / HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.43.0
> Accept: */*
>
* Connection #0 to host localhost left intact
Hello world from Python%
哦,你的客户端代码也是错误的。您需要创建一个流式套接字,而不是数据报套接字(这就是您所拥有的)。
客户代码:
import java.net.*;
import java.io.*;
class Main
{
public static void main(String args[]) throws Exception
{
// This constructor creates a streaming socket
Socket socket = new Socket(InetAddress.getByName(null), 5000);
OutputStream out = socket.getOutputStream();
PrintStream ps = new PrintStream(out, true);
ps.println("Hello Python!");
InputStream in = socket.getInputStream();
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
while (buff.ready()) {
System.out.println(buff.readLine());
}
socket.close();
}
}
答案 1 :(得分:0)
这就是服务器代码现在的样子。当我从命令行启动服务器然后从eclipse运行java-client时,所有表达式都在控制台中正确计算和打印。真正奇怪的是,当我传递一些字符串作为回复并且它似乎有效时,它可以在我在while循环中添加一些print参数时停止工作,如下面的代码中所示。
import socket
import sys
import pickle
import numpy as np
f=open('C:\\nn.pkl','rb')
mod=pickle.load(f)
HOST = 'localhost'
PORT = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
s.bind((HOST, PORT))
print 'Socket bind complete'
s.listen(10)
print 'Socket now listening'
while True:
try:
s.listen(10)
conn, addr = s.accept()
data = conn.recv(1024)
print data
arr = np.array([1.0, 0.9712, 0.01019, 0.333, 0.01008, 0.5625, 0.75, 0.4, 0.25, 0.6, 0.0])
pred = np.array_str(mod.predict(arr))
print pred
reply = "Answer.." + pred
conn.send(reply)
conn.close()
except:
break
s.close()
另外,当我在服务器运行时反复运行java客户端时,我有时会得到一个答案,而不是因为buff.ready()在每次运行时都不是真的。
public static void main(String args[]) throws IOException {
Socket socket = new Socket(InetAddress.getByName(null), 5000);
OutputStream out = socket.getOutputStream();
PrintStream ps = new PrintStream(out, true);
ps.println("Hello Python!");
InputStream in = socket.getInputStream();
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
while (buff.ready()) {
System.out.println(buff.readLine());
}
}