Asp.net c #. net Tcp socket receives data code

Source: Internet
Author: User

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.

*/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.