
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{ Dictionary<string, Socket> d = new Dictionary<string, Socket>();
public Form1()
{
InitializeComponent();
}
Socket so; private void button1_Click(object sender, EventArgs e)
{
string str = this.textBox1.Text.Trim();
Byte[] buf = System.Text.Encoding.UTF8.GetBytes(str);
//Byte[] buffernew = new Byte[buf.Length + 1];
//buffernew[0] = 0;
//int i = 1;
//foreach (byte a in buf)
//{
// buffernew[i] = a;
// i++;
//}
List<byte> list = new List<byte>();
list.Add(0);
list.AddRange(buf);
byte[] buffernew = list.ToArray();
d[comboBox1.SelectedItem.ToString()].Send(buffernew);
//a.Send(buf);
} private void button2_Click(object sender, EventArgs e)
{
string id = this.ip.Text;
int port = Convert.ToInt32(this.port.Text);
IPAddress ip = IPAddress.Parse(id);
IPEndPoint ports = new IPEndPoint(ip,port);
so = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
so.Bind(ports);
so.Listen(10);
Thread d = new Thread(client);
d.IsBackground = true;
d.Start(so); } public void listin(string msg)
{ this.textBox2.AppendText(msg + "\r\n"); } //public void ceshi()
//{
// while (true)
// {
// Socket ce = so.Accept();
// listin(ce.RemoteEndPoint.ToString()+"连接成功");
// Thread ax = new Thread(client);
// ax.Start(ce); // } //} public void client(object o)
{
Socket s = o as Socket; while (true)
{
Socket a = s.Accept();
d.Add(a.RemoteEndPoint.ToString(), a);
this.comboBox1.Items.Add(a.RemoteEndPoint.ToString());
listin(a.RemoteEndPoint.ToString() + "连接成功");
Thread th = new Thread(recevie);
th.IsBackground = false;
th.Start(a); } } void recevie(object o)
{
Socket a = o as Socket;
while (true)
{
Byte[] buffer = new Byte[1024 * 1024 * 3];
int t = a.Receive(buffer);
if (t == 0)
{
break;
}
string str = Encoding.UTF8.GetString(buffer, 0, t);
listin(a.RemoteEndPoint.ToString() + ":" + str);
}
} private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
} private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();//创建对话框
open.InitialDirectory = @"C:\Documents and Settings\All Users\桌面"; //设置对话框路径
open.Title = "对话框1"; //对话框标题
open.Filter = "所有文件|*.*";
open.Multiselect = true; //多选
open.ShowDialog(); //打开对话框
string paths = open.FileName; //读取文件的全路径
if (paths == "") return;
this.textBox3.Text = paths; } private void button4_Click(object sender, EventArgs e)
{
string path = this.textBox3.Text;
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[1024 * 1024 * 5];
int r = fs.Read(bytes, 0, bytes.Length);
List<byte> list = new List<byte>();
list.Add(1);
list.AddRange(bytes);
byte[] buffernew = list.ToArray();
d[comboBox1.SelectedItem.ToString()].Send(buffernew,0,r+1,SocketFlags.None);
}
} private void button5_Click(object sender, EventArgs e)
{
byte[] buf = new byte[1];
buf[0] = 2;
List<byte> list = new List<byte>();
list.Add(2);
list.AddRange(buf);
byte[] buffernew = list.ToArray();
d[comboBox1.SelectedItem.ToString()].Send(buffernew);
}
}
}