//客户端:-Socket clientSocket
= new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
-IPEndPoint endPoint
= new IPEndPoint(IPAddress.Parse(
"127.0.0.1"),
8888);
-clientSocket.Connect(endPoint);
SocketHelper:
public class SocketHelper
{
//创建服务器端socket//创建一个监听一个ip和端口的socket//传递两个参数:ip,端口号public static Socket CreateListenSocket(
string ip,
int port)
{
if(
string.IsNullOrEmpty(ip)
|| port
>65535 || port
< 0)
{
return null;
}
Socket socket
= new Socket(AddressFamily.InterNetWork,SocketType.Stream,ProtocolType.Tcp);
IPAddress ipAddress
= IPAddress.Parse(ip);
IPEndPoint endpoint
= new IPEndPoint(ipAddress,port);
socket.Bind(endpoint);
socket.Listen(
10);
return socket;
}
//客户端连接服务器public static Socket ConnectServer(
string ip,
int port)
{
Socket clientSocket
= null;
try{
clientSocket
= new Socket(AddressFamily.InterNetWork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint endPoint
= new IPEndPoint(IPAddress.Parse(ip),port);
clientSocket.Connect(endPoint);
}
finally{
//记录日志}
return clientSocket;
}
}
通过 为知笔记 发布
优质内容筛选与推荐>>
1、再谈从vc6迁移到vs20052、碰撞检测3、平衡树(模板+文艺平衡树)4、python安装simplejson5、[转]Ubuntu下编译安装Opencv
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。