java - Python socket problems on server side -
on python server plan receive parameters java client, run them through neural network , send result back. messages numpy arrays converted strings communication process. i'am not far yet trying pass dummy string client envoke server routine , fine when send string back. when call mod.predict(arr) inside loop or not concatenate received data reply, server doesn't react. have idea how done?
server - 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()
client - 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();
server code:
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 python") conn.close() except: break sock.close()
this correct, because curl says so.
➜ ~ curl -v localhost:5000 * rebuilt url to: localhost:5000/ * trying 127.0.0.1... * connected localhost (127.0.0.1) port 5000 (#0) > / http/1.1 > host: localhost:5000 > user-agent: curl/7.43.0 > accept: */* > * connection #0 host localhost left intact hello world python%
oh, , client code wrong well. need create streaming socket, not datagram socket (which have).
client code:
import java.net.*; import java.io.*; class main { public static void main(string args[]) throws exception { // constructor creates 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(); } }
Comments
Post a Comment