0

There are question with the same problem but they are not helping me out.

Below is the code I've been working on where the ServerSocket not receiving any message from client Socket.

Server

package rescue_server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.TreeMap;

/**
 *
 * @author Mohammad Faisal
 */
public class RServer {

    private TreeMap<String, PrintWriter> clients;
    private static final int PORT = 6621;

    private class ClientHandler implements Runnable {

        private PrintWriter writer;
        private BufferedReader reader;

        public ClientHandler(Socket client) {
            try {
                writer = new PrintWriter(client.getOutputStream());
                reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }

        @Override
        public void run() {
            System.out.println("client handler thread started");
            String msg;
            try {
                while ((msg = reader.readLine()) != null) {
                    System.out.println(msg);
                }
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

    public RServer() {
        clients = new TreeMap<>();
    }

    public void start() {
        try {
            ServerSocket server = new ServerSocket(RServer.PORT);
            while (true) {
                System.out.println("waiting....");
                Socket client = server.accept();
                Thread tclient = new Thread(new ClientHandler(client));
                tclient.setDaemon(true);
                tclient.start();
                System.out.println("got a connection.");
            }
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public static void main(String[] args) {
        new RServer().start();
    }
}

Client

package rescue_client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 * @author Mohammad Faisal
 */
public class RClient {

    private final static int SERVER_PORT = 6621;
    private final static String SERVER_IP = "127.0.0.1";
    private Socket socket;
    private PrintWriter writer;
    private BufferedReader reader;

    private class ServerHandler implements Runnable {

        @Override
        public void run() {
            System.out.println("server handler started.");
            String msg;
            try {
                while ((msg = reader.readLine()) != null) {
                    System.out.println(msg);
                }
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

    public RClient() {
        JFrame frame = new JFrame(SERVER_IP);
        frame.setVisible(true);
        frame.setBounds(10, 20, 100, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void startServerHandler() {
        Thread tserver = new Thread(new ServerHandler());
        tserver.setDaemon(true);
        tserver.start();
        sendWelcomeMsg();
    }

    private void sendWelcomeMsg() {
        try {
            writer.write(InetAddress.getLocalHost().getHostName());
            writer.flush();
        } catch (UnknownHostException ex) {
            System.out.println(ex.getMessage());
        }
        writer.flush();
    }

    public void start() {
        System.out.println("starting client...");
        try {
            socket = new Socket(RClient.SERVER_IP, RClient.SERVER_PORT);
            writer = new PrintWriter(socket.getOutputStream());
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            startServerHandler();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public static void main(String[] args) {
        new RClient().start();
    }
}

The problem is I'm not getting the message on server from client.

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
  • Maybe you are not sending an actual line? The server is waiting for a line - something more than just some bytes. – Martin James Sep 21 '14 at 12:28
  • @MartinJames: in the client `sendWelcomeMsg()` I'm writing it to server – Mohammad Faisal Sep 21 '14 at 12:31
  • 2
    Add a newline (`"\n"`) to the String being sent. – Fildor Sep 21 '14 at 12:34
  • @Fildor: thanks! it works. Why its not being sent without `new line`? – Mohammad Faisal Sep 21 '14 at 12:37
  • `readLine` waits for a `\n` to know that a line is complete and return the result. So, no newline - no complete line - readline waits ... Not a very technical explanation, but basically, that's what happens. – Fildor Sep 21 '14 at 12:38
  • possible duplicate of [Socket comunication, Java client C server](http://stackoverflow.com/questions/19561941/socket-comunication-java-client-c-server) – alk Sep 21 '14 at 13:19
  • @MohammadFaisal 'Why its not being sent without new line' lol - because you did not tell it to send one, (which is what I was hinting at)! – Martin James Sep 21 '14 at 23:02
  • @Martin I'd also written a program in which the input from the swing text field is sent to the server. And in that case I do not need to add the new line character. – Mohammad Faisal Sep 22 '14 at 01:00

1 Answers1

1

From JavaDoc:

A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

So you have to add one of these in your String that is being sent.

Fildor
  • 14,510
  • 4
  • 35
  • 67