0

I am currently developing a client-server application and I have this problem: I want to create a different class instance depending on what the connected socket sends, but it only creates the first instance then it stucks. Here is some piece of code:

    Socket clientSocket = null;
    ServerSocket server = null;
    String buff = null;
    transfer tr = null;
    colectieClienti clienti = new colectieClienti();

And:

while (true) {
            try {
                clientSocket = server.accept();
                buff = (new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))).readLine();
                if (buff.equals("client")) {
                    (colectieClienti.useri[colectieClienti.nrUseri++] = new clientThread(clientSocket, stmt)).start();
                } else {
                    tr = new transfer(clientSocket);
                    tr.start();
                }
            } catch (IOException ex) {
                System.out.println(ex);
            }
        }

I have to mention that clientThread is a class that extends Thread and communicates with a GUI, and transfer is a class that only send some files from client to server. The logic is something like this: In the GUI the user connects to the server, so it is created a new instance of clientThread and after this, when the user press a button it creates a new socket (on the client side and send a message to the server, something like "I want to create a new instance of transfer class, which is done by the buff) and receive the data. But it only creates the clientThread instance and then it stucks. Can anyone help me?

LE: This is the constructor of clientThread

 public clientThread(Socket socket, Statement statement) {
        comunicare = socket;
        try {
            oStream = comunicare.getOutputStream();
            is = new BufferedReader(new InputStreamReader(comunicare.getInputStream()));
            os = new PrintStream(oStream);
        } catch (IOException ex) {
            System.out.println(ex);
        }
        this.statement = statement;
    }
Ionut Ungureanu
  • 380
  • 3
  • 9
  • 25
  • 1
    This code is trying to connect an infinite number of times. Is that what you intended? – Andrew Thompson May 08 '11 at 13:29
  • have you tried debugging this? shouldn't `accept`ing the socket be a one-time thing? – mre May 08 '11 at 13:47
  • 1
    @sthupahsmaht: No, accepting a socket isn't a one-time thing. – Ionut Ungureanu May 08 '11 at 13:55
  • 1
    @Andrew Thompson: Yes, the server is listening for an infinite possible number of clients. The application is server - multiclient. – Ionut Ungureanu May 08 '11 at 13:57
  • What happens when you debug the server code? – Kaj May 08 '11 at 14:43
  • @Kaj: The server works perfectly (it communicates with the GUI OK), but when the GUI creates a new socket, the server accepts the connection and stucks on `buff = (new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))).readLine();` (That instruction is never done or it never reaches to this line, but `clientSocket = server.accept();` is done). – Ionut Ungureanu May 08 '11 at 14:51

2 Answers2

1

This is only a guess, but are you sending a line terminator from the client? BufferedReader.readLine() reads until it gets a \n, \r, or a \r\n, so if the client is not writing that, the server will just wait.

Ryan
  • 151
  • 1
  • On the client side I have this: `output = new PrintWriter(client2.getOutputStream()); output.println("transfer");` , so I don't think it is from "\n". – Ionut Ungureanu May 08 '11 at 15:23
0

First and foremost: avoid extending the thread class and opt out for implementing a runnable.

But it only creates the clientThread instance and then it stucks. Can anyone help me?

Then show us what you do when you create the clientThread, showing us what you do on the server (alone) is not enough for us to tell you what you're doing wrong.

Community
  • 1
  • 1
Kiril
  • 39,672
  • 31
  • 167
  • 226
  • @Ionut and where does it get stuck? – Kiril May 08 '11 at 17:31
  • @Lirik: After the second socket is created in the server side. `clientSocket = server.accept();` – Ionut Ungureanu May 08 '11 at 17:44
  • @Ionut the `accept()` call blocks until a client attempts to make a connection. How many times is your client attempting to connect? If it only connects once (which it should be, unless you get disconnected), then the server will block until it gets another connection. – Kiril May 08 '11 at 17:50
  • @Lirik: Not the `accept()` statement is the problem. But never mind ... I've tried something else. I couldn't find the solution for this problem. :( – Ionut Ungureanu May 11 '11 at 16:29
  • @Ionut I have a feeling that you're not doing something right. The accept statement is **not** the problem?!? If I undersatnd you correctly you're telling me that `server.accept()` was blocking (which is exactly what it's supposed to do until a client connects). – Kiril May 11 '11 at 16:51
  • @Lirik: No, not the `server.accept()` was blocking. Reading from the socket was blocking. – Ionut Ungureanu May 12 '11 at 16:11