简体   繁体   中英

Server/Android client receives/sends only once

I wanted to send a string of text from my android phone over to a Java server running on my PC and it works but only once, it would receive the first string but when I type in another one on my phone and I press the button, the server doesn't receive anything, (here is my code for the android app):

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            messsage = etMsg.getText().toString();
            etMsg.setText("");

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try
                    {
                        client = new Socket(etIP.getText().toString(), port);
                        printwriter = new PrintWriter(client.getOutputStream());
                        printwriter.write(messsage);
                        printwriter.flush();
                        printwriter.close();
                        client.close();
                    }
                    catch (UnknownHostException e)
                    {
                        e.printStackTrace();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    });

And here is the code for the Java server:

package src;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class VRS {

public static void main(String[] args) throws IOException {
    Socket clientSocket = null;
    ServerSocket serverSocket = null;
    try{
        serverSocket = new ServerSocket(4444);
        System.out.println("server started on port 4444");
        clientSocket = serverSocket.accept();
    }catch(Exception e){} //read & display the message
        //BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputS­tream()));
        Scanner in1 = new Scanner(clientSocket.getInputStream());
        String mes; 
        while(true){
            if (in1.hasNext())
            {
                mes=in1.nextLine();
                System.out.println("Client message :"+mes + System.lineSeparator());
            }
        }
    }
}

Can anyone help me find the problem as I'm a beginner in terms of Java.

The scanner on the server is waiting for a complete token with a terminator and you are not sending one from the client. Try appending a line terminating character on the client side eg

printwriter.println(messsage);

In addition to that it seems that for every click on the client side a new Socket object is created. But the server is not waiting for a new connection. You can either :

  1. reuse the Socket on the client side instead of creating a new one for every click. eg make your client variable a class member.
  2. On the server side after each message call clientSocket = serverSocket.accept(); again to create a new Socket . This new server side Socket will correspond to the new Socket on the client.

The first option is considered more efficient especially as the number of connections and messages you want to handle increases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM