0

服务器

    public class Server {

class ServerHelper implements Runnable
    {
        InputStream is;
        private InputStreamReader isr;
        private BufferedReader br;

        public ServerHelper(InputStream is) {
            this.is  = is;
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
        }



         private void display() throws IOException {

            String s = "";
            System.out.print("client says : ");
            while ( ( s = br.readLine() ) != null ) {                
                System.out.print(s + " ");
            }
        }

        @Override
        public void run() {
            try {
                display( );
            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        void start( ) throws Exception{
          ServerSocket ss = new ServerSocket(5555);

             while (true) {            
                System.out.println("waiting for conn..");
              Socket accept =   ss.accept();//code hangs over here and doesn't proceed ahead

                 if( accept == null )
                     System.out.println("got null...");
                 System.out.println("got the client req...");
                 ServerHelper sh = new ServerHelper(accept.getInputStream());
                 Thread t = new Thread(sh);
                 t.start();
             }

        }

         public static void main(String[] args) {
             try {
                // TODO code application logic here
                new Server().start();
             } catch (Exception ex) {
                 Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
         }

客户

public class Client {

void start( ) throws Exception{
    System.out.println("enter window size ");
    Scanner sc = new Scanner(System.in);
    int wsize = sc.nextInt();
    Socket s = new Socket("127.0.0.1", 5555);
    System.out.println("is connected .." + s.isConnected());

    OutputStream outputStream = s.getOutputStream();
    PrintWriter pw = new PrintWriter(outputStream);
    String c  = "y";
    int j = 0;
    do{
        String se = "";
        for (int i = 0; i < wsize; i++) {
            j++;
            se = se + String.valueOf(j);

        }
        pw.println(se);
        pw.flush();

        System.out.println("do u wanna send more....?(y|n)");
        c =  sc.next();
    }while( c.equalsIgnoreCase("y") );

}
public static void main(String[] args) {
    try {
        // TODO code application logic here
        new Client().start();
    } catch (Exception ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

Socket accept =   ss.accept();

在这里我的代码挂断了我知道它正在阻止 io,但是在客户端我确实验证了客户端是否已连接但它显示已连接...accept() 有什么问题?我为我所有的 TCP 应用程序都以类似的方式编码,但这很奇怪..任何人都可以帮忙

我还为那些想看看那个类的人添加了 ServerHelper 代码。

4

3 回答 3

0

If the call to accept is really blocking, then you have another program running that is listening on 5555 and is bound to "127.0.0.1". Your client is connecting to this other program, so ss.accept still blocks.

The solution is to find and kill this other process

Additional information:

Since you did not specify the bind address in your server socket, it is bound to "0.0.0.0" (all interfaces).

It is possible to have one process bind to 127.0.01 and another to bind to 0.0.0.0 (at least on Windows). I copied some data from tcpview. The second column is process id.

java.exe    5944    TCP 0.0.0.0 5555    0.0.0.0 0   LISTENING                                       
java.exe    5944    TCPV6   [0:0:0:0:0:0:0:0]   5555    [0:0:0:0:0:0:0:0]   0   LISTENING                                       
java.exe    5608    TCP 127.0.0.1   5555    0.0.0.0 0   LISTENING       

I can connect to the process 5944 using telnet 10.101.16.28 5555 (my local IP address) or connect to process 5608 using telnet 127.0.0.1 5555

于 2013-09-09T18:50:47.427 回答
0

当然会阻塞。它正在等待连接。这就是它应该做的。顺便说一句,检查 accept() 的结果是否为空是没有意义的。它不会为空。可能这是您的误解:如果没有传入连接,您是否希望它返回 null?它不这样做。请参阅 Javadoc。

于 2013-09-09T21:47:41.157 回答
0

看看你的代码:

while (true) {            
      System.out.println("waiting for conn..");
      Socket accept =   ss.accept();
      .................
}

你调用ss.accept()无限循环。连接您的客户端后,将accept()返回该Socket实例。然后将此套接字传递给在其自己的线程中运行的服务器助手并返回到ss.accept(),因此您的服务器已准备好接受其他客户端。您的代码看起来不错。不幸的是,您没有发送您的代码,ServerHelper也没有解释您的客户端是否成功将数据发送到服务器,但是到目前为止一切看起来都很好。

于 2013-09-09T18:28:00.853 回答