using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace SocketServerAcceptMultipleClient
{
public class SocketServer
{
// 创建一个和客户端通信的套接字
static Socket socketwatch = null;
//定义一个集合,存储客户端信息
static Dictionary<string, Socket> clientConnectionItems = new Dictionary<string, Socket> { };
public static void Main(string[] args)
{
//定义一个套接字用于监听客户端发来的消息,包含三个参数(IP4寻址协议,流式连接,Tcp协议)
socketwatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//服务端发送信息需要一个IP地址和端口号
IPAddress address = IPAddress.Parse("127.0.0.1");
//将IP地址和端口号绑定到网络节点point上
IPEndPoint point = new IPEndPoint(address, 7777);
//此端口专门用来监听的
//监听绑定的网络节点
socketwatch.Bind(point);
//将套接字的监听队列长度限制为20
socketwatch.Listen(20);
//负责监听客户端的线程:创建一个监听线程
Thread threadwatch = new Thread(watchconnecting);
//获取客户端的IP和端口号
IPAddress clientIP = (connection.RemoteEndPoint as IPEndPoint).Address;
int clientPort = (connection.RemoteEndPoint as IPEndPoint).Port;