phcaze
phcaze

Reputation: 1777

Android TCP socket client/server implementation

I'm new to Java and Android, I'm trying to create a server/client application. For the moment I'm running the server on the PC and the client is on an Android device. The communication takes place and all works fine, but I want to differentiate incoming messages from the client in order to do different actions on the server. Here's the code for the server. The client is pretty easy and works well. For example when I send "Name" from the client, the serve should answer with "Matteo" but it always answers with "Something else" and I can't understand why! I imagine that the problem is in the statement if (dataInputStream.equals("Name")) {

Thank you.

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class Server1 {

 public static void main(String[] args){
  ServerSocket serverSocket = null;
  Socket socket = null;
  DataInputStream dataInputStream = null;
  DataOutputStream dataOutputStream = null;

  try {
   serverSocket = new ServerSocket(8888);
   System.out.println("Listening on port 8888");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  while(true){
   try {
    socket = serverSocket.accept();
    dataInputStream = new DataInputStream(socket.getInputStream());
    dataOutputStream = new DataOutputStream(socket.getOutputStream());
    System.out.println(socket.getInetAddress() + " : " + dataInputStream.readUTF());

    if (dataInputStream.equals("Name")) {
        dataOutputStream.writeUTF("Matteo!");

    }
    else if (dataInputStream.equals("Surname")) {
        dataOutputStream.writeUTF("Rossi!");

    }
    else {
        dataOutputStream.writeUTF("Something else!");

    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   finally{
    if( socket!= null){
     try {
      socket.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }

    if( dataInputStream!= null){
     try {
      dataInputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }

    if( dataOutputStream!= null){
     try {
      dataOutputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   }
  }
 }
}

Upvotes: 3

Views: 4456

Answers (3)

Jake B
Jake B

Reputation: 682

Maybe look into an ObjectInputStream you can then read the object and use the:

instanceof

keyword to differntiate between objects

Upvotes: 1

jbx
jbx

Reputation: 22158

DataInputStream represents the handle to stream of data coming from the socket, but not the data itself. So .equals() compares it with another DataInputStream not with a String. You are comparing it directly with a string, which obviously returns false.

Just like you are writing data with writeUTF(), you need to read data from the socket connection through that DataInputStream. Use one of the read() methods described here or if you know that your client will send whole lines, you can wrap it in a BufferedReader and use the following:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
String input = bufferedReader.readLine();
if (input.equals("Name")
{
  dataOutputStream.writeUTF("Matteo!");
}
else ...

On another note, in Java 7, you can use switch statements on String too, instead of .equals()

Upvotes: 3

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16060

The trick is that you should read something from the DataInputStream. Eg.

byte[] buffer = new byte[ 1024 ];
dataInputStream.read( buffer );
if ( new String( buffer ).equals( "Name" ) ) { /*  do your stuff */ }    

This is much abbreviated, but you could work from there.

Cheers,

Upvotes: 2

Related Questions