问题描述
我需要使用套接字将流发送到服务器.
I need to send a stream to a server using socket.
我想按以下顺序将以下内容转换为流:
I want to convert the following into stream in this order :
5 [Msg] + 6 [User] + int(照片的长度) + PhotoByte []
5[Msg] + 6[User] + int (lenght of photo) + PhotoByte[]
数字代表要读取的字节数.即5表示读取5后要读取的5个字节.
The Number represent How many bytes to read. Ie 5 means 5 bytes to read after 5 is read.
a)如何将数字表示为4个字节的整数?
a) How to represent the number as 4 byte integer?
我是否必须按以下方式在十六进制中将数字写为字符串并将其转换为字节数组?或者有更好的方法.怎样写照片长度以十六进制表示5 Mb大小的照片?
Do I have to write the number as string in Hex as follows and convert into bytearray or there is a better way. How to write number of Photo lenght say 5 Mb size of a photo in hex?
字符串strPart1 =" {0x05} {0x00} {0x00} {0x00} [消息] {0x06} {0x00} {0x00} {0x00} [用户] + ......
string strPart1 = " {0x05}{0x00}{0x00}{0x00}[Msg]{0x06}{0x00}{0x00}{0x00}[User] + ...... "
byte [] strPartPhoto = PhotoByte [];
byte[] strPartPhoto = PhotoByte[];
b)通过服务器
b) Over the server
如何我会阅读吗?还是拿出每个零件?像[Msg] [User]一样,照片长度为photoByte []吗?
How do i read or get each parts out ? like [Msg][User] , int lenght of photo, photoByte[]?
谢谢
推荐答案
根据我的理解..我已经为相同的要求编写了代码段.
As per my understanding.. i have written a code snippet for the same requirement.
private void WriteAndRead()
{
string MSG = "TEST MSG";
string USR = "PAVAN_PARETA";
int IMG_DATA = 1212112;
Byte[] DATA_1 = GetBytes(MSG);
Byte[] DATA_2 = GetBytes(USR);
Byte[] DATA_3 = BitConverter.GetBytes(IMG_DATA);
Byte[] TOTAL_DATA = new Byte[DATA_1.Length + DATA_2.Length + DATA_3.Length];
Array.Copy(DATA_1, 0, TOTAL_DATA, 0, DATA_1.Length);
Array.Copy(DATA_2, 0, TOTAL_DATA, DATA_1.Length, DATA_2.Length);
Array.Copy(DATA_3, 0, TOTAL_DATA, DATA_1.Length + DATA_2.Length, DATA_3.Length);
// TODO: Here you can save your (TOTAL_DATA) data to server or file.
// TODO: Here you can read data from the server. This example I have used the exsiting TOTAL_DATA
// Assuem it is reading from the server.
Byte[] READ_MSG = ReadSpecificStreamToBytes(TOTAL_DATA, 0, DATA_1.Length);
Byte[] READ_USR = ReadSpecificStreamToBytes(TOTAL_DATA, DATA_1.Length, DATA_2.Length);
Byte[] READ_IMG = ReadSpecificStreamToBytes(TOTAL_DATA, DATA_1.Length + DATA_2.Length, DATA_3.Length);
MessageBox.Show(GetString(READ_MSG));
MessageBox.Show(GetString(READ_USR));
MessageBox.Show(GetString(READ_IMG));
}
希望有帮助.
这篇关于如何发送流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!