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();
//不检测跨线程操作的合法性
Control.CheckForIllegalCrossThreadCalls = false;
}
//当多用户连接到同一个服务器的时候,记录通信点与对应通信的socket,以字典的键值对来保存,方便调用,这样服务器就可以选择的来给不同的用户发送消息了
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)
{
//ip地址
IPAddress ip = IPAddress.Parse(txtServer.Text);
//当前计算机上任何可用的ip地址
//IPAddress ip = IPAddress.Any;
IPEndPoint point = new IPEndPoint(ip, int.Parse(txtPort.Text));
//创建负责监听的socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
//绑定ip和端口号
socket.Bind(point);
//监听
socket.Listen(10);
ShowMsg("开始监听");
}
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++;
//创建一个负责通信用的socket 阻塞窗体的运行
Socket connSocket = socket.Accept();
string s = connSocket.RemoteEndPoint.ToString();
ShowMsg(s + ":连接成功");
//记录通信用的socket
dicSocket.Add(s, connSocket);
cboUsers.Items.Add(s);
//接收消息
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
{
//接收客户端发送过来的消息
byte[] buffer = new byte[1024 * 1024];
//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");
}
//服务器发送消息
private void btnSend_Click(object sender, EventArgs e)
{
//获取文本框内容 转化成字节数组
byte[] buffer = Encoding.UTF8.GetBytes(txtMsg.Text);
try
{
//List<byte> list = new List<byte>();
////标志 0 文字
//list.Add(0);
//list.AddRange(buffer);
Common.MyCommon my = new Common.MyCommon();
my.Flag = 0;
my.Buffer = buffer;
//客户端的通信用的socket
//获取当前选中的客户端的ip地址
string s = cboUsers.Text;
dicSocket[s].Send(Serialize(my));
}
catch (Exception ex)
{
ShowMsg(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
//选择文件
private void btnSelect_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtPath.Text = ofd.FileName;
}
}
//发送文件
private void btnSendFile_Click(object sender, EventArgs e)
{
string key = cboUsers.Text;
using (FileStream fs = new FileStream(txtPath.Text,FileMode.Open))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer,0,buffer.Length);
//List<byte> list = new List<byte>();
////标志 1 文件
//list.Add(1);
//list.AddRange(buffer);
Common.MyCommon my = new Common.MyCommon();
my.Flag = 1;
my.Buffer = buffer;
dicSocket[key].Send(Serialize(my));
}
}
//震动
private void btnZD_Click(object sender, EventArgs e)
{
string key = cboUsers.Text;
//byte[] buffer = new byte[1];
////标志 震动
//buffer[0] = 2;
Common.MyCommon my = new Common.MyCommon();
my.Flag = 2;
dicSocket[key].Send(Serialize(my));
}
//序列化,我想把这个对象转换成一个数组,那就传进去对象给我返回数组吧
byte[] Serialize(Common.MyCommon my)
{
//先把这个对象转换成一个流,再把这个流转换成一个字节数组,这个流只是一个中转流,用内存流就可以了
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream,my);
//把序列化好的流转换成数组
byte[] buffer = stream.ToArray();
//返回出字节数组
return buffer;
}
}
}
}