ASP tutorial. NET c#.net TCP socket receive data code
Listening Network
public bool OpenNet (string sport)
{
processor = new Thread (new ThreadStart (startlistening));/New Listener thread
processor.priority = Threadpriority.normal;
Processor.isbackground = true;
Processor.start ()//thread start
return true;
}
private void Startlistening ()
{
Get Port number
String sport = M_inifile.inireadvalue ("net", "TCPPort");
int iport = Int.parse (sport);
//
M_blisten = false;
//
Try
{
M_endpoint = new IPEndPoint (Ipaddress.any, iport);//The combination will access the host's IP address and port number.
_svrsock = new Socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp);//Instantiate the socket object.
_svrsock.bind (m_endpoint);//Bind the host that will be accessed.
_svrsock.listen (50);//start monitoring, Maximum packet length 50.
While loop receive
while (true)
{
if (M_blisten = = False)
{
Accsock = _svrsock.accept ();//Receive Client's service request socket.
}
//
Idatabuflen = (ushort) accsock.receive (bdatabuf, bdatabuf.length, 0);
//
if (Idatabuflen > 0)
{
Working with Data
At the same time, M_blisten is reset True
}
}
}
catch (Exception ex)
{
MessageBox.Show ("Monitor exception:" + Ex.message);
Return
}
}
/*
The server and the client are connected by TCP.
The server sends several consecutive data, and the client while (true) loops over the receiver
The result can only receive the first two data, and the data will not be received later.
Server side to ensure that there is no problem, the problem should be on the client.
*/