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