2011-03-22 49 views
2

我從你的網站上得到了這段代碼。java中的套接字編程問題

import java.io.*; 
import java.net.*; 

class sevr implements Runnable{ 
    public void run() { 
     ServerSocket sSkt = null; 
     Socket skt = null; 
     BufferedReader br = null; 
     BufferedWriter bw = null; 

     try{ 
      System.out.println("Server: is about to create socket"); 
      sSkt = new ServerSocket(6666); 
      System.out.println("Server: socket created"); 
     } 
     catch(IOException e){ 
      System.out.println("Server: socket creation failure"); 
     } 
     try{ 
      System.out.println("Server: is listening"); 
      skt = sSkt.accept(); 
      System.out.println("Server: Connection Established"); 
     } 
     catch(IOException e){ 
      System.out.println("Server: listening failed"); 
     } 
     try{ 
      System.out.println("Server: creating streams"); 
      br = new BufferedReader(new InputStreamReader(skt.getInputStream())); 
      bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())); 
      System.out.println("Server: stream done"); 
     } 
     catch(IOException e){ 
      System.out.println("Server: stream failed"); 
     } 
     System.out.println("Server: reading the request"); 
     try{ 
      String line = null; 
      line = br.readLine(); 
      System.out.println("Server: client said-> "+ line); 
     } 
     catch(IOException e){ 
      System.out.println("Server: reading failed"); 
     } 
     System.out.println("Server: reading fished"); 

     System.out.println("Server: responding"); 
     try{ 
      bw.write("Hi! I am server!\n"); 
      bw.flush(); 
     } 
     catch(IOException e){ 
      System.out.println("Server: responding failed"); 
     } 
     System.out.println("Server: responding finished"); 

     System.out.println("Server: is finishing"); 
     try { 
      br.close(); 
      bw.close(); 
      skt.close(); 
      sSkt.close(); 
     } catch (IOException e) { 
      System.out.println("Server: finishing failed"); 
     } 
     System.out.println("Server: done"); 
    } 
} 

class clnt implements Runnable{ 
    public void run() { 
     Socket skt = null; 
     BufferedReader br = null; 
     BufferedWriter bw = null; 

     try{ 
      System.out.println("Client: about to create socket"); 
      skt = new Socket(InetAddress.getLocalHost(),6666); 
      System.out.println("Client: socket created"); 
     } 
     catch(IOException e){ 
      System.out.println("Client: socket creation failure"); 
     } 

     try{ 
      System.out.println("Client: creating streams"); 
      br = new BufferedReader(new InputStreamReader(skt.getInputStream())); 
      bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())); 
      System.out.println("Client: stream done"); 
     } 
     catch(IOException e){ 
      System.out.println("Client: stream failed"); 
     } 
     System.out.println("Client: requesting"); 
     try{ 
      bw.write("Hi! I am Client!\n"); 
      bw.flush(); 
     } 
     catch(IOException e){ 
      System.out.println("Client: requesting failed"); 
     } 
     System.out.println("Client: requesting finished"); 
     System.out.println("Client: reading the respond"); 
     try{ 
      String line = null; 
      line =br.readLine(); 
      System.out.println("Client: server said-> "+ line); 
     } 
     catch(IOException e){ 
      System.out.println("Client: reading failed"); 
     } 
     System.out.println("Client: reading fished"); 



     System.out.println("Client: is finishing"); 
     try { 
      br.close(); 
      bw.close(); 
      skt.close(); 
     } catch (IOException e) { 
      System.out.println("Client: finishing failed"); 
     } 
     System.out.println("Client: done"); 
    } 
} 


public class Soc { 


    public static void main(String[] args) { 
     System.out.println("Main started"); 
     Thread sThread = new Thread(new sevr()); 
     Thread cThread = new Thread(new clnt()); 
     sThread.start(); 
     cThread.start(); 
     try { 
      sThread.join(); 
      cThread.join(); 
     } catch (InterruptedException ex) { 
      System.out.println("joining failed"); 
     } 
     System.out.println("Main done"); 

    } 

} 

我通過路由器連接到網絡。共有3檯筆記本電腦連接到網絡。我在eclipse上運行這個代碼。代碼執行成功,不會返回任何錯誤。但是,我如何知道我的筆記本電腦與我的筆記本電腦建立了連接?我如何確定這一點?

+2

看起來像筆記本電腦正在連接到自身,從'skt = new Socket(InetAddress.getLocalHost(),6666);' – 2011-03-22 10:31:58

+0

@Fareesh - 同意。另外2個名爲sThread和cThread的線程指的是服務器和客戶端線程。 – 2011-03-22 10:34:44

+1

每當你得到一個異常,只有在你能做一些有用的事情時才抓住它。打印出一條信息並假裝什麼都沒有出錯是沒有用的。 – 2011-03-22 10:35:36

回答

2

您不會連接到任何其他計算機。該程序在同一臺計算機上同時運行客戶端和服務器。

skt = new Socket(InetAddress.getLocalHost(),6666); 

正如你可以看到這裏的客戶端連接到本地主機上的端口6666,並且服務器偵聽端口6666連接來連接到另一臺計算機,你將需要分離出的客戶端和服務器端的代碼並在不同的機器上運行它們。然後您必須更改上面的行來創建套接字到運行服務器的機器的地址。

+0

我遵照你的指示,分離出了代碼並重寫了上面所述的代碼。我在服務器端它說得到一個錯誤 - 主開始 服務器:即將形成插座 服務器:套接字創建失敗 服務器:在線程監聽 例外「線程0」顯示java.lang.NullPointerException \t在sockettest.sevr.run(Soc.java:23) \t在java.lang.Thread.run(來源不明) 主要做 客戶端是停留在行「客戶端:讀取響應」 – KS2 2011-03-22 11:30:48

1
 skt = new Socket(InetAddress.getLocalHost(),6666); 

您可以關閉:)你從本地主機連接到本地主機的所有其他計算機。

修改客戶端代碼以連接到在另一臺計算機上運行的服務器代碼後,可以使用getRemoteSocketAddress()方法來發現遠程對等方的SocketAddress