Server
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Net;
Using System.Net.Sockets;
Using System.Threading;
Namespace Multithreadserv
{
Class Threadtcpserver
{
Private Socket server;
private int usernum;//number of online customers
private int socketnum;//number of connection service segments
Private socket[] Socketuser = new socket[40];//store online client
Send the customer service end nowclient information data, sent to other online each client, not including nowclient customer service side.
private void Sendalluser (byte[] data, Socket nowclient)
{
if (Usernum > 0)
{
for (int i = 0; i < socketnum; i++)
{
if (Socketuser[i]. Equals (nowclient)) continue;
Exceptions may occur when some customers are offline during the sending process
Try
{
Socketuser[i]. Send (data);
}
Catch
{
When an exception is found, the description socket[i] is offline and needs to be removed from the socketuser[] array
Socket temp = socketuser[i];
Socketuser[i] = socketuser[socketnum-1];
i--;
socketnum--;
}
}
}
}
Methods of receiving information from client clients
private void Receivedata (object client)
{
Socket nowclient = (socket) client;
while (true)
{
int res = 0;
byte[] bytes = new byte[1024];
No segment of the receiving client sends information when the client is offline and exits.
Try
{
res = nowclient.receive (bytes);
}
Catch
{
Client offline, update Usernum value
usernum--;
Console.WriteLine ("now has: {0} customers in the connection. ", Usernum);
Return
}
String str = Encoding.UTF8.GetString (bytes, 0, res);
byte[] data = Encoding.UTF8.GetBytes (str);
Send this information to other clients
Sendalluser (data, nowclient);
}
}
Listening on-line clients
private void Accpetuser ()
{
while (true)
{
When you hear a client on-line, update socketuser[] and open a thread that accepts information to this client
Socket nowclient = server. Accept ();
socketuser[socketnum++] = nowclient;
usernum++;
Console.WriteLine ("now has: {0} customers in the connection. ", Usernum);
Open a thread and accept the message from the nowclient.
The method called here is a method with arguments, and a delegate is required.
Thread nowthread = new Thread (new Parameterizedthreadstart (Receivedata));
Nowthread.isbackground = true;
Nowthread.start (nowclient);
}
}
constructor function
Public Threadtcpserver ()
{
Usernum = 0;
Socketnum = 0;
Initialize IP address
IPAddress local = Ipaddress.parse ("192.168.1.101");//IPAddress address of the client
IPEndPoint IEP = new IPEndPoint (local, 3000);//Port 3000
Server = new Socket (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp);
Binding a socket to a local endpoint
Server. Bind (IEP);
Listening on a local 3000 port line
Server. Listen (20);
Console.WriteLine ("Waiting for client level to connect ...");
Accpetuser ();
}
static void Main (string[] args)
{
Threadtcpserver instance = new Threadtcpserver ();
}
}
}
Client:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Text;
Using System.Net;
Using System.Net.Sockets;
Using System.Threading;
Namespace Multithreadclient
{
Class Clientsocket
{
public string names;
public Socket client;
Public Clientsocket ()
{
client = null;
}
public void Receivedata ()
{
byte[] data = new byte[1024];
while (true)
{
int res = 0;
Try
{
res = client. Receive (data);
}
Catch
{
Console.WriteLine ("Disconnect from the server! ");
Return
}
Console.WriteLine (Encoding.UTF8.GetString (data, 0, res));
Console.WriteLine ();
}
}
static void Main (string[] args)
{
byte[] buf = new byte[1024];
Clientsocket ads = new Clientsocket ();
IPAddress local = Ipaddress.parse ("192.168.1.101");//Ipaddres address of the server
IPEndPoint IEP = new IPEndPoint (local, 3000);
Try
{
Ads.client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp);
Ads.client.Connect (IEP);
}
catch (SocketException)
{
Console.WriteLine ("Unable to connect to server");
Console.ReadLine ();
Return
}
Finally
{
}
Console.Write ("Connection is successful, please enter a nickname:");
String names = Console.ReadLine ();
Thread newthread = new Thread (new ThreadStart (ADS). Receivedata));
Newthread.start ();
Names + = "say:";
while (true)
{
Enter a message on the console
Console.WriteLine ("I Want to say:");
Console.WriteLine ();
string input = Console.ReadLine ();
input = names + input;
Ads.client.Send (Encoding.UTF8.GetBytes (input));
}
Console.WriteLine ("Disconnect from server ...");
Ads.client.Close ();
Console.ReadLine ();
}
}
}
Server and Client