首页 > 其他 > 详细

CTcpClient类的源文件

时间:2018-12-02 15:43:41      阅读:193      评论:0      收藏:0      [点我收藏+]

CTcpClient类的源文件

 

转载:

http://blog.chiwudaili.com/detail.aspx?id=6b46e08d4c482f304e976a1081e40352

 

public class CTcpClient

{

private Socket ClientSocket { get; set; }

public CTcpClient()

{

ClientSocket = null;

}

public CTcpClient(Socket socket)

{

ClientSocket = socket;

}

public bool Start(int port = 8080, string ip = "127.0.0.1", int timeoutInSec = 10)

{

try

{

IPAddress ipA = IPAddress.Parse(ip);

ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

ClientSocket.SendTimeout = timeoutInSec * 1000;

ClientSocket.ReceiveTimeout = timeoutInSec * 1000;

ClientSocket.Connect(new IPEndPoint(ipA, port));

}

catch (Exception)

{

return false;

}

 

return true;

}

public int Receive(byte[] buffer, int offset, int size)

{

SocketError socketError = SocketError.Success;

 

int rel = ClientSocket.Receive(buffer, offset, size, SocketFlags.None, out socketError);

if (socketError == SocketError.Success)

{

return rel; 

}

return -1;

}

public int Send(byte[] buffer, int offset, int size)

{

SocketError socketError = SocketError.Success;

int rel = ClientSocket.Send(buffer, offset, size, SocketFlags.None, out socketError);

if (socketError == SocketError.Success)

{

return rel;

}

return -1;

}

public void Close()

{

if (ClientSocket!=null)

{

try

{

ClientSocket.Disconnect(false);

}

catch (Exception e)

{

}

try

{

ClientSocket.Dispose();

}

catch (Exception e)

{

}

try

{

ClientSocket.Close();

}

catch (Exception e)

{

}

ClientSocket = null;

}

}

}

CTcpClient类的源文件

原文:https://www.cnblogs.com/zj41342626/p/10053579.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!