2

I'm trying to write a little SocketServer and a fitting ClientApplet. The connection works (I echo out incoming/closing connections), but the server does not get any InputStream. I just can't fix the problem and feel a bit lost :/

The complete project is here.

Here is the responsible part of my server:

MessageService.java

public class MessageService implements Runnable {

private final Socket client;
private final ServerSocket serverSocket;

MessageService(ServerSocket serverSocket, Socket client) {
    this.client = client;
    this.serverSocket = serverSocket;
}

@Override
public void run() {
    PrintWriter out = null;
    BufferedReader in = null;
    String clientName = client.getInetAddress().toString();
    try {
        out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
        in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String line;
        System.out.println("Waiting for "+clientName);

                    /* HERE I TRY TO GET THE STREAM */

        while((line = in.readLine()) != null) {
            System.out.println(clientName + ": " + line);
            out.println(line);
            out.flush();
        }
    }
    catch (IOException e) {
        System.out.println("Server/MessageService: IOException");
    }
    finally {
        if(!client.isClosed()) {
            System.out.println("Server: Client disconnected");
            try {
                client.close();
            }
            catch (IOException e) {}
        }
    }
}
}

Part of Client

QueueOut.java

public class QueueOut extends Thread {
Socket socket;
public ConcurrentLinkedQueue<String> queue;
PrintWriter out;

public QueueOut(Socket socket) {
    super();
    this.socket = socket;
    this.queue = new ConcurrentLinkedQueue<String>();
    System.out.print("OutputQueue started");
}

@Override
public void start() {
    try {
        out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
        System.out.println("Running outputqueue");
        while(true) {
            if(this.queue.size() > 0) {
                String message = this.queue.poll();
                System.out.println("Sending "+message);
                out.println(message+"\n");
            }
        }
    }
    catch (IOException ex) {
        System.out.println("Outputqueue: IOException");
    }
}

public synchronized void add(String msg) {
    this.queue.add(msg);
}
}

I have reduced my post to the (as i think) necessary parts :)

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
Daniel
  • 601
  • 1
  • 4
  • 13
  • Thanks, i will cut a few things .. – Daniel May 20 '12 at 15:12
  • Solved: forgot to flush the outputstream in the client. Nothing more to say .. – Daniel May 20 '12 at 16:13
  • Very good but next time please don't post vague statements like 'the server does not get any InputStream'. Post the actual exception, message, and stack trace, via cut and paste. Not whatever you think is a good enough approximation. It isn't. Otherwise you will only get a complaint and a request of this form. It also saves you time. – user207421 May 21 '12 at 01:54

1 Answers1

0

Try getting your input stream before you get the output stream, even though you're not using it, you should match the inverse order on your client and your server (as discussed in another similar threads).

Edit:

Also see Socket programming

Good Luck!

Community
  • 1
  • 1
  • Thanks, I tried this. But no difference. The server still gets no stream. – Daniel May 20 '12 at 15:34
  • In my case the input and output stream of the client start in separate threads each. So there should be no "lock" because of waiting for handshakes. But also in inverse order client/server there is no stream. – Daniel May 20 '12 at 15:40
  • What happens if you convert your applet into a standard client? Maybe you're facing the security constraints of the applet. – Juan Alberto López Cavallotti May 20 '12 at 17:20