//创建Socket服务
bool CreateServer(){
try{
//创建负责监听的套接字,注意其中的参数
socketServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//获取文本框中的IP对象
IPAddress address = IPAddress.Parse(IP);//赋值IP
//创建包含IP和端口号的网络节点对象
IPEndPoint endPoint = new IPEndPoint(Ip,port);//复制IP,端口号
try{
//将负责监听的套接字绑定到唯一的IP和端口上
socketServer.Bind(endPoint);
}
catch(SocketException se){
return false;
}
//设置监听队列长度
socketServer.Listen(10);
//创建负责监听的线程
threadServer = new Thread(ServerConnecting);
threadServer.IsBackground = true;
threadServer.Start();
return true;
}
catch(Exception ex){
return false;
}
Socket sokConnection = socketServer.Accept();
Threadt thr = new Thread(read);//接受客户端发送的信息
thr.Isbackground = true;
thr.Start(sokConnection)
}
catch{}
}
//创建客户端
socketClient = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
bool CreateServer(){
try{
//创建负责监听的套接字,注意其中的参数
socketServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//获取文本框中的IP对象
IPAddress address = IPAddress.Parse(IP);//赋值IP
//创建包含IP和端口号的网络节点对象
IPEndPoint endPoint = new IPEndPoint(Ip,port);//复制IP,端口号
try{
//将负责监听的套接字绑定到唯一的IP和端口上
socketServer.Bind(endPoint);
}
catch(SocketException se){
return false;
}
//设置监听队列长度
socketServer.Listen(10);
//创建负责监听的线程
threadServer = new Thread(ServerConnecting);
threadServer.IsBackground = true;
threadServer.Start();
return true;
}
catch(Exception ex){
return false;
}
}
//实时监测连接到Server的Socket
void ServerConnecting(){
try{
Socket sokConnection = socketServer.Accept();
Threadt thr = new Thread(read);//接受客户端发送的信息
thr.Isbackground = true;
thr.Start(sokConnection)
}
catch{}
}
//创建客户端
socketClient = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPAddress address = IPAddress.Parse(IP);//实例化IP
socketClient.Connect(address,port);//IP、端口