C#用socket发送组包给java服务器,调用recieve时ConnectionAbort。
java要求 将数据报文通过utf8编码发送。
connection和send都能通过。
调用接收直接返回0。
不知道是组包格式不对还是字节序,查资料说utf8不需要字节序,请大神帮忙分析一下。
IPAddress ip = IPAddress.Parse("192.168.65.30");
IPEndPoint endP = new IPEndPoint(ip, 8099);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(endP);
byte[] face1 = converttobytes("image/1.JPG");
byte[] face2 = converttobytes("image/1.JPG");
byte[] magic_word = Encoding.ASCII.GetBytes("HBVE");
int version = 1;
byte type = 1;
int id = 10000;
byte type1 = 1;
byte type2 = 1;
int face1_len = face1.Length;
int face2_len = face2.Length;
int totallength = 10 + face1.Length + face2.Length + 5;
byte[] bytes = null;
using (MemoryStream ms = new MemoryStream())
{
using (BinaryWriter bw = new BinaryWriter(ms, Encoding.UTF8))
{
//bw.Write(bytes);
bw.Write(magic_word);
bw.Write(version);
bw.Write(totallength);
bw.Write(type);
bw.Write(id);
bw.Write(type1);
bw.Write(type2);
bw.Write(face1_len);
bw.Write(face1);
bw.Write(face2_len);
bw.Write(face2);
ms.Position = 0;
using (BinaryReader br = new BinaryReader(ms, Encoding.UTF8))
{
bytes = br.ReadBytes((int)ms.Length);
}
}
}
try
{
SocketError error = SocketError.AccessDenied;
int result = client.Send(bytes, 0, bytes.Length, SocketFlags.None, out error);
byte[] by = new byte[client.ReceiveBufferSize];
result = client.Receive(by, 0, by.Length, SocketFlags.None, out error);
}
catch (Exception ex)
{
throw ex;
}