端口创建好了,可是不能通信是怎么回事。
通信我是用Socket(host,port) 方法做的。
在客户运行的时候提示“找不到服务器”(服务器端已经运行)。谁能帮我详细解释一下。谢谢!

解决方案 »

  1.   

    客户端程序:
    import java.io.*;
    import java.net.*;
    public class client {
      public static void main(String[] args)throws IOException{
         Socket kksocket=null;
         PrintWriter out=null;
         BufferedReader in=null;
         boolean runable=true;
         String toserver;     try{
           kksocket=new Socket("bemyfriend",1112) ;
           out =new PrintWriter(kksocket.getOutputStream() ,true) ;
           in=new BufferedReader(new InputStreamReader(kksocket.getInputStream() ) ) ;
         }catch (UnknownHostException e){
           System.err .println("找不到服务器") ;
           System.exit(1) ;
         }       catch(IOException e){
             System.err .println("不能获得sock的读入与写出器") ;
             System.exit(1) ;
           }
           BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in ) ) ;
           out.println("新用户登陆") ;
           RSThread rsthread=new RSThread(in) ;
           rsthread.start() ;
           while(runable){
             toserver=stdin.readLine() ;
             out.println(toserver);
             if(toserver.equals("Bye") )break;
             runable=rsthread.runable ;       }
           rsthread.fromserver="欢迎下次再来";
            rsthread.runable =false;
            out.close() ;
            in.close() ;
            stdin.close() ;
           kksocket.close() ;
         }
       }
       class RSThread extends Thread {
         BufferedReader in=null;
        String fromserver="";
        boolean runable=true;
        public RSThread(BufferedReader in){
          this.in =in;
        }
        public void run(){
          while (runable) {
            try {
              fromserver = in.readLine();        }
            catch (Exception e) {
              runable = false;
            }
            if (fromserver.equals("Bye")) {
              System.out.print("服务器端程序退出");
              runable = false;
              break;
            }
            System.out.println("服务器;" + fromserver);      }
        }
       }服务器端程序:
    import java.awt.Toolkit;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import java.awt.Dimension;
    import java.net.*;
    import java.io.*;public class server{
      public static void main(String[] args)throws IOException{
        String toclient;
        PrintWriter out=null;
        BufferedReader in=null;
        boolean runable=true;
        ServerSocket serversocket=null;
        try{
          serversocket=new ServerSocket(1112) ;
        }
        catch(IOException e){
          System.err .println(" 不能创建1111断口") ;
          System.exit(1) ;
        }
        Socket clientsocket=null;
        try{
          clientsocket=serversocket.accept() ;
        }
        catch(IOException e){
          System.err .println("访问断口失败") ;
          System.exit(1) ;
        }
        out =new PrintWriter(clientsocket.getOutputStream() ,true) ;
        in=new BufferedReader(new InputStreamReader(clientsocket.getInputStream() ) ) ;
        BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in ) ) ;
        toclient ="你好,欢迎你!";
        out.printf(toclient) ;
        RCthread rcthread=new RCthread(in) ;
            rcthread.start() ;
            while (runable){
              toclient=stdin.readLine() ;
              out.println(toclient) ;
              if(toclient.equals("Bye."))break;
              runable=rcthread.runable ;        }
            rcthread.fromclient ="欢迎下次再来";
            rcthread.runable =false;
            out.close() ;
            stdin.close() ;
            clientsocket.close() ;
            serversocket.close() ;
      }
    }
      class RCthread extends Thread {
        BufferedReader in=null;
        String fromclient="";
        boolean runable=true;
        public RCthread (BufferedReader in){
          this.in =in;    }
        public void run(){
          while(runable){
            try{
              fromclient=in.readLine() ;           }
               catch(Exception e){
                 runable=false;
               }
            if(fromclient.equals("Bye") ){
              System.out .print("客户端程序退出") ;
              runable=false;
               break;
            }
            System.out .println("客户端;"+fromclient) ;
          }
        }
      }在客户端运行结果是:
    “找不到服务器”(服务器端已经运行)。
      

  2.   

    楼主看一下java网络编程的书,有很多创建socket通信编程的,我看过你的代码,好象没问题,但是客户端连不上~
      

  3.   

    在client端开cmd窗口i,执行
    telnet bemyfriend 1112有什么反应吗?
      

  4.   

    telnet 是命令行程序,用来和远程计算机通信。
    我的意思是,你先执行这条命令看一下,是否可以访问到你程序中指定的那台计算机。
    如果输入命令后,CMD窗口变成黑屏(一般是没有任何内容),则可以说明和远程电脑的连接是正常的,问题出在程序。否则,如果屏幕提示为“无法连接……”,则说明到对方电脑的网络连接有问题,首先要排除这方面的问题。所以,你试下来的结果是?
      

  5.   

    CMD窗口打开方式:
    左下角: 点[开始] -> 点[运行] ->输入:cmd -> 按[确定]