Asp.net c #. net Tcp socket receives data code
Asp tutorial. net c #. net tcp socket receives data code
// Listen to the network
Public bool opennet (string sport)
{
Processor = new thread (new threadstart (startlistening); // create a listening thread
Processor. priority = threadpriority. normal;
Processor. isbackground = true;
Processor. start (); // thread start
Return true;
}
Private void startlistening ()
{
// Obtain the 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); // you can specify the IP address and port number used to access the host.
_ Svrsock = new socket (addressfamily. internetwork, sockettype. stream, protocoltype. tcp); // instantiate the socket object.
_ Svrsock. bind (m_endpoint); // bind the host to be accessed.
_ Svrsock. listen (50); // start listening. The maximum package length is 50.
// While LOOP receiving
While (true)
{
If (m_blisten = false)
{
Accsock = _ svrsock. accept (); // receives the client's service request socket.
}
//
Idatabuflen = (ushort) accsock. receive (bdatabuf, bdatabuf. length, 0 );
//
If (idatabuflen> 0)
{
// Process data
// M_blisten is set to true at the same time
}
}
}
Catch (exception ex)
{
Messagebox. show ("listening exception:" + ex. message );
Return;
}
}
/*
The server and client are connected over tcp.
The server sends several data records continuously, and the client receives the while (true) messages cyclically.
Only the first two pieces of data can be received in the result.
The server ensures that there is no problem, and the problem should be on the client.
*/