C#基于Socket的网络通信类你了解吗_第1页
C#基于Socket的网络通信类你了解吗_第2页
C#基于Socket的网络通信类你了解吗_第3页
C#基于Socket的网络通信类你了解吗_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第C#基于Socket的网络通信类你了解吗//获取TcpListener对象的本地终结点的IP地址

IPEndPointlocalEndPoint=(IPEndPoint)tcpListener.LocalEndpoint;

returnlocalEndPoint.Address.ToString();

publicstaticintGetLocalPoint_Port(TcpListenertcpListener)

//获取TcpListener对象的本地终结点的端口号

IPEndPointlocalEndPoint=(IPEndPoint)tcpListener.LocalEndpoint;

returnlocalEndPoint.Port;

publicstaticIPEndPointGetLocalPoint(SocketNetSocket)

//获取Socket对象的本地终结点

return(IPEndPoint)NetSocket.LocalEndPoint;

publicstaticstringGetLocalPoint_IP(SocketNetSocket)

//获取Socket对象的本地终结点的IP地址

IPEndPointlocalEndPoint=(IPEndPoint)NetSocket.LocalEndPoint;

returnlocalEndPoint.Address.ToString();

publicstaticintGetLocalPoint_Port(SocketNetSocket)

//获取Socket对象的本地终结点的端口号

IPEndPointlocalEndPoint=(IPEndPoint)NetSocket.LocalEndPoint;

returnlocalEndPoint.Port;

publicstaticvoidBindEndPoint(SocketNetSocket,IPEndPointendPoint)

//绑定终结点

if(!NetSocket.IsBound)

NetSocket.Bind(endPoint);

publicstaticvoidBindEndPoint(SocketNetSocket,stringNetIP,intNetPort)

//创建终结点

IPEndPointendPoint=CreateIPEndPoint(NetIP,NetPort);

//绑定终结点

if(!NetSocket.IsBound)

NetSocket.Bind(endPoint);

publicstaticvoidStartListen(SocketNetSocket,intNetPort)

//创建本地终结点

IPEndPointlocalPoint=CreateIPEndPoint(InternetProHelper.LocalHostName,NetPort);

//绑定到本地终结点

BindEndPoint(NetSocket,localPoint);

//开始监听

NetSocket.Listen(200);

publicstaticvoidStartListen(SocketNetSocket,intNetPort,intmaxConnection)

//创建本地终结点

IPEndPointlocalPoint=CreateIPEndPoint(InternetProHelper.LocalHostName,NetPort);

//绑定到本地终结点

BindEndPoint(NetSocket,localPoint);

//开始监听

NetSocket.Listen(maxConnection);

publicstaticvoidStartListen(SocketNetSocket,stringNetIP,intNetPort,intmaxConnection)

//绑定到本地终结点

BindEndPoint(NetSocket,NetIP,NetPort);

//开始监听

NetSocket.Listen(maxConnection);

publicstaticboolConnect(SocketNetSocket,stringNetIP,intNetPort)

//连接到基于TCP协议的服务器,连接成功返回true,否则返回false

//连接服务器

NetSocket.Connect(NetIP,NetPort);

//检测连接状态

returnNetSocket.Poll(-1,SelectMode.SelectWrite);

catch(SocketExceptionex)

thrownewException(ex.Message);

//以同步方式向指定的Socket对象发送消息

publicstaticvoidSendMsg(SocketNetSocket,byte[]dataStr)

//发送消息

NetSocket.Send(dataStr,dataStr.Length,SocketFlags.None);

//使用UTF8编码格式以同步方式向指定的Socket对象发送消息

publicstaticvoidSendMsg(SocketNetSocket,stringdataStr)

//将字符串消息转换成字符数组

byte[]NetBuf=System.Text.Encoding.UTF8.GetBytes(dataStr);

//发送消息

NetSocket.Send(NetBuf,NetBuf.Length,SocketFlags.None);

//以同步方式接收消息

publicstaticvoidReceiveMsg(SocketNetSocket,byte[]NetBuf)

NetSocket.Receive(NetBuf);

//以同步方式接收消息

publicstaticstringReceiveMsg(SocketNetSocket)

//定义接收缓冲区

byte[]NetBuf=newbyte[10000];

//接收数据,获取接收到的字节数

intRecvNum=NetSocket.Receive(NetBuf);

//定义临时缓冲区

byte[]DataBuf=newbyte[RecvNum];

//将接收到的数据写入临时缓冲区

Buffer.BlockCopy(NetBuf,0,DataBuf,0,RecvNum);

//对DataBuf进行处理

returnConvert.ToString(DataBuf);

//关闭基于Tcp协议的Socket对象

publicstaticvoidClose(SocketNetSocket)

//禁止Socket对象接收和发送数据

NetSocket.S

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论