其他分享
首页 > 其他分享> > Socket通讯服务器端

Socket通讯服务器端

作者:互联网

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace ChatServer
{
    class SocketServer
    {
        private Thread ServerThread;//线程
        public SocketServer()//构造方法
        {
            ServerThread = new Thread(OnServerRun);//new一个线程
            ServerThread.Start();//启动线程
        }
        private void OnServerRun()
        {
            //创建Socket  IP地址   数据流   协议
            Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp );
            //ip地址  127.0.0.1本机
            IPAddress Ip = IPAddress.Parse("127.0.0.1");
            // 端口号
            int port = 10086;
            //IPEndPoint两个参数(ip地址,端口号)
            IPEndPoint iPEndPoint=new IPEndPoint (Ip,port );
            socket.Bind(iPEndPoint);
            //监听
            socket.Listen(0);
            while (true)
            {
                Console.WriteLine("开始接收一个客户端");
                Socket client = socket.Accept();
                Console.WriteLine("接收一个客户端");
            }
        }
    }
}

标签:通讯,服务器端,System,IPEndPoint,socket,new,using,Socket
来源: https://blog.csdn.net/SeptemberTan/article/details/120194793