fweigl
fweigl

Reputation: 22038

Java server- client socket communication

I'm trying to let a (android) client communicate with a server. Sending data client -> server works fine, but I want the server to respond. Client side code is:

try {
                Socket s = new Socket("192.168.0.36", 12390);
                s.setSoTimeout(5000);

                JSONObject json = new JSONObject();
                json.put("emergency", false);
                json.put("imei", imei);
                json.put("lat", l.getLatitude());
                json.put("lon", l.getLongitude());
                json.put("acc", l.getAccuracy());
                json.put("time", l.getTime());


                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                s.getOutputStream()));
                out.write(json.toString());
                out.newLine();
                out.write("###");
                out.flush();
                Log.d(TAG, "sent");


                String inputLine = null;
                String result = "";
                BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));  

                Log.d(TAG, "open input");
                while ((inputLine = in.readLine()) != null) {
                    Log.d(TAG, "while");
                    if (inputLine.contains("###")) {
                        break;
                    }
                    result = result.concat(inputLine);

                }

                Log.d(TAG, "closing socket");
                s.close();

Server side is:

Socket c = null;
    while (true) {
        try {
            c = s.accept();
        } catch (IOException e) {
            System.out.println("Accept failed: " + port);
            System.exit(-1);
        }
        try {

            BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(c.getOutputStream()));



            String inputLine = null;
            String result = "";
            while ((inputLine = in.readLine()) != null) {

                if (inputLine.contains("###")) {
                    System.out.println("received ###");
                        out.write("Hello phone");
                        out.newLine();
                        out.write("###");
                        out.newLine();
                        out.flush();

                }

                result = result.concat(inputLine);  


            }
            System.out.println(result);

            out.close();

The server reads the message from the client correctly and after receiving ### should be sending back a message, but that is never received by the client. The client times out on

                    while ((inputLine = in.readLine()) != null) {
                    Log.d(TAG, "while");
                    if (inputLine.contains("###")) {
                        break;
                    }

and never enters the while loop. What am I missing here? Any ideas appreciated!

Upvotes: 1

Views: 3202

Answers (2)

ductran
ductran

Reputation: 10203

From the client:

out.write(json.toString());
out.newLine();
out.write("###");
out.flush();

You forgot adding a new line after send "###", because your server use readLine()

 while ((inputLine = in.readLine()) != null) {
 //...
 }

So, I believe your server can't receive this message. Try to put

   out.newLine();

after sending "###".

Upvotes: 2

Germann Arlington
Germann Arlington

Reputation: 3353

I may be very wrong here but in my experience I always open both in and out streams before I start the comms. Try moving your

BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));  

right next to

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

just in case your packets expire before you open your input stream and start reading them.

Upvotes: 0

Related Questions