Jump to content

Sockets - Java Client & Python Server issues


Recommended Posts

Posted

Hi guys,

I am using a java client to communicate to a python server (done as I know python well but my Java knowledge can be shaky) but I am encountering some issues.

The client code looks like this:

public String serverMuleRequest() {
    try {
        Socket socket = new Socket("localhost", 8080);
        DataOutputStream dataoutput = new DataOutputStream(socket.getOutputStream());
        DataInputStream datainput = new DataInputStream(socket.getInputStream());


        dataoutput.writeUTF("MULE");
        dataoutput.flush();
        String str = datainput.readUTF();//in.readLine();
        System.out.println("String: " + str);

        dataoutput.close();
        datainput.close();
        socket.close();
        return str;
    } catch (Exception e) {
        e.printStackTrace();
    }


    return "";
}


@Override

public void onStart() {
    log("Test");
    String strng = serverMuleRequest();
    log("Your string");
    log(strng);

}

The python code looks like this:

data = client_socket.recv(size)
                javaRequest = data.decode('utf-8')
                if 'q^' in data.decode('utf-8'):    
                    print('Received request for exit from: ' + str(
                        address[0]) + ':' + str(address[1]))
                    break

                if("MULE" in javaRequest):
                    looper = 1
                    print("Java request: " +javaRequest)
                    while(looper > 0):
                        try:
                            muleID, muleName, muleLoc = getMuleDetails(muleaccountpointer)
                            sendData = ("MULE_INFO:" +str(muleID) + ":" + str(muleName) + ":" + str(muleLoc) + ":")
                            client_socket.send(sendData.encode('utf-8'))

When I call the java code the following happens:

1. Test is printed to the console.

2. The socket connection is established and the python server sends over the correct message (tested with a python client).

3. After this the log("your string") and log(strng) will not actually run. I am unsure if something I have done in the socket function would cause this.

 

Does anyone have any ideas as I am a bit stuck!

Thanks!

Posted

First off, stop using port 8080 because that's already an established port for HTML. 

Second off, you only call it once... 

You need to do a loop on serverMuleRequest if you want it to keep writting. Like while (true) { method } - but you don't want to keep establishing a new socket, so you'll need to assign that somewhere else before the loop 

Posted
9 hours ago, asdttt said:

First off, stop using port 8080 because that's already an established port for HTML. 

Second off, you only call it once... 

You need to do a loop on serverMuleRequest if you want it to keep writting. Like while (true) { method } - but you don't want to keep establishing a new socket, so you'll need to assign that somewhere else before the loop 

I fixed the issue it was due to me sending the response from python as Utf-8 encoded text and trying to receive in Java as utf-8 this messes up something somewhere. Sending as bytes then converting fixed it.

using port 8080 doesn’t matter I can use whatever port I like as it’s an internal application. In addition to this I only need to send the message once and get the reply from the server so there isn’t a need for the loop.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...