2011-06-09 76 views
1

我已经写了一本基于Java的基本客户端 - 服务器套接字程序,从书中的'java-all-in-one桌面参考傻瓜'它包含3个类:BartServer,BartClient,BartQuote ..基本上BartServer.class所做的是侦听BartClient.class,执行后的BartClient将向BartServer发送命令,BartServer将根据BartClient发送的内容进行回复。基本的Java套接字,服务器不能发送也不能客户端可以接收任何东西

如果“获取”命令,BartServer将回复从“再见”的BartQuote.class 否则产生的随机报价将退出客户端... 我试图在本地主机,客户端成功连接服务器,但是BartServer不会做任何事情,也不会做任何事情ts会收到...命令无法响应或任何...我哪里出错了?对不起,我的英文不好...

BartServer: 

package socketBart; 
import java.net.*;import java.io.*;import java.util.*; 

public class BartServer { 

public static void main(String[] args) 
{ 
    int port = 1892; 

    BartQuote bart = new BartQuote(); 

    try 
    { 
     System.out.println("BartServer 1.0"); 
     System.out.println("Listening on port "+port); 
     ServerSocket ss = new ServerSocket(port); 
     Socket s = ss.accept(); // WAITS for a client to connect, once connected, returns a socket object 
     // once clients connects and socket s is created, it will proceed to the next line 

     String client; 
     client = s.getInetAddress().toString(); 
     System.out.println("Connected to "+ client); 

     Scanner in = new Scanner(s.getInputStream()); //READS data sent to this server from CLIENTS 
     PrintWriter out = new PrintWriter(s.getOutputStream()); //SENDS data FROM this server to CLIENTS 

     out.println("Welcome to BartServer 1.0");// these lines are sent to the client 
     out.println("Enter GET to get a quote or BYE to exit."); 

     while (true) 
     { 
      String input = in.nextLine(); 
      if (input.equalsIgnoreCase("bye")) 
       break; 
      else if (input.equalsIgnoreCase("get")) 
      { 
       out.println(bart.getQuote()); 
       System.out.println("Serving "+ client); 
      } 
      else 
       out.println("Huh?"); 
     } 
     out.println("So long suckers!"); 
     s.close(); 

     System.out.println("Closed connection to " + client); 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 

} 

} 

BartClient:

package socketBart; 
import java.net.*;import java.util.*;import java.io.*; 

public class BartClient { 

public static void main(String[] args) 
{ 
    int port = 1892; 
    System.out.println("Welcome to the Bart Client\n"); 
    Socket s = getSocket(port); 

    try 
    { 
     System.out.println("Connected on port " + port); 

     Scanner in = new Scanner(s.getInputStream()); 
     PrintWriter out = new PrintWriter(s.getOutputStream(),true); 

     //discard welcome message from server 
     in.nextLine(); 

     //discard the exit instructions 
     in.nextLine(); 

     //get a quote 
     out.println("get"); 
     String quote = in.nextLine(); 

     //disconnect from server 
     out.println("bye"); 
     s.close(); 

     //write the quote of the 'chalkboard' 
     for (int i = 0; i < 20; i++) 
      System.out.println(quote); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

} 

private static Socket getSocket(int port) 
{ 
    Socket s; 
    String host; 
    InetAddress ip; 

    Scanner sc = new Scanner(System.in); 
    while (true) 
    { 
     System.out.print("Connect to server: "); 
     host = sc.nextLine(); 

     try 
     { 
      ip = InetAddress.getByName(host); 
      s = new Socket(ip,port); 
      return s; 
     } 
     catch(UnknownHostException e) 
     { 
      System.out.println("The host is unknown."); 
     } 
     catch (IOException e) 
     { 
      System.out.println("Network error, check your port."); 
     }//end catch 
    } 
} 

} 

BartQuote:

package socketBart; 
import java.util.Random; 
import java.util.ArrayList; 

public class BartQuote { 

ArrayList<String> q = new ArrayList<String>(); 

public BartQuote() 
{ 
    q.add("I will not waste chalk"); 
    q.add("I will not skateboard in the halls"); 
    q.add("I will not burp in the class"); 
    q.add("I will not instigate a revolution"); 
    q.add("It's potato, not potatoe."); 
    q.add("I will not encourage others to fly."); 
    q.add("Tar is not a plaything."); 
    q.add("I will not sell school property."); 
    q.add("I will not get very far with this attitude"); 
    q.add("I will not sell land in Florida."); 
    q.add("I will not grease the monkey bars."); 
    q.add("I am not a dentist"); 
    q.add("I will finish what I started."); 
    q.add("Hamsters cannot fly"); 
} 

public String getQuote() 
{ 
    int i = new Random().nextInt(q.size()); 
    return q.get(i); 
} 



} 
+3

您是否在向其写入数据之后尝试刷新PrintWriter? – Voo 2011-06-09 17:31:08

+0

我反对没有找到任何与你的程序错误,我被困在某个类似的情况...好吧,它听起来很愚蠢,但尝试颠倒行的顺序:扫描仪=新扫描仪(s.getInputStream()); //从CLIENTS发送到此服务器的READS数据 PrintWriter out = new PrintWriter(s.getOutputStream()); //将SENDS数据从此服务器发送到CLIENTS ...................这是第一次初始化,然后在服务器上。 – Nik 2011-06-09 17:31:30

+0

我在客户端编写'get'和'bye'命令后做了刷新...仍然无法使其工作 – Shizumaru18 2011-06-09 17:35:13

回答

4

改变这一行的服务器上:

PrintWriter out = new PrintWriter(s.getOutputStream());

到:

PrintWriter out = new PrintWriter(s.getOutputStream(), true);

这种情况将会改变,使服务器始终是一个写操作后刷新流。否则数据可能会滞留在输出流的服务器缓冲区中。

请注意,您的客户端代码已被设置为自动刷新。

+0

啊......废话..但真棒!非常感谢!!它的工作现在 – Shizumaru18 2011-06-09 17:44:46

相关问题