服務(wù)端
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];
//初始化服務(wù)器
static void InitServer()
{
Console.WriteLine("InitServer");
//實(shí)例化服務(wù)器
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//IPAddress.Any IPAddress.Parse("192.168.0.147")
//實(shí)例化一個(gè)IP端口對(duì)象
//IPAddress.Any是自動(dòng)匹配本機(jī)的IP
IPEndPoint ep = new IPEndPoint(IPAddress.Any, PORT);
//綁定IP端口
serverSocket.Bind(ep);
//設(shè)置客戶(hù)端連接數(shù)量
serverSocket.Listen(100);
}
//監(jiān)聽(tīng)客戶(hù)端的方法
static void AcceptClientSocket()
{
//監(jiān)聽(tīng)客戶(hù)端的變化,一直等待客戶(hù)端的鏈接
//如果有客戶(hù)端鏈接會(huì)終止等待跳轉(zhuǎn)到 AcceptClientCallBack回掉函數(shù)里面去
serverSocket.BeginAccept(AcceptClientCallBack, null);
}
//客戶(hù)端連接回調(diào)函數(shù)
static void AcceptClientCallBack(IAsyncResult ar)
{
Socket socket;
//必須要終,才能重新開(kāi)啟一個(gè)新的連接
try
{
socket = serverSocket.EndAccept(ar);
}
catch (ObjectDisposedException)
{
return;
}
//接收客戶(hù)端的消息
socket.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, socket);
Console.WriteLine("有新的客戶(hù)端連接");
//再次重新開(kāi)啟一個(gè)新的連接
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);
//繼續(xù)監(jiān)聽(tīng)消息
current.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current);
}
static void Main(string[] args)
{
//初始化
InitServer();
//接收新的客戶(hù)端
AcceptClientSocket();
Console.ReadLine();
}
}
}
客戶(hù)端
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);
}
}
}