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.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace Server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void btnStart_Click(object sender, EventArgs e)
{
IPAddress ip = IPAddress.Parse(txtServer.Text);
//網絡斷點 ip地址和端口號
IPEndPoint point = new IPEndPoint(ip, int.Parse(txtPort.Text));
//創建負責監聽用的socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
//綁定網絡斷點
socket.Bind(point);
//監聽 10 連接隊列的長度
socket.Listen(10);
SetTxt("開始監聽");
Thread th = new Thread(Listen);//開啟監聽的線程
th.IsBackground = true;
th.Start(socket);
}
catch (Exception ex)
{
SetTxt(ex.Message);
}
}
Dictionary<string, Socket> dic = new Dictionary<string, Socket>();
void Listen(object o)
{
//負責監聽用的socket
Socket socket = o as Socket;
while (true)
{
try
{
//創建負責通信用的socket
Socket connSocket = socket.Accept();
//獲取客戶端ip和端口
string ipStr = connSocket.RemoteEndPoint.ToString();
SetTxt(ipStr + ":連接成功");
//填充下拉框
cboUsers.Items.Add(ipStr);
//
dic.Add(ipStr, connSocket);
//開啟線程接收消息
Thread th = new Thread(RecMsg);
th.IsBackground = true;
th.Start(connSocket);
}
catch (Exception ex)
{
SetTxt(ex.Message);
}
}
}
void RecMsg(object o)
{
Socket connSocket = o as Socket;
byte[] buffer = new byte[1024 * 1024 * 5];
while (true)
{
try
{
//count實際收到的字節個數
int count = connSocket.Receive(buffer);
string ipStr = connSocket.RemoteEndPoint.ToString();
if (count == 0)
{
SetTxt(ipStr + ":斷開連接");
connSocket.Shutdown(SocketShutdown.Both);
connSocket.Close();
break;
}
string msg = Encoding.UTF8.GetString(buffer, 0, count);
SetTxt(ipStr + ":" + msg);
}
catch (Exception ex)
{
SetTxt(ex.Message);
break;
}
}
}
void SetTxt(string t)
{
txtLog.AppendText(t + "\r\n");
}
//發送文字消息
private void btnSend_Click(object sender, EventArgs e)
{
if (cboUsers.SelectedIndex > -1)
{
string key = cboUsers.Text;
byte[] buffer = Encoding.UTF8.GetBytes(txtMsg.Text);
List<byte> list = new List<byte>();
list.Add(1); //協議 1 文字
list.AddRange(buffer);
dic[key].Send(list.ToArray());
}
else
{
MessageBox.Show("請選擇客戶端");
}
}
//選擇路徑
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)
{
if (cboUsers.SelectedIndex > -1)
{
string key = cboUsers.Text;
//dic[key]
//判斷文件是否存在
if (txtPath.Text.Length > 0)
{
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>();
list.Add(2 ); //協議 2 文件
list.AddRange(buffer);
dic[key].Send(list.ToArray());
}
}
else
{
MessageBox.Show("請選擇文件");
}
}
else
{
MessageBox.Show("請選擇客戶端");
}
}
//震動
private void btnZD_Click(object sender, EventArgs e)
{
if (cboUsers.SelectedIndex > -1)
{
string key = cboUsers.Text;
byte[] buffer = new byte[1];
buffer[0] = 3; //協議 3 震動
dic[key].Send(buffer);
}
else
{
MessageBox.Show("請選擇客戶端");
}
}
}
}