Socket-based client and server chatbots and socket chatbots

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.