My goal is to send a message from a python socket to a java socket. I looked at the resource mentioned above. However, I try my best to have the Python client talk to the Java server. Mostly because (end of line) in python is different from (at the end of line) in java.
let's say writing from python client: message 1: abcd message 2: efgh message 3: q (to quit)
On a java server: I get a 1: abcdefghq message followed by an exception because the python client closed the socket from its end.
Can someone suggest a solution for a consistent conversation between java and python.
Link I used: http://www.prasannatech.net/2008/07/socket-programming-tutorial.html
Update : I forgot to add, I'm working on TCP.
My JAVA code is as follows: (server socket)
String fromclient; ServerSocket Server = new ServerSocket (5000); System.out.println ("TCPServer Waiting for client on port 5000"); while(true) { Socket connected = Server.accept(); System.out.println( " THE CLIENT"+" "+ connected.getInetAddress() +":"+connected.getPort()+" IS 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 to Quit):" ) if (data <> 'Q' and 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 to Quit): abcd (press ENTER)
SEND (TYPE q or Q to Quit): efgh (press ENTER)
SEND (TYPE q or Q to Quit): q (press ENTER)
ON JAVA CONSOLE (Server):
TCPServer Waiting for a client on port 5000
CUSTOMER / 127.0.0.1: 1335 CONNECTED
GOT: abcdefghq