主题:关于TcpClient类的问题
[em10]
写了个S/C模型的代码,C端是用C#写的,代码如下:
private void button2_Click(object sender, System.EventArgs e)//连接WHClient
{
string ClientMachine = textBox3.Text;
IPAddress remoteIPAddress = new IPAddress(0);
remoteIPAddress = IPAddress.Parse(ClientMachine);
ConnectWHClient.Connect(remoteIPAddress,2000);//真正关键的连接语句。
textBox4.Text = " ok";
}
private void button6_Click(object sender, System.EventArgs e)//断开
{
ConnectWHClient.Close();
textBox4.Text = "Close";
}
S端是用API写的,代码如下:
WHCSocketSend=socket(AF_INET,SOCK_STREAM,0)
setsockopt(WHCSocketSend,SOL_SOCKET,SO_REUSEADDR,(const char*)&bDontLinger,sizeof(BOOL));
WSAAsyncSelect(WHCSocketSend,NULL,NULL,FD_ACCEPT|FD_CLOSE|FD_READ);
if( WSAAsyncSelect!=0 )
{
switch(ConnectlParam)
{
case FD_ACCEPT:
accept(WHCSocketSend,INADDR_ANY,NULL);
break;
case FD_CLOSE:
closesocket(WHCSocketSend);
break;
}
}
现在遇到的问题是,连接没问题,但每次断开后S端总是在出现CLOSE_WATE就没反应了,我查过网上的很多文章,说加了“setsockopt(WHCSocketSend,SOL_SOCKET,SO_REUSEADDR,(const char*)&bDontLinger,sizeof(BOOL));”这句应该可以解决,但文章中所说的S端和C端都用的是API,而我C端用的是C#,好像在.NET Framenwork类库中能关闭TCP连接的也只有ConnectWHClient.Close()这一个方法,请问高手们该如何解决“CLOSE_WATE没反应”的问题。
[em10]
写了个S/C模型的代码,C端是用C#写的,代码如下:
private void button2_Click(object sender, System.EventArgs e)//连接WHClient
{
string ClientMachine = textBox3.Text;
IPAddress remoteIPAddress = new IPAddress(0);
remoteIPAddress = IPAddress.Parse(ClientMachine);
ConnectWHClient.Connect(remoteIPAddress,2000);//真正关键的连接语句。
textBox4.Text = " ok";
}
private void button6_Click(object sender, System.EventArgs e)//断开
{
ConnectWHClient.Close();
textBox4.Text = "Close";
}
S端是用API写的,代码如下:
WHCSocketSend=socket(AF_INET,SOCK_STREAM,0)
setsockopt(WHCSocketSend,SOL_SOCKET,SO_REUSEADDR,(const char*)&bDontLinger,sizeof(BOOL));
WSAAsyncSelect(WHCSocketSend,NULL,NULL,FD_ACCEPT|FD_CLOSE|FD_READ);
if( WSAAsyncSelect!=0 )
{
switch(ConnectlParam)
{
case FD_ACCEPT:
accept(WHCSocketSend,INADDR_ANY,NULL);
break;
case FD_CLOSE:
closesocket(WHCSocketSend);
break;
}
}
现在遇到的问题是,连接没问题,但每次断开后S端总是在出现CLOSE_WATE就没反应了,我查过网上的很多文章,说加了“setsockopt(WHCSocketSend,SOL_SOCKET,SO_REUSEADDR,(const char*)&bDontLinger,sizeof(BOOL));”这句应该可以解决,但文章中所说的S端和C端都用的是API,而我C端用的是C#,好像在.NET Framenwork类库中能关闭TCP连接的也只有ConnectWHClient.Close()这一个方法,请问高手们该如何解决“CLOSE_WATE没反应”的问题。
[em10]