Socket-based client and server-side chat Bots

Source: Internet
Author: User

The service-side 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 ();
To make a thread-safe call to a Windows forms control
Richtextbox.checkforillegalcrossthreadcalls = false;
}
Private Socket ServerSocket;
<summary>
Service-side turn on monitoring
</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 memory buffers
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 Access information:" + recstr);
}
}
<summary>
Server sends messages
</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 ("Server-side Send message:" + str);
}
}
}

Service-side startup

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

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 ();
To make a thread-safe call to a Windows forms control
Richtextbox.checkforillegalcrossthreadcalls = false;
}
Private Socket Clientsocket;
<summary>
Client sends a message
</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 sends message:" + str);
This.textBox1.Text = "";
}
<summary>
Establish a connection
</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 ()
{

Create memory buffers
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 access to information:" + recstr);
}
}
}
}

Client Startup

Note: If you need project source code, you can send [email protected] email me, I will send you the project source code.

----sharing not only helps others, but also promotes themselves. Luchao_it

Socket-based client and server-side chat Bots

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.