【问题标题】:Client-Server TCP communication客户端-服务器 TCP 通信
【发布时间】:2012-04-19 15:40:02
【问题描述】:

服务器应该向客户端发送消息“Hello :: enter QUIT to exit”,然后客户端输入任何文本,服务器回显客户端的文本,在其消息前添加“From server:”。 但似乎顺序混淆了,我似乎找不到在哪里!我整天都在做这个!

这是服务器的代码:

    import java.net.*;


public class Server {

    public static void main(String[] args) {

        int nreq = 1;
        try
        {
            ServerSocket sock = new ServerSocket (8080);
            for (;;)
            {
                Socket newsock = sock.accept();
                System.out.println("Creating thread ...");
                Thread t = new ThreadHandler(newsock,nreq);
                t.start();
            }
        }
        catch (Exception e)
        {
            System.out.println("IO error " + e);
        }
        System.out.println("End!");
    }
}

线程处理器代码:

    import java.io.*;
import java.net.*;

class ThreadHandler extends Thread {
    Socket newsock;
    int n;

    ThreadHandler(Socket s, int v) {
        newsock = s;
        n = v;
    }

    // @SuppressWarnings("deprecation")
    public void run() {
        try {

            PrintWriter outp = new PrintWriter(newsock.getOutputStream(), true);
            BufferedReader inp = new BufferedReader(new InputStreamReader(
                    newsock.getInputStream()));
            outp.println("Hello :: enter QUIT to exit");
            boolean more_data = true;
            String line;
            while (more_data) {
                line = inp.readLine();
                if (line == null) {
                    more_data = false;
                } else {
                    outp.println("From server: " + line + "\n");
                    if (line.trim().equals("QUIT"))
                        more_data = false;
                }
            }
            newsock.close();
        } catch (Exception e) {
            System.out.println("IO error " + e);
        }

    }
}

以及客户端代码:

 import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client {
    // @SuppressWarnings("deprecation")
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        try {
            Socket s = new Socket("localhost", 8080);
            PrintWriter outp = new PrintWriter(s.getOutputStream(), true);
            BufferedReader inp = new BufferedReader(new InputStreamReader(
                    s.getInputStream()));
            boolean more_data = true;
            System.out.println("Established connection");
            String line;
            while (more_data) {
                line = inp.readLine();
                String userInput = scanner.nextLine();
                outp.println(userInput);
                if (line == null) {
                    more_data = false;
                } else
                    System.out.println(line);
            }
            System.out.println("end of while");
        } catch (Exception e) {
            System.out.println("IO error " + e);
        }
    }
}

我正在测试它,所以在我打算让客户端成为 Android 手机之后 - 如果可能的话 -


更新:

我已将服务器的代码更改为:

outp.println("Hello :: enter QUIT to exit \n");
        boolean more_data = true;
        String line;
        while (more_data) {
        line = inp.readLine();
        System.out.println("Message '" + line + "' echoed back to client.");// !!
        if (line == null) {
            System.out.println("line = null");
            more_data = false;
        } else {
            outp.println("From server: " + line + ". \n");
            if (line.trim().equals("QUIT"))
                more_data = false;
        }
    }
    newsock.close();
    System.out.println("Disconnected from client number: " + n);

并按照 Luis Miguel Serrano 的建议在 Hello 消息的末尾添加“\n”,并将客户端更改如下:

boolean more_data = true;
        System.out.println("Established connection");
        String line;// = inp.readLine();

        while (more_data) {
            line = inp.readLine();
            System.out.println(line);
            if (line == null) {
                // nothing read
                more_data = false;
            } else
                line = inp.readLine();
            System.out.println(line);
            String userInput = scanner.nextLine();
            if (userInput.trim() == "QUIT") {
                s.close();
                System.out.println("Disconnected from server.");
                more_data = false;
            } else
                outp.println(userInput);

        }
        System.out.println("end of while");

现在可以正常使用了。

如果有人能向我推荐一些 Android 客户端-java 服务器教程,将不胜感激。

【问题讨论】:

  • 你得到了哪种行为?哪些错误(如果有)?
  • 没有错误,但是消息的顺序混淆了。服务器应该发送“Hello :: .....”然后客户端应该输入一条消息,以便服务器回显它。但它等待客户端输入消息,然后发送“Hello :: ....”消息

标签: android sockets tcp client


【解决方案1】:

按照您的评论顺序,这可能是一个冲洗问题。尝试添加以下行:

outp.flush();

之后:

outp.println("Hello :: enter QUIT to exit");

当您写入流时,有时您写入的内容会保存在缓冲区中。如果要确保缓冲区被清空并实际发送字符串,则需要调用 flush() 方法。

更新

另外,将“\n”添加到来自服务器的 Hello 欢迎消息的末尾。我认为这会让它发挥作用。

【讨论】:

  • 它还在做同样的事情!无法弄清楚为什么服务器的消息不会发送到客户端! - 还是谢谢你
  • 在 Hello 字符串的末尾添加“\n”。我敢打赌就是这样。我突然想到:)
  • 谢谢路易斯。这有帮助 :) 现在可以正常工作了。
猜你喜欢
  • 1970-01-01
  • 2013-12-14
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
  • 2023-03-18
  • 2016-05-16
  • 2012-04-21
  • 1970-01-01
相关资源
最近更新 更多