Socket-based client and server chatbots and socket chatbots
The server code is as follows:
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 ();
// Perform thread-safe calls on Windows Forms controls
RichTextBox. checkforillegalcrossthreadcils = false;
}
Private Socket serverSocket;
/// <Summary>
/// Enable the listener on the server
/// </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 = "";
// Create a memory cache Zone
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 ("server-side information:" + recStr );
}
}
/// <Summary>
/// The server sends the message
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button#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 ("server-side message:" + str );
}
}
}
Server startup
Bytes ----------------------------------------------------------------------------------------------------------------------
The client code is as follows::
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 ();
// Perform thread-safe calls on Windows Forms controls
RichTextBox. checkforillegalcrossthreadcils = false;
}
Private Socket clientSocket;
/// <Summary>
/// Send a message from the client
/// </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 ("client sending message:" + str );
This. textBox1.Text = "";
}
/// <Summary>
/// Establish a connection
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button#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 ()
{
// Create a memory cache Zone
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 ("client-side information:" + recStr );
}
}
}
}
Start the client
Note: If you need the project source code, you can send the 1870902607@qq.com mailbox to tell me, I will send the project source code to you.
---- Sharing not only helps others, but also improves themselves. Luchao_it