简体   繁体   English

客户端服务器程序未建立连接

[英]Connection not establish in for client server program

I am very new to socket programming for java. 我对java的套接字编程非常陌生。 I have develop a simple client server program which involve actionListener. 我已经开发了一个包含actionListener的简单客户端服务器程序。 The connection can't be establish once the join button being click, my client program didn't response anything to me. 一旦单击了加入按钮,就无法建立连接,我的客户端程序对我没有任何响应。 When I run my server program first, the server program response some initial message in the program to indicate that the server is starting, but when I run my client program and try to connect to the server, it will not response anything. 当我首先运行服务器程序时,服务器程序会在程序中响应一些初始消息以指示服务器正在启动,但是当我运行客户端程序并尝试连接到服务器时,它不会响应任何内容。 Beside, the program is testing using two CMD in my PC 此外,该程序正在使用我的PC中的两个CMD进行测试

I try several method such as flush(), close() and it also not working 我尝试了几种方法,例如flush(),close(),但它也无法正常工作
Simple client server program not working 简单的客户端服务器程序不起作用
This is one of my reference source for my problem 这是我的问题的参考资料之一

This is one part of my client program 这是我的客户计划的一部分

public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==btn1)
    {
       try
        {
           Socket s = new Socket("127.0.0.1",8888); //initialize the socket in client
           DataInputStream input = new DataInputStream(s.getInputStream()); // receive message from server 
           DataOutputStream output = new DataOutputStream(s.getOutputStream());  // send the message to server 
           String word = input.readUTF(); // read the input from server 
           JOptionPane.showMessageDialog(null,word);       // display the message
           output.flush();
           output.close();
           btn2.setVisible(true);
           btn3.setVisible(true);
           btn4.setVisible(true);   
        }
        catch(IOException exp)
        {
            JOptionPane.showMessageDialog(null,"Client : Can't Connect To Server, Please Try Again");
        }

    } 

This is my server program 这是我的服务器程序
http://codepad.org/AlUr9Qi1 http://codepad.org/AlUr9Qi1

The problem seems to me to be in your server code. 在我看来,问题出在您的服务器代码中。 Your server loops on accept: 您的服务器在接受时循环:

 while(true)
    {
      socket = server.accept();
    }

So you accept the socket and do nothing else, and never reach the code dealing with the socket stream. 因此,您接受套接字而不做其他任何事情,并且永远不会到达处理套接字流的代码。 You need to read/write from the socket inside that loop, possibly spanning a thread to process the socket while continuing waiting for another client. 您需要从该循环内的套接字读取/写入,可能跨越一个线程来处理套接字,同时继续等待另一个客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM