使用套接字发送和接收数据 [英] Using Sockets to send and receive data

查看:34
本文介绍了使用套接字发送和接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用套接字连接我的 Android 应用程序(客户端)和 Java 后端服务器.每次与服务器通信时,我想从客户端发送两个数据变量.

I am using sockets to connect my Android application (client) and a Java backend Server. From the client I would like to send two variables of data each time I communicate with the server.

1) 某种消息(由用户通过界面定义)

1) Some kind of message (Defined by the user through the interface)

2) 消息的语言(由用户通过界面定义)

2) The language of the message (Defined by the user through the interface)

我如何发送这些以便服务器将每个解释为一个单独的实体?

How could I send these so that the server interprets each as a separate entity?

在服务器端读取数据并做出适当的结论后,我想向客户端返回一条消息.(我想我会接受这个)

Having read the data on the server side and made an appropriate conclusion, I would like to return a single message to the client. (I think I will be okay with this)

所以我的两个问题是如何确定正在发送的两个字符串(客户端到服务器)在客户端是唯一的,以及如何在服务器端分离这两个字符串.(我正在考虑一个字符串数组,但无法确定这是否可能或合适.)

So my two questions are how can I establish that the two Strings being sent (client to server) are unique on the client side and how can I separate these two Strings on the server side. (I was thinking an array of Strings, but could not establish if this was possible or appropriate.)

我打算发布一些代码,但我不确定这会有什么帮助.

I was going to post some code but I'm not sure how that would help.

推荐答案

我假设您使用 TCP 套接字进行客户端-服务器交互?将不同类型的数据发送到服务器并使其能够区分两者的一种方法是将第一个字节(如果您有超过 256 种类型的消息,则更多)作为某种标识符.如果第一个字节为 1,则为消息 A,如果为 2,则为消息 B.通过套接字发送此消息的一种简单方法是使用 DataOutputStream/DataInputStream:

I assume you are using TCP sockets for the client-server interaction? One way to send different types of data to the server and have it be able to differentiate between the two is to dedicate the first byte (or more if you have more than 256 types of messages) as some kind of identifier. If the first byte is one, then it is message A, if its 2, then its message B. One easy way to send this over the socket is to use DataOutputStream/DataInputStream:

客户:

Socket socket = ...; // Create and connect the socket
DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());

// Send first message
dOut.writeByte(1);
dOut.writeUTF("This is the first type of message.");
dOut.flush(); // Send off the data

// Send the second message
dOut.writeByte(2);
dOut.writeUTF("This is the second type of message.");
dOut.flush(); // Send off the data

// Send the third message
dOut.writeByte(3);
dOut.writeUTF("This is the third type of message (Part 1).");
dOut.writeUTF("This is the third type of message (Part 2).");
dOut.flush(); // Send off the data

// Send the exit message
dOut.writeByte(-1);
dOut.flush();

dOut.close();

服务器:

Socket socket = ... // Set up receive socket
DataInputStream dIn = new DataInputStream(socket.getInputStream());

boolean done = false;
while(!done) {
  byte messageType = dIn.readByte();

  switch(messageType)
  {
  case 1: // Type A
    System.out.println("Message A: " + dIn.readUTF());
    break;
  case 2: // Type B
    System.out.println("Message B: " + dIn.readUTF());
    break;
  case 3: // Type C
    System.out.println("Message C [1]: " + dIn.readUTF());
    System.out.println("Message C [2]: " + dIn.readUTF());
    break;
  default:
    done = true;
  }
}

dIn.close();

显然,您可以发送各种数据,而不仅仅是字节和字符串 (UTF).

Obviously, you can send all kinds of data, not just bytes and strings (UTF).

请注意,writeUTF 编写了一种修改后的 UTF-8 格式,前面是一个无符号的两字节编码整数的长度指示符,为您提供 2^16 - 1 = 65535 字节发送.这使得 readUTF 可以找到编码字符串的结尾.如果您决定自己的记录结构,那么您应该确保记录的结尾和类型是已知的或可检测的.

Note that writeUTF writes a modified UTF-8 format, preceded by a length indicator of an unsigned two byte encoded integer giving you 2^16 - 1 = 65535 bytes to send. This makes it possible for readUTF to find the end of the encoded string. If you decide on your own record structure then you should make sure that the end and type of the record is either known or detectable.

这篇关于使用套接字发送和接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆