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

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

问题描述

我使用套接字连接我的Andr​​oid应用程序(客户端)和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)

我怎么能发送这些,使服务器间$ P $点各为一个独立的实体?

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.)

我会张贴一些code,但我不知道怎么样,这将有助于。

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

推荐答案

我假设你使用的是TCP套接字客户端 - 服务器交互?到不同类型的数据发送给服务器,并让它能够在两者之间区分的一种方法是将专用的第一个字节(或更多,如果你有超过256类型的消息),为某种标识符。如果第一个字节是其中之一,那么它是消息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).

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

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