服务器模块
sever部分
server2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ConsoleApp8
{
class Sever2
{
private Socket serverSocket;
private List<Socket> clientList;//存储连进来的客户端
//构造函数
public Sever2()
{
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientList = new List<Socket>();//连一个客户端往里面加一个
}
//启动函数
public void Start()
{
serverSocket.Bind(new IPEndPoint(IPAddress.Any, 12305));
serverSocket.Listen(10);
Console.WriteLine("服务器启动成功!");
Thread threadAccept = new Thread(Accept);
threadAccept.IsBackground = true;
threadAccept.Start();
}
private void Accept()
{
//接收客户端的方法,会挂起当前线程
Socket client = serverSocket.Accept();
IPEndPoint ipPoint = client.RemoteEndPoint as IPEndPoint;
Console.WriteLine($"{ipPoint.Address} [{ipPoint.Port}]连接成功!");
clientList.Add(client);
Thread threadReceive = new Thread(Receive);
threadReceive.IsBackground = true;
threadReceive.Start(client);
Accept();
}
private void Receive(object obj)
{
Socket client =obj as Socket;
IPEndPoint ipPoint = client.RemoteEndPoint as IPEndPoint;
try
{
byte[] msg = new byte[1024];
int msgLen = client.Receive(msg);
String msgStr = $"{ipPoint.Address}[{ipPoint.Port}] 说: {Encoding.UTF8.GetString(msg, 0, msgLen)}";
Console.WriteLine(msgStr);
Broadcast(client, msgStr);
//服务器往客户端发消息
//client.Send(Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(msg, 0, msgLen) + "-楼上说的对"));
Receive(client);//尾递归
}
catch
{
string catchStr = $"{ipPoint.Address}[{ipPoint.Port}]断开";
Console.WriteLine(catchStr);
Broadcast(client, catchStr);
clientList.Remove(client);
}
}
private void Broadcast(Socket clientOther, string msg)
{
foreach(var item in clientList)
{
if(item==clientOther)
{
//同一客户端,无需响应
}
else
{
item.Send(Encoding.UTF8.GetBytes(msg));
}
}
}
}
}
主程序部分
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp8
{
class Program
{
static void Main(string[] args)
{
Sever2 sever2 = new Sever2();
sever2.Start();
Console.ReadKey();
}
}
}
客户端模块
client部分
ClientSocket.cs
using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace Client2
{
class ClientSocket
{
private Socket clientSocket;
public ClientSocket()
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//构造函数
}
public void Connect(string ip, int port)
{
clientSocket.Connect(ip,port);
Console.WriteLine("连接服务器成功!");
Thread ReceiveThread = new Thread(Receive);
ReceiveThread.IsBackground = true;
ReceiveThread.Start();
}
private void Receive()
{
try
{
byte[] msg = new byte[1024];
int msgLen = clientSocket.Receive(msg);
Console.WriteLine($"{Encoding.UTF8.GetString(msg, 0, msgLen)}");
Receive();
}
catch
{
Console.WriteLine("服务器断开");
}
}
public void send(string msg)
{
clientSocket.Send(Encoding.UTF8.GetBytes(msg));
}
}
}
主程序部分
Program.cs
using System;
namespace Client2
{
class Program
{
static void Main(string[] args)
{
ClientSocket client2 = new ClientSocket();
client2.Connect("127.0.0.1", 12305);
Console.WriteLine("请输入要发送的内容,输入quit退出");
string msg = Console.ReadLine();
while(msg!="quit")
{
client2.send(msg);
msg = Console.ReadLine();
}
Console.ReadKey();
}
}
}
网友评论