TCP Server-Client Socket Communications

  • Replies:1
xortan none
  • Forum posts: 1

Mar 11, 2014, 12:31:22 AM via Website

Hello everybody, new to android and new to this forum :)

Anyways I am trying to write a server-client application. The client will send data to the server and the server will sort and store accordingly. I haven't started working on the sorting part of the code yet. I have the client establishing a connection with the server and I want the server to send a reply back to the client letting them know the message was received, the server should also be displaying the incoming message onto the screen.

Where I am having problems is I can see in the Log that I am getting a response every time I send data to the server. However the message on the server side is not updating on the screen and anything other than the first transmission is returning way more than the first one did. Here is some relevant sections from the code.

Client: This is the code for the onClickListener for the sending button. I think this part is fine...
1// Listener for the Send Data button.
2 public void sendData(View view){
3 tv = (TextView) findViewById(R.id.textView1);
4
5 if (response_count > 0) {
6 try {
7 // Get data from spinners and save to file
8 getSpinner(); // Get the user selected data
9 readFile(FILE); // Read data from file
10
11 new Thread(new Runnable() {
12 public void run() {
13 try {
14 dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); // Output stream -> to server
15 dis = new DataInputStream(new BufferedInputStream(socket.getInputStream())); // Input stream <- from server
16 serverResponse = (TextView) findViewById(R.id.textView2); // Text view to see response
17
18 dos.writeUTF(myData); // Send data to server
19 dos.flush(); // Flush output stream
20
21 // Create variable to receive incoming response from the server
22 final byte[] inputData = new byte[1024];
23 dis.read(inputData);
24
25 // Runnable thread that will listen for the response from the server
26 Runnable thRead = new Runnable() {
27 public void run() {
28 String inputString = new String(inputData).trim();
29 //String inputString = dis.readUTF().toString();
30 Log.e("ERROR", inputString);
31 serverResponse.setText(inputString);
32 }
33 };
34 runOnUiThread(thRead); // Start the waiting thread
35 } catch (IOException e) {
36 // TODO Auto-generated catch block
37 e.printStackTrace();
38 }
39 }}).start();
40 }
41 catch (Exception e){
42 e.printStackTrace();
43 Log.e("ERROR", "Unable to send data");
44 }
45 tv.setText("Number of Responses Left: " + response_count);
46
47 } else {
48 tv.setText("Out of transmission attempts");
49
50 }

Server: This is a while loop that exists inside my Runnable thread, this thread is started in the onCreate method. The first transmission will display the message and the Log shows only one "response", the 2nd transmission will not update the message and will send back "responseresponseresponse..."
1while (true) {
2 // LISTEN FOR INCOMING CLIENTS
3 Socket client = serverSocket.accept();
4 handler.post(new Runnable() {
5 @Override
6 public void run() {
7 serverStatus.setText("Connected.");
8 }
9 });
10
11 try {
12 in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
13 out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
14
15 message = in.readUTF().toString();
16
17 // Read the message sent and store it to "line"
18 String line = in.readUTF().toString();
19
20 while (!Thread.currentThread().isInterrupted()) {
21 Log.d("ServerActivity", line);
22 handler.post(new Runnable() {
23 @Override
24 public void run() {
25 // Print the received message, should call methods to organize the string here and then store in database
26 serverStatus.setText("message received: " + message);
27
28 // Send ACK back to client
29 String response = "response";
30 try {
31 out.write(response.getBytes());
32 out.flush();
33 } catch (IOException e) {
34 Log.e("ERROR", response);
35 e.printStackTrace();
36 }
37 }
38 });
39 } // end while

Any kind of help on this would be very much appreciated, if you need to see more code just let me know please and thank-you.

Reply