简体   繁体   English

Android TCP套接字客户端/服务器实现

[英]Android TCP socket client/server implementation

I'm new to Java and Android, I'm trying to create a server/client application. 我是Java和Android的新手,我正在尝试创建一个服务器/客户端应用程序。 For the moment I'm running the server on the PC and the client is on an Android device. 目前我在PC上运行服务器,客户端在Android设备上。 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! 例如,当我从客户端发送“姓名”时,服务应回答“Matteo”,但它总是回答“其他”,我不明白为什么! I imagine that the problem is in the statement if (dataInputStream.equals("Name")) { 我想问题是在语句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();
     }
    }
   }
  }
 }
}

DataInputStream represents the handle to stream of data coming from the socket, but not the data itself. DataInputStream表示来自套接字的数据流的句柄,但不表示数据本身。 So .equals() compares it with another DataInputStream not with a String . 所以.equals()将它与另一个没有String DataInputStream进行比较。 You are comparing it directly with a string, which obviously returns false . 您将它直接与字符串进行比较,显然返回false

Just like you are writing data with writeUTF() , you need to read data from the socket connection through that DataInputStream . 就像使用writeUTF()编写数据一样,您需要通过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: 使用此处描述的read()方法之一,或者如果您知道客户端将发送整行,则可以将其包装在BufferedReader并使用以下命令:

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() 另一方面,在Java 7中,您也可以在String上使用switch语句,而不是.equals()

The trick is that you should read something from the DataInputStream . 诀窍是你应该从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, 干杯,

Maybe look into an ObjectInputStream you can then read the object and use the: 也许看一下ObjectInputStream然后你可以读取对象并使用:

instanceof

keyword to differntiate between objects 用于区分对象的关键字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM