Jump to content

Sockets - Java Client & Python Server issues


Impensus

Recommended Posts

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!

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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