基于socket的客户端和服务端聊天机器人

服务端代码如下:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//对 Windows 窗体控件进行线程安全调用
RichTextBox.CheckForIllegalCrossThreadCalls = false;
}
private Socket serverSocket;
/// <summary>
/// 服务端开启监听
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
int port = 6000;
string host = "127.0.0.1";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket sSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sSocket.Bind(ipe);
sSocket.Listen(100);
ParameterizedThreadStart par = ServerListenMethod;
Thread thread = new Thread(par);
thread.Start(sSocket);
}
public void ServerListenMethod(object sSocket)
{
while (true)
{
Socket serSocket = ((Socket)sSocket).Accept();
ParameterizedThreadStart p = ServerCommunicationMethod;
Thread t = new Thread(p);
t.Start(serSocket);
}
}
public void ServerCommunicationMethod(object sSocket)
{
serverSocket = (Socket)sSocket;
string recStr = "";

//创建内存缓存区
byte[] recByte = new byte[1024 * 1024 * 5];
while (true)
{
int bytes = serverSocket.Receive(recByte, recByte.Length, 0);
recStr = Encoding.Default.GetString(recByte, 0, bytes);
this.richTextBox1.AppendText("服务器端获得信息:" + recStr);
}
}
/// <summary>
/// 服务端发送消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
var str = this.textBox1.Text;
this.textBox1.Text = "";
byte[] sendByte = Encoding.Default.GetBytes(str);
serverSocket.Send(sendByte, sendByte.Length, 0);
this.richTextBox1.AppendText("服务器端发送信息:" + str);
}
}
}

服务端启动如图:基于socket的客户端和服务端聊天机器人

----------------------------------------------------------------------------------------------------------------------

客户端代码如下

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Servers
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//对 Windows 窗体控件进行线程安全调用
RichTextBox.CheckForIllegalCrossThreadCalls = false;
}
private Socket clientSocket;
/// <summary>
/// 客户端发送消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnMsg_Click(object sender, EventArgs e)
{
var str = this.textBox1.Text;
clientSocket.Send(Encoding.Default.GetBytes(str));
this.richTextBox1.AppendText("客户端发送信息:" + str);
this.textBox1.Text = "";
}
/// <summary>
/// 建立连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
int port = 6000;
string host = "127.0.0.1";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(ipe);
ThreadStart start = ClientListenMethod;
Thread t = new Thread(start);
t.Start();
}
public void ClientListenMethod()
{

//创建内存缓存区
byte[] recByte = new byte[1024 * 1024 * 5];
while (true)
{
int bytes = clientSocket.Receive(recByte, recByte.Length, 0);
string recStr = Encoding.Default.GetString(recByte, 0, bytes);
this.richTextBox1.AppendText("客户端端获得信息:" + recStr);
}
}
}
}

客户端启动如图:基于socket的客户端和服务端聊天机器人

注:如果需要项目源代码,可以发1870902607@qq.com邮箱告诉我,我会将项目源代码发给你。

----分享不仅帮助了别人,同时也提升了自己。luchao_it

上一篇:Netty入门——客户端与服务端通信


下一篇:由于检索用户的本地应用程序数据路径时出错,导致无法生成 SQL Server 的用户实例