服务器端和客户端程序在编译过程中都正确,但是客户端程序在运行时却不能正常显示
异常:java.net.ConnectException:Operation timed out:connect
这是什么原因,请各位高手给以帮助。谢了

解决方案 »

  1.   

    yuzl32(Hello!有酒醉)
    我也觉得是连接超时,那怎么解决?
    帮帮我?急需。再线等
    谢了
      

  2.   

    //Socket 客户端程序
    import java.net.*;
    import java.io.*;
    public class c14_6{
     public static void main(String[] args)
      {
      String str;
      try{
    //InetAddress addr=InetAddress.getByName("127.0.0.1");
      InetAddress addr=InetAddress.getByName("198.198.1.68");
      Socket socket=new Socket(addr,8000);
      System.out.println("Socket:"+socket);
      // 获得对应socket的输入/输出流
       InputStream fIn=socket.getInputStream();
       OutputStream fOut=socket.getOutputStream();
      //  建立数据流
       InputStreamReader isr=new InputStreamReader(fIn);
       BufferedReader in=new BufferedReader(isr);
       PrintStream out=new PrintStream(fOut);
       InputStreamReader userisr=new InputStreamReader(System.in);
       BufferedReader userin=new BufferedReader(userisr);
       while(true){
     System.out.print("发送字符串:");
     str=userin.readLine();  //读取用户输入的字符串
     out.println(str);  //将字符串传给服务器端
     if(str.equals("end"))break;  //如果是end则退出
           System.out.println("等待服务器端消息...");
       str=in.readLine();  //获取服务器获得字符串
       System.out.println("服务器端字符:"+str);
       if(str.equals("end"))break;
          }
        socket.close();  //关闭连接
      }
     catch(Exception e){
     System.out.println("异常:"+e);}
        }
      }
       //Socke 服务器端程序 
    import java.net.*;
    import java.io.*;
    public class c14_5{
    public static final int port=8000;
        public static void main(String args[])
    {
          String str;
      try{   //在端口port注册服务
          ServerSocket server=new ServerSocket(port);  //创建当前线程的监听对象
      System.out.println("Started: "+server);
      Socket socket=server.accept();  //负责C/S通信的Socket对象
      System.out.println("Socket: "+socket);
      //获得对应Socket的输入/输出流
      InputStream fIn=socket.getInputStream();
      OutputStream fOut=socket.getOutputStream();
      //建立数据流
      InputStreamReader isr=new InputStreamReader(fIn);
      BufferedReader in=new BufferedReader(isr);
      PrintStream out=new PrintStream(fOut);
      InputStreamReader userisr=new InputStreamReader(System.in);
      BufferedReader userin=new BufferedReader(userisr);
      while(true){
      System.out.println("等待客户端的消息");
      str=in.readLine();  //读客户端传送的字符串
      System.out.println("客户端:"+str);  //显示字符串/                                                                 
      if(str.equals("end"))break;  //如果是end,则退出
      System.out.print("给客户端发送:");
      str=userin.readLine();
      out.println(str);   //向客户端发送消息
      if(str.equals("end"))break;
      }//while
      socket.close();
      server.close();
      }//try
      catch(Exception e){
      System.out.println("异常:"+e);}
     }
    }
    随便问一下,怎么ping呀
      

  3.   

    另,服务器与客户端是一个交互过程,就算建立连接成功,程序也会在
     str=in.readLine();  //读客户端传送的字符串
    一行时阻塞,因为这时不论是服务端还是客户端都等待网络数据,但是两端都没有向流中写数据
    所以同时阻塞了,正确流程是,一端写,一端读互相交替
      

  4.   

    yanhan0615(炮炮) 说的对,楼主的代码里面客户端与服务端都是在监听等待着从流里面读数据,但是却没有往里面写数据,所以你等我,我等你,结果就是死锁了。等待一定时间超过默认的等待时间后没有响应,连接就超时断掉了。
      

  5.   

    TOboltzjf(Bolt(晶峰))你的意思是说,我的程序有问题?
    这是书上的一个程序
    服务器端运行结果如下:
    Started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8000]
    Socket:  Socket[addr=PU67/198.198.1.67,port=1065,localport=8000]
    等待客户端的消息...
    客户端:How do you do!
    给客户端发送:Fine,thanks.
    等待客户端的消息...
    客户端:How is Zhang.
    给客户端发送:She's very well.
    等待客户端的消息...
    客户端:Good_bye.
    给客户端发送:Good_bye.
    等待客户端的消息...
    客户端:end
    客户端运行结果如下:
    Socket:  Socket[addr=PU68/198.198.1.68,port=8000,localport=1065]
    发送字符串:How do you do!
    等待服务器端的消息....
    服务器端字符:Fine,thanks.
    发送字符串:How is Zhang.
    等待服务器端的消息....
    服务器端字符:She's very well.
    发送字符串:Good_bye.
    等待服务器端的消息...
    服务器端字符:Good_bye.
    发送字符串:end编译时没有错误,在运行时,先运行服务器端程序,出现了第一行的Started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8000]接下来运行服务器端程序:却出现 异常:java.net.ConnectException:Operation timed out:connect。理论上应该出现Socket:  Socket[addr=PU68/198.198.1.68,port=8000,localport=1065]
    我是初学不怎么懂,请高手给以指导,谢了。
      

  6.   

    我试过了,程序可以正常运行。为了在自己机子上运行,修改了其中一句InetAddress addr=InetAddress.getByName("198.198.1.68");改为InetAddress addr=InetAddress.getLocalHost();
    先启动服务器端,在运行客户端,结果如下:
    服务器端
    Started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8000]
    Socket: Socket[addr=/192.168.0.103,port=3696,localport=8000]
    等待客户端的消息
    客户端:hahha
    给客户端发送:xixiixixi
    等待客户端的消息
    客户端:ajsdlfjlasdjf
    给客户端发送:asdfasdfa
    等待客户端的消息
    客户端:asdfasdfa
    给客户端发送:客户端:
    Socket:Socket[addr=seuyoung/192.168.0.103,port=8000,localport=3696]
    发送字符串:hahha
    等待服务器端消息...
    服务器端字符:xixiixixi
    发送字符串:ajsdlfjlasdjf
    等待服务器端消息...
    服务器端字符:asdfasdfa
    发送字符串:asdfasdfa
    等待服务器端消息...
      

  7.   

    你真的可以运行吗??不知道为什么我的只出来:Started:   ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8000],,就没有下文了??