Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过UDP套接字发送数据包_Java_Sockets_Bytearray_Dataoutputstream_Bytearrayoutputstream - Fatal编程技术网

Java 通过UDP套接字发送数据包

Java 通过UDP套接字发送数据包,java,sockets,bytearray,dataoutputstream,bytearrayoutputstream,Java,Sockets,Bytearray,Dataoutputstream,Bytearrayoutputstream,我正在尝试将以下数据发送到将使用C++的服务器: static int user_id; // 4 Bytes static byte state; // 1 Byte static String cipher_data; // 128 Bytes static String hash; // 128 Bytes static final int PACKET_SIZE = 261; public static byte [] packet = new byte [PACKET_SIZE

我正在尝试将以下数据发送到将使用C++的服务器:

static int user_id; // 4 Bytes
static byte state;  // 1 Byte
static String cipher_data; // 128 Bytes
static String hash;  // 128 Bytes

static final int PACKET_SIZE = 261;

public static byte [] packet = new byte [PACKET_SIZE];
我正在尝试创建一个字节数组,其中我将包含所有字节:

ByteArrayOutputStream baos = new ByteArrayOutputStream(PACKET_SIZE);
DataOutputStream dos = new DataOutputStream(baos);
dos.write(state);
dos.writeInt(user_id);
for (int i = 0; i < cipher_data.length(); i++) {
    dos.write((byte) cipher_data.charAt(i));
}
for (int i = 0; i < cipher_data.length(); i++) {
    dos.write((byte) hash.charAt(i));
}
packet = baos.toByteArray();
ByteArrayOutputStream-bas=新的ByteArrayOutputStream(数据包大小);
DataOutputStream dos=新的DataOutputStream(BAS);
写(状态);
dos.writeInt(用户id);
对于(int i=0;i
现在我有了包含所有数据的字节数组,但我不确定我所做的是否正确,以及是否能够从服务器端读取所有这些数据。如果你能给我一些建议,我将不胜感激


谢谢,

您首先需要关心的是源计算机和目标计算机的名称

Java是

C++并不重要,您需要确定目标程序在哪台机器(硬件/操作系统)上执行


在那之后,thread将能够让您通过。

第二个是字符串编码。使用
String.getBytes()
而不是将字符强制转换为字节