最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net c#.net Tcp socket 接收数据代码
时间:2010-07-13 编辑:简简单单 来源:一聚教程网
asp教程.net c#.net tcp socket 接收数据代码
//监听网络
public bool opennet(string sport)
{
processor = new thread(new threadstart(startlistening));//新建监听线程
processor.priority = threadpriority.normal;
processor.isbackground = true;
processor.start();//线程开始
return true;
}
private void startlistening()
{
//获取端口号
string sport = m_inifile.inireadvalue("net", "tcpport");
int iport = int.parse(sport);
//
m_blisten = false;
//
try
{
m_endpoint = new ipendpoint(ipaddress.any, iport);//组合将访问主机的ip地址和端口号。
_svrsock = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);//实例化socket对象。
_svrsock.bind(m_endpoint);//绑定将访问的主机。
_svrsock.listen(50);//开始监听,最大包长50。//while循环接收
while (true)
{
if (m_blisten == false)
{
accsock = _svrsock.accept();//接收客户端的服务请求套接字。
}
//
idatabuflen = (ushort)accsock.receive(bdatabuf, bdatabuf.length, 0);
//
if (idatabuflen > 0)
{
//处理数据
//同时m_blisten被置true
}
}
}
catch (exception ex)
{
messagebox.show("监听异常:" + ex.message);
return;
}
}
/*
服务器和客户端采用tcp方式连接。
服务器连续发送几条数据过来,客户端while(true)循环接收
结果只能接收到前面两条数据,后面还有数据接收不到。
服务器端确保没有问题,问题应处在客户端。
*/
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10