1

As you can see from the Client class, if the while breaks then the (Client) system prints a message, this message is never printed.

The only thing the Server prints is "fromClient = CLIENT: I was just told the following by you...: Server: you are connected, your ID is 1"

(see the try block in 'ThreadWorker')

Yet the Server doesn't display the "understood" message (which should be sent as a response to "101" being sent to the client and the client responding with "understood") and it also doesn't display the repeatedly written Client's response which should include an incremented 'num' value on a loop.

This tells me something is failing in either the threads or the sockets but I'm learning it atm and I really hope it's just something obvious I'm missing but I've spent about two days trying different things, some help, some make it worse.

Now I know posting loads of code isn't that good sometimes but I think I need to here. Sorry if I don't.

Ideas? Thanks :)

(BTW, sorry if you've already read this, this is a bit of a trainwreck of a post, hopefully I've sorted the previous problems)

CLIENT

import java.io.*;
import java.net.*;

public class Client 
{
    public static void main(String[] args) throws IOException 
    {
        Socket clientSocket = null;
        DataOutputStream os = null;    
        BufferedReader is = null;
        String fromServer = null;

        try 
        {
            clientSocket = new Socket("localhost", 4444);
            os = new DataOutputStream(clientSocket.getOutputStream());
            is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        } 
        catch (UnknownHostException e) 
        {
            System.err.println("Don't know about host: localhost.");
            System.exit(1);
        } 
        catch (IOException e) 
        {
            System.err.println("Couldn't get I/O for the connection to: localhost.");
            System.exit(1);
        }    

        while(clientSocket != null && is != null && os != null)
        {
            fromServer = is.readLine();
                if(fromServer.equals("101"))
                {
                    os.writeBytes("Understood" + "\n");
                }
                else
                {
                    os.writeBytes("CLIENT: I was just told the following by you...: " + fromServer + "\n");
                }
        }
        System.out.println("If you're reading this then the while loop (line 30 - 48) has broken");
        os.close();
        is.close();
        clientSocket.close();
    }
}

SERVER

import java.io.*;
import java.net.*;

public class Server
{
    ServerSocket server = null; 
    Socket service = null;

    public Server()
    {       
        try
        {
            server = new ServerSocket(4444);
        }
        catch(IOException e)
        {
            System.err.println(e + "Could not listen on port 4444");
            System.exit(1);
        }

        System.out.println("Listening for clients on 4444");

        int id = 1;
        boolean b = true;
        while(b = true)
        {
            try
            {
                service = server.accept();

                ThreadWorker tW = new ThreadWorker(service, id);
                new Thread(tW).start();
            }
            catch(IOException e)
            {
                System.err.println("Exception encountered on accept. Ignoring. Stack Trace: ");

                e.printStackTrace();
                System.exit(1);
            }
            id++;
        }
    }

    public static void main(String args[]) throws IOException 
    {
        Server s = new Server();
    }
    }

THREADWORKER

import java.io.*;
import java.net.*;
public class ThreadWorker implements Runnable
{
    protected Socket clientSocket = null;
    protected int id;
    boolean running = true;
    DataOutputStream os = null;
    BufferedReader is = null;
    String fromClient;

    public ThreadWorker(Socket service, int i)
    {
        clientSocket = service;
        id = i;
    }

    public void run()
    {
        try
        {
            os = new DataOutputStream(clientSocket.getOutputStream());
            is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            os.writeBytes("Server: you are connected, your ID is " + id + "\n");
            fromClient = is.readLine();
            System.out.println("fromClient = " + fromClient);
            os.writeBytes("101");
            fromClient = is.readLine();
            System.out.println("fromClient = " + fromClient);
            if(fromClient.equals("Understood"))
            {
                System.out.println("please work(line 35 ThreadWorker)");
                while(is != null && os != null && clientSocket != null)//running)
                {
                    int num = 1;
                    os.writeBytes("Hello client, here is a number:" + num + " from thread: " + "\n");
                    fromClient = is.readLine();
                    System.out.println("'Hello client, here is a number: " + num + "' written to connected client with id of " + id + " (line 36 ThreadWorker)");
                    System.out.println(fromClient);
                    num++;
                }
            }
        }
        catch(IOException e)
        {
            System.err.println("Error line 38: " + e);
        }
    }
}
6
  • 1
    Is readLine() waiting for an end-of-line character? If so, sending "101" will not cause it to return. Commented Apr 7, 2012 at 9:42
  • it could be, but from my experience the writeBytes requires "\n" and then the receiver of that message through the socket doesn't usually require that to explicitly be received. Perhaps I misunderstand your point? Commented Apr 7, 2012 at 9:45
  • 1
    If that is not the problem, you could always resort to debugging it. The server must have got into ThreadWorker else it could not have printed anything. How far does it get? What gets stuck on what, exactly, on both the client and server? Commented Apr 7, 2012 at 9:46
  • Ok I'll try that suggestion (similar to what pimaster has suggested), thanks a lot. Commented Apr 7, 2012 at 9:48
  • legend, thanks a lot to youand pimaster for that, new it had to be something nubish I missed (hence the name) Commented Apr 7, 2012 at 9:50

1 Answer 1

3

Debugging this, looks like the offending code is os.writeBytes("101");. You need to send back os.writeBytes("101\n"); or the readLine() on the other side won't continue.

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

3 Comments

Thanks, Martin James suggested something similar, I'll give that a try. Thanks a lot.
legend, thanks a lot to youand Martin James for that, new it had to be something nubish I missed (hence the name)
oh, that probably reads sarcasticly, my name was originally "another_nub" , man this is truly a trainwreck of a question. Thanks again pimaster :)

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.