日期:2014-05-20  浏览次数:20761 次

求一段代码,关于Socket连接
以下是我写的Socket连接的类
import   java.io.*;
import   java.net.*;

public   class   CallLink
{
        //使用套接字进行连接
    String   ipAddr=null;        
    Socket   outSock=null;      
    ServerSocket   inServSock=null;      
    Socket   inSock=null;      
    int   TALK_PORT=2007;
 
    CallLink(String   inIP)
    {      
          ipAddr=inIP;
    }      
    void   open()   throws   IOException,UnknownHostException      
    {//打开网路连接      
      if(ipAddr!=null)      
      outSock=new   Socket(ipAddr,TALK_PORT);      
      }      
    void   listen()   throws   IOException      
      {//监听,等候呼叫
        inServSock=new   ServerSocket(TALK_PORT);
        inServSock.setSoTimeout(1000);
        inSock=inServSock.accept();      
      }
      public   InputStream   getInputStream()throws   IOException      
      {//返回音频数据输入流      
        if(inSock!=null)      
        return   inSock.getInputStream();      
        else      
        return   null;      
        }
      public   OutputStream   getOutputStream()throws   IOException      
      {//返回音频数据输出流      
        if(outSock!=null)      
        return   outSock.getOutputStream();      
        else      
        return   null;      
      }
      void   close()   throws   IOException      
      {//关闭网络连接      
        inSock.close();      
        outSock.close();      
      }
}


问题:
在另一个类里面创建了这个类的对象curCallLink,
现在希望俩个终端用同样的代码能实现互联(即每一端都既是服务器又是客户端)
请大虾指教,小妹已经折磨了一天了!

------解决方案--------------------
客户端口和服务端口用不同的值试试outSock=new Socket(ipAddr,IN_TALK_PORT);
new ServerSocket(OUT_TALK_PORT);