Communication between python client and java server -
my aim send message python socket java socket. did out on resource mentioned above. struggling make python client talk java server. because (end of line) in python different in java.
say write python client: message 1: abcd message 2: efgh message 3: q (to quit)
at java server: receive message 1:abcdefghq followed exception because python client had closed socket end.
could please suggest solution consistent talk between java , python.
reference used: http://www.prasannatech.net/2008/07/socket-programming-tutorial.html
update: forgot add, working on tcp.
my java code goes this:(server socket)
string fromclient; serversocket server = new serversocket (5000); system.out.println ("tcpserver waiting client on port 5000"); while(true) { socket connected = server.accept(); system.out.println( " client"+" "+ connected.getinetaddress() +":"+connected.getport()+" connected "); bufferedreader infromclient = new bufferedreader(new inputstreamreader (connected.getinputstream())); while ( true ) { fromclient = infromclient.readline(); if ( fromclient.equals("q") || fromclient.equals("q") ) { connected.close(); break; } else { system.out.println( "recieved:" + fromclient ); } } }
my python code : (client socket)
client_socket = socket.socket(socket.af_inet, socket.sock_stream) client_socket.connect(("localhost", 5000)) while 1: data = raw_input ( "send( type q or q quit):" ) if (data <> 'q' , data <> 'q'): client_socket.send(data) else: client_socket.send(data) client_socket.close() break;
output::
on python console(client):
send( type q or q quit):abcd ( pressing enter)
send( type q or q quit):efgh ( pressing enter)
send( type q or q quit):q ( pressing enter)
on java console(server):
tcpserver waiting client on port 5000
the client /127.0.0.1:1335 connected
recieved:abcdefghq
you need append \n
end of data...
i.e. :
client_socket.send(data + '\n')
Comments
Post a Comment