服务器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace _000_NetWorkMyService
{
class Program
{
private const int port = 9999; //端口号
private static string IpStr = "127.0.0.1"; //ip地址
private static Socket serverSocket; //socket对象
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse(IpStr);
IPEndPoint iPEndPoint = new IPEndPoint(ip, port);
//创建服务器socket对象,并设置属性
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//绑定ip和端口
serverSocket.Bind(iPEndPoint);
//设置连接队列长度
serverSocket.Listen(33);
Console.WriteLine("启动监听{0}成功", serverSocket.LocalEn