2012-04-05 327 views
1

服务器应该向客户端发送消息“Hello :: enter QUIT退出”,然后客户端键入任意文本,服务器回显客户端的文本,在他们之前添加“From server:”信息。 但似乎有一个混乱的顺序,我似乎无法找到的地方!我一整天都在这里!客户端 - 服务器TCP通信

这是服务器的代码:

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); 

,并在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服务器教程将不胜感激。

+0

你会得到哪些行为?哪些错误(如果有的话)? – 2012-04-05 21:46:00

+0

没有错误,但消息的顺序混淆了。服务器应该发送“Hello :: .....”,那么客户端应该输入一条消息,以便服务器回应它。但它等待客户端输入消息,然后发送“Hello :: ....”消息 – 2012-04-05 21:47:56

回答

1

您的评论的顺序,它可能是一个潮红的问题。尝试添加下面一行:

outp.flush(); 

后:

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

当你写一个流,有时你写的东西都保存在一个缓冲区。如果你想确保缓冲区被清空并且字符串被实际发送,你需要调用flush()方法。

更新

此外,“\ n”添加到从服务器的欢迎你好消息的结尾。我认为这会使它工作。

+0

它仍然在做同样的事情!无法弄清楚为什么服务器的消息不会发送到客户端! - 无论如何感谢您 – 2012-04-05 22:07:30

+0

将“\ n”添加到您的Hello字符串的末尾。我敢打赌是这样。刚刚发生在我身上:) – 2012-04-05 22:09:20

+0

谢谢路易斯。这有助于:)现在工作正常。 – 2012-04-06 10:34:42