服务端
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace MySurver
{
class Program
{
private static Socket serverSocket;
private static int PORT = 1780;
private const int BUFFER_SIZE = 2048;
private static readonly byte[] buffer = new byte[BUFFER_SIZE];
//初始化服务器
static void InitServer()
{
Console.WriteLine("InitServer");
//实例化服务器
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//IPAddress.Any IPAddress.Parse("192.168.0.147")
//实例化一个IP端口对象
//IPAddress.Any是自动匹配本机的IP
IPEndPoint ep = new IPEndPoint(IPAddress.Any, PORT);
//绑定IP端口
serverSocket.Bind(ep);
//设置客户端连接数量
serverSocket.Listen(100);
}
//监听客户端的方法
static void AcceptClientSocket()
{
//监听客户端的变化,一直等待客户端的链接
//如果有客户端链接会终止等待跳转到 AcceptClientCallBack回掉函数里面去
serverSocket.BeginAccept(AcceptClientCallBack, null);
}
//客户端连接回调函数
static void AcceptClientCallBack(IAsyncResult ar)
{
Socket socket;
//必须要终,才能重新开启一个新的连接
try
{
socket = serverSocket.EndAccept(ar);
}
catch (ObjectDisposedException)
{
return;
}
//接收客户端的消息
socket.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, socket);
Console.WriteLine("有新的客户端连接");
//再次重新开启一个新的连接
serverSocket.BeginAccept(AcceptClientCallBack, null);
}
private static void ReceiveCallback(IAsyncResult AR)
{
Socket current = (Socket)AR.AsyncState;
int received;
try
{
received = current.EndReceive(AR);
}
catch (SocketException)
{
Console.WriteLine("Client forcefully disconnected");
current.Close();
return;
}
byte[] recBuf = new byte[received];
Array.Copy(buffer, recBuf, received);
string text = Encoding.ASCII.GetString(recBuf);
Console.WriteLine("Received Text: " + text);
//继续监听消息
current.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current);
}
static void Main(string[] args)
{
//初始化
InitServer();
//接收新的客户端
AcceptClientSocket();
Console.ReadLine();
}
}
}
客户端
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace MyClient
{
class Program
{
private const int BUFFER_SIZE = 2048;
private static byte[] buffer = new byte[BUFFER_SIZE];
private static Socket ClientSocket = new Socket
(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private const int PORT = 1780;
static void Main()
{
Console.Title = "Client";
ConnectToServer();
RequestLoop();
}
private static void ConnectToServer()
{
int attempts = 0;
while (!ClientSocket.Connected)
{
try
{
attempts++;
Console.WriteLine("Connection attempt " + attempts);
ClientSocket.Connect(IPAddress.Parse("192.168.0.137"), PORT);
}
catch (SocketException)
{
Console.Clear();
}
}
Console.Clear();
Console.WriteLine("Connected");
}
private static void RequestLoop()
{
while (true)
{
SendRequest();
}
}
private static void SendRequest()
{
string request = Console.ReadLine();
SendString(request);
}
private static void SendString(string text)
{
byte[] buffer = Encoding.ASCII.GetBytes(text);
ClientSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);
}
}
}