asp.net c#.net TCP socket receive data code

Source: Internet
Author: User
Tags bind socket

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.

*/

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.