Sunday 13 December 2020

Android can't send PrintWriter message twice in socket with Java. It is also possible instead of sending me a Java code to send a Python code

I created a code that sends commands to a server I wrote using Python. The code only works once (the server receives what I sent the first time) but the second time it seems that nothing is sent because the server does not receive new information it keeps waiting for new information. My code:

    StringBuilder Data = new StringBuilder(); // The Data to send

    public void Send(View view) {
        Thread send = new Thread() {

            @Override
            public void run() {
                try {
                    Socket socket = new Socket("20.7.65.2", 6398); // Create connection to the server
                    OutputStream output = socket.getOutputStream(); // Get the key to output to server

                    PrintWriter writer = new PrintWriter(output, true);
                    writer.println(Data.toString()); // Send the data

                    Data.setLength(0); // Delete "Data" contant


                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };

        send.start();
    }

    public void Continue(View view) { 
        Data.append("Hello from client"); // Append to "Data"

        Send(null); // Run the send functionn (again)

    }


    }

My Python Server:

import socket, time

soc = socket.socket()
soc.bind((socket.gethostname(), 6398))

soc.listen(5)
(client, (ipNum, portNum)) = soc.accept()


print("Client connected")

while True:
    message = client.recv(1024).decode()

    if(message != ""):
        print(message)
    time.sleep(0.1)

In short, I try to run the run function twice. The first time it sends to the server and it receives the information, and the second time the server is still waiting for the information and not receiving what I sent again. Maybe it's because he's not available to receive all messages sent to him from all clients

It is also possible instead of sending me a Java code to send a Python code that will work and receive all messages from all clients



from Android can't send PrintWriter message twice in socket with Java. It is also possible instead of sending me a Java code to send a Python code

No comments:

Post a Comment