简体   繁体   English

当客户端发送的数据未到达服务器但客户端断开发送至服务器的数据的连接时,连接成功

[英]Connection is success, when client sent data not reached to server but client disconnects data sent to server

Here i put some code, connection establish but whenever client wants to sent some data to server data not sent if client dis-connect data as soon as reaches to server how to fix that problem. 在这里,我放置了一些代码,建立了连接,但是如果客户端希望尽快将一些数据发送到服务器,那么只要客户端断开与服务器之间的连接数据就不会发送该数据。

what i want : if connection establish and connection not close. 我想要的是:如果连接建立且连接未关闭。

public class Server extends AppCompatActivity {

private ServerSocket serverSocket;
Handler UIHandler;
Thread Thread1 = null;
EditText edText;
public static final int SERVERPORT = 3000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_server);
    getSupportActionBar().setTitle("Server");
    edText = (EditText) findViewById(R.id.editText);
    UIHandler = new Handler();
    this.Thread1 = new Thread(new Thread1());
    this.Thread1.start();

}

class Thread1 implements Runnable {
    InetAddress addr;

    @Override
    public void run() {
        Socket socket = null;
        try {
            addr = InetAddress.getByName(getLocalIpAddress());
            serverSocket = new ServerSocket(SERVERPORT);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        while (!Thread.currentThread().isInterrupted()) {
            try {
                socket = serverSocket.accept();

                Thread2 commThread = new Thread2(socket);

                new Thread(commThread).start();

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

private String getLocalIpAddress() throws Exception {
    String resultIpv6 = "";
    String resultIpv4 = "";

    for (Enumeration en = NetworkInterface.getNetworkInterfaces();
         en.hasMoreElements(); ) {

        NetworkInterface intf = (NetworkInterface) en.nextElement();
        for (Enumeration enumIpAddr = intf.getInetAddresses();
             enumIpAddr.hasMoreElements(); ) {

            InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
            if (!inetAddress.isLoopbackAddress()) {
                if (inetAddress instanceof Inet4Address) {
                    resultIpv4 = inetAddress.getHostAddress().toString();
                } else if (inetAddress instanceof Inet6Address) {
                    resultIpv6 = inetAddress.getHostAddress().toString();
                }
            }
        }
    }
    return ((resultIpv4.length() > 0) ? resultIpv4 : resultIpv6);
}

private class Thread2 implements Runnable {
    Socket clientSocket;
    BufferedReader input;
    String read;

    public Thread2(Socket clientSocket) {
        this.clientSocket = clientSocket;

        try {
            this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));

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

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {

            try {

                read = this.input.readLine();

                if (read != null) {

                    Server.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            edText.setText(edText.getText().toString() + "Client says: " + read + "\n");
                            Log.i("hhFULL", edText.getText().toString());
                        }
                    });
                } else {

                    Thread1 = new Thread(new Thread1());
                    Thread1.start();
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

This is probably because the client you're using is sending a message without a \\n or \\r in the end. 这可能是因为您正在使用的客户端发送的消息末尾没有\\ n或\\ r。 Below code that reads a line from inputStream waits for a newline character infinitely:- 下面的从inputStream读取一行的代码将无限期地等待换行符:-

read = this.input.readLine();

问题是解决了客户端发送一些数据时,它需要(ASCII码)换行的问题。

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

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