Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Net;
Using System.Net.Sockets;
Using System.Threading;
Using System.IO;
Using System.Runtime.Serialization.Formatters.Binary;
Namespace Mychatserver
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
Do not detect the legality of cross-threading operations
Control.checkforillegalcrossthreadcalls = false;
}
When multiple users connect to the same server, record communication points and correspondence socket, with the dictionary key value pair to save, convenient to call, so that the server can choose to send messages to different users
dictionary<string, socket> dicsocket = new dictionary<string, socket> ();
dictionary<string, thread> dicthread = new dictionary<string, thread> ();
private void Btnstart_click_1 (object sender, EventArgs e)
& nbsp; {
// IP address
ipaddress IP = ipaddress.parse ( Txtserver.text);
//any available IP address on the current computer
//ipaddress IP = ipaddress.any;
IPEndPoint point = new IPEndPoint (IP, int. Parse (Txtport.text));
//create socket for listening
SOCKET socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, PROTOCOLTYPE.TCP);
Try
{
//Binding IP and port number
socket. Bind (point);
//Listening
socket. Listen (10);
ShowMsg ("Start listening");
}
catch (Exception ex)
{
ShowMsg (ex. message);
Return
}
Thread th = new Thread (Listen);
Th. IsBackground = true;
Th. Start (socket);
}
int count = 0;
void Listen (Object o)
{
Socket socket = o as socket;
while (count<100)
{
Try
{
count++;
Create a socket that is responsible for communication blocking the operation of the form
Socket Connsocket = socket. Accept ();
string s = connSocket.RemoteEndPoint.ToString ();
ShowMsg (S + ": Connection Success");
To record the socket used for communication
Dicsocket.add (S, connsocket);
CBOUSERS.ITEMS.ADD (s);
//Receive Message
thread th = new Thread (RECMSG);
th. IsBackground = true;
th. Start (Connsocket);
Dicthread.add (s, th);
}
catch (Exception Ex)
{
ShowMsg (ex. message);
break;
}
}
}
void Recmsg (Object o)
{
Socket connsocket = o as Socket;
while (true)
{
Try
{
Receive messages sent by client
byte[] buffer = new byte[1024 * 1024];
Number of actual valid bytes received by num
int num = connsocket.receive (buffer);
string s = Encoding.UTF8.GetString (buffer, 0, num);
ShowMsg (connSocket.RemoteEndPoint.ToString () + ":" + s);
}
catch (Exception ex)
{
ShowMsg (ex. message);
Break
}
}
}
void ShowMsg (String msg)
{
Txtlog.appendtext (msg + "RN");
}
Server sends messages
private void Btnsend_click (object sender, EventArgs e)
{
Gets the text box contents into a byte array
byte[] buffer = Encoding.UTF8.GetBytes (Txtmsg.text);
Try
{
list<byte> list = new list<byte> ();
Sign 0 text
List. ADD (0);
List. AddRange (buffer);
Common.mycommon my = new Common.mycommon ();
My. Flag = 0;
My. buffer = buffer;
Sockets for communication with the client
Gets the IP address of the currently selected client
string s = cbousers.text;
Dicsocket[s]. Send (Serialize (my));
}
catch (Exception ex)
{
ShowMsg (ex. message);
}
}
private void Form1_Load (object sender, EventArgs e)
{
}
Select File
private void Btnselect_click (object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog ();
if (OFD. ShowDialog () = = System.Windows.Forms.DialogResult.OK)
{
Txtpath.text = ofd. FileName;
}
}
//Send file
private void Btnsendfile_click (object sender, EventArgs e)
{
string key = Cbousers.text;
using (FileStream fs = new FileStream ( Txtpath.text,filemode.open)
{
& nbsp; byte[] buffer = new BYTE[FS. Length];
FS. Read (Buffer,0,buffer. Length);
list<byte> list = new list<byte> ();
Sign 1 file
List. ADD (1);
List. AddRange (buffer);
Common.mycommon my = new Common.mycommon ();
My. Flag = 1;
My. buffer = buffer;
Dicsocket[key]. Send (Serialize (my));
}
}
//vibration
private void Btnzd_click (object sender, EventArgs e)
{
string key = Cbousers.text;
byte[] buffer = new BYTE[1];
Sign Vibration
Buffer[0] = 2;
Common.mycommon my = new Common.mycommon ();
My. Flag = 2;
Dicsocket[key]. Send (Serialize (my));
}
serialization, I want to convert this object into an array, and then I'm going to get the object back to the array.
Byte[] Serialize (Common.mycommon my)
{
First convert this object into a stream, and then change the flow to a byte array, this stream is only a transit stream, with the memory stream can be
using (MemoryStream stream = new MemoryStream ())
{
BinaryFormatter bf = new BinaryFormatter ();
Bf. Serialize (stream,my);
To replace a serialized flow with an array.
byte[] buffer = stream. ToArray ();
Returns a byte array
return buffer;
}
}
}
}