同一个Socket, 连续发送2次,Receive就会报错
//create Socket
public void Connect()
{
if (Connected) return;
byte[] data = new byte[1024];
newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//newclient.
string ipadd = "127.0.0.1";
int port = Convert.ToInt32("8888");
IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port);
try
{
newclient.Connect(ie);
Connected = true;
}
catch (SocketException e)
{
return;
}
}
//接收报文
public void ReceiveMsg()
{
bool bbl = false;
while (!bbl)
{
try
{
byte[] data = new byte[55296];
//同一个Socket, 连续发送2次,Receive就会报错:10053 An established connection was aborted by the software in your host mac
int recv = newclient.Receive(data);//
string stringdata = Encoding.GetEncoding("gb2312").GetString(data, 0, recv);
if (stringdata.Trim().Length > 0)
{
///////////读取报文回执///
//readerxml(stringdata);
sendtag = 0;//标记空闲
bbl = true;
}
}
catch
{
}
}
}
//发送报文
private void timer1_Tick(object sender, EventArgs e)
{
if (sendtag == 0)//标记空闲
{
if (tag < arrlist.Count)
{
sendtag = 1;//标记正在发送
String Msg = arrlist[tag].ToString();
if (Connected)
{
int m_length = Msg.Length;
byte[] data = new byte[m_length];
data = Encoding.GetEncoding("gb2312").GetBytes(Msg);
int i = newclient.Send(data);
ReceiveMsg();
tag++;
}
else
{
addlog("服务可能未启动.");
}
}
else
{
timer1.Stop();
}
}
}
[解决办法]
有可能是服务端主动关闭了连接
是否因为发送了错误的数据
[解决办法]
用个线程监听
[解决办法]
http://www.cnblogs.com/JimmyZhang/archive/2008/09/07/1286300.html
你看看这个的博客吧,这个博客上讲的很清楚,非常不错,它分了5个部分去讲,你仔细看看,就应该能帮你解决问题。
[解决办法]
一边连接。另一边监听。