static void Main(string[] args)
{
//1、创建socket
Socket tcpServer = new Socket(
AddressFamily.InterNetwork, //内网
SocketType.Stream, //用什么东西通信 流
ProtocolType.Tcp);// 以什么协议进行通信 Tcp协议
//2绑定IP跟端口号
IPAddress ipaddress = new IPAddress(new byte[]{192,168,2,91});//确定服务器IP地址
EndPoint point = new IPEndPoint(ipaddress,7788);//IPEndPoint是对IP+端口做了一层封装的类。
tcpServer.Bind(point);//完成绑定,向操作系统申请一个可以用的IP和端口号,用来做通信
tcpServer.Listen(100); //开始监听,等待客户端做链接,参数是最大连接数。
Socket ClientSocket = tcpServer.Accept();//暂停到当前线程,直到有一个客户端有一个链接过来,再进行下面的代码
//使用返回的Socket和客户端做通信
string message = "hi,欢迎你";
byte[] data = Encoding.UTF8.GetBytes(message);//对字符串做编码,得到一个字符串的字节数组。
ClientSocket.Send(data);
}
{
//1、创建socket
Socket tcpServer = new Socket(
AddressFamily.InterNetwork, //内网
SocketType.Stream, //用什么东西通信 流
ProtocolType.Tcp);// 以什么协议进行通信 Tcp协议
//2绑定IP跟端口号
IPAddress ipaddress = new IPAddress(new byte[]{192,168,2,91});//确定服务器IP地址
EndPoint point = new IPEndPoint(ipaddress,7788);//IPEndPoint是对IP+端口做了一层封装的类。
tcpServer.Bind(point);//完成绑定,向操作系统申请一个可以用的IP和端口号,用来做通信
tcpServer.Listen(100); //开始监听,等待客户端做链接,参数是最大连接数。
Socket ClientSocket = tcpServer.Accept();//暂停到当前线程,直到有一个客户端有一个链接过来,再进行下面的代码
//使用返回的Socket和客户端做通信
string message = "hi,欢迎你";
byte[] data = Encoding.UTF8.GetBytes(message);//对字符串做编码,得到一个字符串的字节数组。
ClientSocket.Send(data);
}