相关文章推荐
曾经爱过的南瓜  ·  Unable to open socket ...·  1 月前    · 
讲道义的企鹅  ·  function - Is there ...·  2 年前    · 
奋斗的乒乓球  ·  How do I apply ...·  2 年前    · 
现在生成了JSON格式的字符串,但是怎样通过Socket发送出去呢?求指教!!谢谢!... 现在生成了 JSON 格式的字符串,但是怎样通过 Socket 发送出去呢?求指教!!谢谢! /// <summary>
/// 序列化
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string getJsonStringByObject(object obj)
{
DataContractJsonSerializer serialier = new DataContractJsonSerializer(obj.GetType());
MemoryStream stream = new MemoryStream();
serialier.WriteObject(stream, obj);
byte[] dataBytes = new byte[stream.Length];
stream.Position = 0;
stream.Read(dataBytes, 0, (int)stream.Length);
string msg = Encoding.UTF8.GetString(dataBytes);
return msg;
}
string jsonString = getJsonStringByObject(list);
byte[] buffer= Encoding.UTF8.GetBytes(jsonString)
NetworkStream netStream = client.GetStream(); //client是Socket连接
netStream.Write(buffer, 0, buffer.Length); //发送