客户端 - 服务器TCP通信

时间:2012-04-05 21:37:39

标签: android sockets tcp client

服务器应该向客户端发送消息“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!");
    }
}

ThreadHandler代码:

    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服务器教程会很感激。

1 个答案:

答案 0 :(得分:1)

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

outp.flush();

后:

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

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

更新

此外,在服务器的Hello欢迎消息的末尾添加“\ n”。我认为这会使它发挥作用。