using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace _20211014网络编程SocketServer
{
class Program
{
static void Main(string[] args)
{
//实例化一个Socket服务类
Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//绑定IP端口
byte[] byteList = new byte[] {127,0,0,1};
IPAddress ip = new IPAddress(byteList);
EndPoint endPoint = new IPEndPoint(ip,7788);
server.Bind(endPoint);
//开始监听
server.Listen(100); //最大连接数100个
//接收客户端连接,派生为客户端服务的Socket
Socket client001 = server.Accept();
//发送欢迎消息
string message = "你好,欢迎登陆!";
byte[] byteSend = Encoding.UTF8.GetBytes(message);
client001.Send(byteSend);
//接收客户端消息
byte[] byteReceiv = new byte[1024];
int leng = client001.Receive(byteReceiv); //接收了lang个字节
string strReceiv = Encoding.UTF8.GetString(byteReceiv,0,leng);
Console.WriteLine(strReceiv);
//保持main线程不退出
Console.ReadKey();
}
}
}
20211014网络编程SocketServer.csproj
最新推荐文章于 2023-03-10 09:45:55 发布