2011-03-22 48 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