0

here is the server side code of my program, the problem is, its accepting one client. when another client is connected, the isConnected method returns true, but the server does not gets the messages from the server. please help me as this is my first java program in netbeans, i have just finished studying core java.

class Conn extends Thread{
        ServerSocket ss;
        Socket s;
        public void run()
        {
            status.setText(status.getText()+"connecting");
            try{
            while(true)
            {
            s=new Socket();
            ss=new ServerSocket(3000);
            s=ss.accept();
            Read r=new Read(s);
            r.start();
            }
            }catch(Exception e){}
        }

    }
    class Read extends Thread{
        DataInputStream inp;
        PrintStream outp;
        String str;
        Read(Socket s)
        {
            try{
            inp=new DataInputStream(s.getInputStream());
            outp=new PrintStream(s.getOutputStream());
            }catch(Exception sd){}
        }
        public void run()
        {
                status.setText(status.getText()+"\nreading");
            try{
            while(true)
            {
                str=inp.readLine();
                chatwin.append(str);
                outp.println(str);
            }
            }catch(Exception er){}
        }

    }
4
  • Whats the problem. You are creating a thread to handle the incoming connection. This means you can handle multiple clients. Commented Jun 13, 2012 at 18:20
  • one threas accepts connection., other thread reads from the socket, display it and echo it back. Its working for one client well but not for other clients. Commented Jun 13, 2012 at 18:20
  • 1
    Why are you ignoring exceptions? You should never do this except for rare circumstances. Would you drive a motorcycle with a blindfold on? Commented Jun 13, 2012 at 18:22
  • The next problem you will encounter after you fix your accept loop and your non-existent exception handling is that your Read threads don't end, because you aren't testing the result of readLine() for null. When you get null you must close the socket and exit the thread. Commented Jun 14, 2012 at 1:48

2 Answers 2

4

Move the while logic after the assignment of ss.

try 
{
    ss = new ServerSocket(3000);
    while (ss.isBound())
    {
        s=ss.accept();
        Read r = new Read(s);
        r.start();
    }
}

Your problem is that you can't do this multiple times:

ss = new ServerSocket(3000);

You've already created a ServerSocket that sits at port 3000, so when you try to make another one, it'll try to bind itself to that socket, and not succeed because your first ss is still sitting there. You should only create one ServerSocket and grab socket connections off of that one ServerSocket as threads connect to it.

Does this answer your questions?

Sign up to request clarification or add additional context in comments.

6 Comments

did you get rid of your first while loop?
so what do you propose i should do? make ServerSocket global?
you should always accept the best answer that answers your question ;)
hey, another thing i want to ask is., are there separate socket for each client and server OR each client and server shares same sockets. i mean to ask is if one client writes into a socket then can the other client read from it?
@user1439164: The server uses one socket to allow it to connect to a client. I believe that when accept occurs, another socket is used by server and client and the listened to socket, the one at 3000 is back to listening for more clients. 1+ to Hans' answer. User, you should accept this answer by checking on the big check mark next to the answer.
|
0

ss.accept() will block until it receives a connection. How are you connecting to it?

3 Comments

there is another thread for Read class which reads from the socket
that reads from the accepted socket, but where are you actually connecting to the server socket? You need to connect to the server on port 3000 for it to accept a new socket.
try in the command prompt in windows running "telnet localhost 3000"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.