2011-06-09 55 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

相關問題