110,536
社区成员
发帖
与我相关
我的任务
分享
private string WriteSyncData(Socket dataSocket, string sendStr) {
if (dataSocket != null && dataSocket.Connected) {
try {
//Convert string to byte
byte[] bytesSendStr = new byte[1024];
bytesSendStr = Encoding.ASCII.GetBytes(sendStr);
dataSocket.Send(bytesSendStr, 0, bytesSendStr.Length, SocketFlags.None);
int result =0;
StringBuilder content = new StringBuilder();
try {
result = dataSocket.Receive(_Buffer, 0, _Buffer.Length, SocketFlags.None);//这里我接收一次后怎么就清了Socket的缓存,不然下次接收的就不是最新的发送来的数据了
content.Append(Encoding.ASCII.GetString(_Buffer, 0, result));
//dataSocket.ReceiveBufferSize = 0;//这个方法不可行
} catch (SystemException ex) {
throw ex;
}
//if (result == 0) {
// CallException(EXCEPTION_CONN_LOST);
// return null;
//}
return content.ToString();
} catch (SystemException ex) {
LogService.Error(ex);
CallException(EXCEPTION_CONN_LOST);
}
}
return null;
}