2013-05-19 28 views
1

我花了很多時間來找出問題出在哪裏,但沒有成功。服務器啓動正確,但是當我啓動客戶端時,我收到「意外錯誤」異常。我也改變了端口,沒有任何效果。我該怎麼做才能做到這一點?Java套接字 - 簡單的程序不起作用

/* Server.java */ 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class Server 
{ 
private static final int PORT = 50000; 
static boolean flaga = true; 

private static ServerSocket serverSocket; 
private static Socket clientSocket; 

public static void main(String[] args) throws IOException 
{ 
    serverSocket = null; 
    try 
    { 
     serverSocket = new ServerSocket(PORT); 
    } 
    catch(IOException e) 
    { 
     System.err.println("Could not listen on port: "+PORT); 
     System.exit(1); 
    } 

    System.out.print("Wating for connection..."); 

    Thread t = new Thread(new Runnable() 
    { 
     public void run() 
     { 
      try 
      { 
       while(flaga) 
       { 
        System.out.print("."); 
        Thread.sleep(1000); 
       } 
      } 
      catch(InterruptedException ie) 
      { 
       // 
      } 

      System.out.println("\nClient connected on port "+PORT); 
     } 
    }); 
    t.start(); 

    clientSocket = null; 
    try 
    { 
     clientSocket = serverSocket.accept(); 
     flaga = false; 
    } 
    catch(IOException e) 
    { 
     System.err.println("Accept failed."); 
     t.interrupt(); 
     System.exit(1); 
    } 

    final PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true); 
    final BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 

    t = new Thread(new Runnable() 
    { 
     public void run() 
     { 
      try 
      { 
       Thread.sleep(5000); 

       while(true) 
       { 
        out.println("Ping"); 
        System.out.println(System.currentTimeMillis()+" Ping sent"); 

        String input = in.readLine(); 

        if(input.equals("Pong")) 
        { 
         System.out.println(System.currentTimeMillis()+" Pong received"); 
        } 
        else 
        { 
         System.out.println(System.currentTimeMillis()+" Wrong answer"); 

         out.close(); 
         in.close(); 
         clientSocket.close(); 
         serverSocket.close(); 
         break; 
        } 


        Thread.sleep(5000); 
       } 
      } 
      catch(Exception e) 
      { 
       System.err.println(System.currentTimeMillis()+" Unexpected Error"); 
      } 
     } 
    }); 
    t.start(); 
} 
} 

和客戶端類

/* Client.java */ 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 

public class Client 
{ 
private static final int PORT = 50000; 
private static final String HOST = "localhost"; 

public static void main(String[] args) throws IOException 
{ 
    Socket socket = null; 

    try 
    { 
     socket = new Socket(HOST, PORT); 
    } 
    catch(Exception e) 
    { 
     System.err.println("Could not connect to "+HOST+":"+PORT); 
     System.exit(1); 
    } 

    final PrintWriter out = new PrintWriter(socket.getOutputStream(),true); 
    final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

    Thread t = new Thread(new Runnable() 
    { 
     public void run() 
     { 
      long start = System.currentTimeMillis(); 

      while (true) 
      { 
       try 
       { 
        String input = in.readLine(); 

        if (input != null) 
        { 
         System.out.println(System.currentTimeMillis() + " Server: " + input); 
        } 

        if (input.equals("Ping")) 
        { 
         if(System.currentTimeMillis()-start>30000) 
         { 
          out.println("Pon g"); 
          System.out.println(System.currentTimeMillis() + " Client: Pon g"); 
          break; 
         } 

         out.println("Pong"); 
         System.out.println(System.currentTimeMillis() + " Client: Pong"); 
        } 
       } 
       catch (IOException ioe) 
       { 
        // 
       } 
      } 
     } 
    }); 
    t.start(); 

    out.close(); 
    in.close(); 
    socket.close(); 
} 
} 

這裏是上運行

Wating for connection............ 
Client connected on port 50000 
1368986914928 Ping sent 
java.lang.NullPointerException 
    at Server$2.run(Server.java:84) 
    at java.lang.Thread.run(Thread.java:722) 
+1

發佈您的堆棧跟蹤將會很有幫助。 – Arun

+0

[你是:]](http://pastebin.com/deShUHSs) – tmq

回答

1

它顯示了你的out對象null。代替input.equals("Pong")Server.java的第84行使用input != null && input.equals("Pong")。我猜你會收到Pong received,但在後面的階段,當你聽到沒有你可以得到這個NPE

+1

感謝此信息。我會將此設置爲已接受的答案,但再次感謝duffymo :) – tmq

+0

Cheers tmq。很高興,如果它的工作原理:-) – Arun

3

你一個很大的錯誤與catch塊是空的或打印出無用輸出信息。

如果您打印或記錄堆棧跟蹤,您將獲得更多信息。這是必須的。

你需要一些介紹說明 - 看看這個,看看它是如何不同於你的。

http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html

+0

感謝這個鏈接,有什麼我需要做的很好的例子:) – tmq

+0

你是最受歡迎的。 Oracle/Sun在網絡上提供了大量的教程信息。讓它成爲Java如何提問的第一站。 – duffymo