Shinji Kagawa
Shinji Kagawa

Reputation: 3

Connection not establish in for client server program

I am very new to socket programming for java. I have develop a simple client server program which involve 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

I try several method such as flush(), close() and it also not working
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

Upvotes: 0

Views: 118

Answers (1)

JP Moresmau
JP Moresmau

Reputation: 7403

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.

Upvotes: 1

Related Questions