简体   繁体   English

服务器/ Android客户端仅接收/发送一次

[英]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): 我想将一串文本从Android手机发送到PC上运行的Java服务器,并且可以运行,但是只有一次,它会收到第一个字符串,但是当我在手机上键入另一个字符串并按下按钮时,服务器什么也没收到,(这是我的android应用代码):

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: 这是Java服务器的代码:

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. 我是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. 除此之外,似乎每次在客户端单击时都会创建一个新的Socket对象。 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. 在客户端重用Socket ,而不是为每次点击都创建一个新Socket eg make your client variable a class member. 例如,使您的client变量成为类成员。
  2. On the server side after each message call clientSocket = serverSocket.accept(); 在每个消息调用之后的服务器端, clientSocket = serverSocket.accept(); again to create a new Socket . 再次创建一个新的Socket This new server side Socket will correspond to the new Socket on the client. 这个新的服务器端Socket将与客户端上的新套接字相对应。

The first option is considered more efficient especially as the number of connections and messages you want to handle increases. 第一种选择被认为效率更高,尤其是当您要处理的连接和消息数量增加时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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