这是服务端的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using System.Net.Sockets;
namespace TcpServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//在多线程编程中,如果子线程需要使用主线程中创建的对象
CheckForIllegalCrossThreadCalls = false;
}
Dictionary<string, Socket> clientList = new Dictionary<string, Socket>();
private void Setserver()
{
//1.创建服务器端电话
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
//2.创建手机卡
IPAddress iP = IPAddress.Parse(textBox1.Text);
IPEndPoint endPoint = new IPEndPoint(iP, int.Parse(textBox2.Text));
//3.将电话卡插入到电话中
server.Bind(endPoint);
//4.开始监听电话
//同一时刻允许同时加入链接的最大数量
server.Listen(20);
textBox3.Text = "服务器已经成功开启!\r\n" + textBox3.Text;
//5.等待来电接电话
while (true)
{
//接受接入的一个客户端
connectClient = server.Accept();
if (connectClient != null)
{
string info = connectClient.RemoteEndPoint.ToString();
clientList.Add(info, connectClient);
textBox3.Text = info + "加入服务器!\r\n" + textBox3.Text;
string msg = DateTime.Now + "你已经成功进入聊天室!";
SendMsg(msg);
//每有一个客户端接入,需要有一个线程进行服务
Thread threadclient = new Thread(ReciveMsg);
threadclient.IsBackground = true;
//设置这个线程中的通信对象时对应的socket与客户端socket进行通信
threadclient.Start(connectClient);
}
}
}
void ReciveMsg(object o)
{
Socket client = o as Socket;
while (true)
{
try
{
byte[] arrMsg = new byte[1024 * 1024];
int length = client.Receive(arrMsg);
if (length > 0)
{
string recMsg = Encoding.UTF8.GetString(arrMsg, 0, length);
IPEndPoint endPoint = client.RemoteEndPoint as IPEndPoint;
textBox3.Text = DateTime.Now + "[" + endPoint.Port.ToString() + "]" + recMsg + "\r\n" + textBox3.Text;
SendMsg("[" + endPoint.Port.ToString() + "]" + recMsg);
}
}
catch (Exception)
{
client.Close();
clientList.Remove(client.RemoteEndPoint.ToString());
}
}
}
Socket connectClient;
void SendMsg(string str)
{
foreach (var item in clientList)
{
byte[] arrMsg = Encoding.UTF8.GetBytes(str);
item.Value.Send(arrMsg);
}
}
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(Setserver);
thread.IsBackground = true;
thread.Start();
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox4.Text != null)
{
SendMsg(DateTime.Now + textBox4.Text);
textBox4.Text = "";
}
}
}
}
--------------------------------------------------------以下是客户端代码----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace TcpClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
Socket client;
private void button2_Click(object sender, EventArgs e)
{
if (textBox4.Text != "")
{
SendMsg(textBox4.Text);
textBox4.Text = "";
}
}
private void button1_Click(object sender, EventArgs e)
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
IPAddress ip = IPAddress.Parse(textBox1.Text);
IPEndPoint endpoint = new IPEndPoint(ip, int.Parse(textBox2.Text));
client.Connect(endpoint);
Thread thread = new Thread(ReciveMsg);
thread.IsBackground = true;
thread.Start(client);
}
/// <summary>
/// 接受数据
/// </summary>
/// <param name="o"></param>
void ReciveMsg(object o)
{
client = o as Socket;
while (true)
{
byte[] arrlist = new byte[1024 * 1024];
int length = client.Receive(arrlist);
string msg = DateTime.Now + Encoding.UTF8.GetString(arrlist, 0, length);
textBox3.Text = msg + "\r\n" + textBox3.Text;
}
}
void SendMsg(string msg)
{
byte[] arrmsg = Encoding.UTF8.GetBytes(msg);
client.Send(arrmsg);
}
}
}
最终结果
转载:https://blog.csdn.net/huang4998802/article/details/105430847
查看评论