C# Tcp传输 不稳定 是不是小弟我的消息太大
C# Tcp传输 不稳定 是不是我的消息太大?
我的消息类如下:
我是直接通过BinnaryFormatter序列化后直接发送,接收到后反序列化的,不知道是不是消息太大了,反正连接过程中不稳定,经常没有响应。
求指导!!!
------解决方案--------------------
public List<String> setValues; 这个内容多的话会超过socket默认的buffer, 8192bytes,所以需要接收完整个数据再反序列化
------解决方案--------------------
不要用序列化,序列化有很多问题,对于引用,比如你的 List<String> setValues,.net处理起来很麻烦,效率很低,你的这个数据不大,但序列化出来的数据可能会很大,
所以你最好自己写序列化方法,
------解决方案--------------------
------解决方案--------------------
试一下就知道了,
------解决方案--------------------
我的消息类如下:
- C# code
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace NetLib { [Serializable] public class NetMsg { #region 常量定义 public const string ALL_PEOPLE = "/all"; public const int BUFFER_SIZE = 4000; public const int OPR_MOV = 0; public const int OPR_CREATE = 1; public const int OPR_START_MOV = 2; public const int OPR_DESTROY = 3; public const int OPR_LOST_BLOOD = 4; public const int OPR_STOP_MOV = 5; public const int OPR_TURN=6; public const int LOGIN = 7; public const int LOGOUT = 8; public const int ROOM_APPLY = 9; public const int ROOM_ENTER = 10; public const int OPR_CHAT=11; public const int ROOM_CREATE=12; public const int MAX_ROOM = 13; public const int MAX_USER = 14; public const int ROOM_FULL = 15; public const int ROOMS_INFO=16; public const int USERS_INFO=17; public const int LOGIN_OK = 18; public const int LOGIN_FAIL = 19; public const int CONNECTED = 20; public const int IMALIVE=21;//心跳包 public const int REG_APPLY = 22; public const int OPR_SENDIP = 23; public const int ROOM_EXIT = 24; public const int OPR_UR_MASTER = 25;//你是房主 public const int OPR_START_GAME = 26;//进入游戏 public const int OPR_PREPARE = 27;//准备消息 public const int OPR_CAN_START = 28;//可以开始游戏 public const int REG_OK = 29; public const int REG_FAIL = 30; public const int OPR_FIRE = 31; public const int TYPE_TANK = 32; public const int TYPE_MISSILE = 33; public const int TYPE_BASE = 34; public const int TYPE_BLAST = 35; public const int OPR_STARTED = 36; #endregion 常量定义结束 public int roomNumber; public int clientID; public int operation; public int objType; public int objNumber; public int locationX; public int locationY; public int direction; public int addtionInt; public string addtionInfo;//额外信息,如发送的消息、ip地址等等 public int receiverID; public List<String> setValues; public string username; public string password; } }
我是直接通过BinnaryFormatter序列化后直接发送,接收到后反序列化的,不知道是不是消息太大了,反正连接过程中不稳定,经常没有响应。
求指导!!!
------解决方案--------------------
public List<String> setValues; 这个内容多的话会超过socket默认的buffer, 8192bytes,所以需要接收完整个数据再反序列化
------解决方案--------------------
不要用序列化,序列化有很多问题,对于引用,比如你的 List<String> setValues,.net处理起来很麻烦,效率很低,你的这个数据不大,但序列化出来的数据可能会很大,
所以你最好自己写序列化方法,
------解决方案--------------------
------解决方案--------------------
试一下就知道了,
------解决方案--------------------