public void loadform() { client = new Client_TCP(); client.OnConnectFail += client_OnConnectTimeout; client.OnConnectSuccess += client_OnConnectSuccess; client.OnDropped += client_OnDisconnect; client.OnServerDisconnect += client_OnRemoteDisconnect; client.OnReceive += clCMD; list.Columns.Add("IP"); list.Columns.Add("Name"); list.Columns.Add("NO"); list.Rows.Add("172.60.100.222", "彪哥", "-1"); list.Rows.Add("172.16.132.86", "振宇", "-1"); list.Rows.Add("172.60.102.217", "通通", "-1"); list.Rows.Add("172.60.102.80", "邱邱", "-1"); list.Rows.Add("172.60.102.29", "杉杉", "-1"); list.Rows.Add("172.16.132.12", "帅杰", "-1"); list.Rows.Add("172.60.102.165", "丽丽", "-1"); list.Rows.Add("127.0.0.1", "本地", "-1"); for (int i = 0; i < list.Rows.Count; i++) { comboBox1.Items.Add(list.Rows[i][1].ToString() + ":" + list.Rows[i][0].ToString()); } }
public static void Send_TCP(IPAddress ip, TcpData data) { new Thread(() => { var client = new Client_TCP(Setting.BUFFER_SIZE); client.SendData(ip, Setting.DATA_PORT, data.ToBytes(), (int)data.ActionType); }) { IsBackground = true } .Start(); }
private void AcceptCallBack(IAsyncResult ar) { Client_TCP temp_client = new Client_TCP(); try { temp_client.socket = Listener.EndAccept(ar); } catch { Listener.Dispose(); return; } Listener.BeginAccept(new AsyncCallback(AcceptCallBack), Listener); if (OnClientsReceiveMsg != null) { temp_client.OnReceiveMsg += OnClientsReceiveMsg; } Clients.Add(temp_client); temp_client.Receive(); if (OnClientDisconnect != null) { temp_client.OnDisconnect += OnClientDisconnect; } if (OnListenSuccess != null) { OnListenSuccess(); } if(OnAcceptNewClient != null) { OnAcceptNewClient(temp_client); } }
public void Send(Client_TCP client, string msg) { if(client.socket.Connected) { client.Send(msg); } else { client.Shutdown(); } }
public void Client_Remote(Client_TCP client) { foreach(Client_TCP temp in Clients) { if(temp == client) { if (client.socket.Connected) { client.Shutdown(); } Clients.Remove(temp); return; } } }
private void AcceptCallBack(IAsyncResult ar) { try { Client_TCP temp_client = new Client_TCP(Listener.EndAccept(ar)); Listener.BeginAccept(new AsyncCallback(AcceptCallBack), Listener); if (OnRemoteDisconnect != null) { temp_client.OnRemoteDisconnect += OnRemoteDisconnect; } if (OnClientsReceive != null) { temp_client.OnReceive += OnClientsReceive; } if (OnDropped != null) { temp_client.OnDropped += OnDropped; } Clients.Add(temp_client); if (OnAcceptNewClient != null) { OnAcceptNewClient(temp_client); } temp_client.Receive(); } catch { Listener.BeginAccept(new AsyncCallback(AcceptCallBack), Listener); if (OnAcceptClientFail != null) { OnAcceptClientFail(); } } }
public void RemoveClient(Client_TCP client) { Clients.Remove(client); client.Shutdown(); }
public void client_OnDisconnect(Client_TCP Client) { Invoke(new EventHandler(delegate { dataGridView1.Rows.Clear(); dataGridView2.Rows.Clear(); textBox1.ReadOnly = false; comboBox1.Enabled = true; button_flash.Text = "连接"; button1.Text = "作为服务器"; button_Reset.Enabled = false; comboBox1.Enabled = true; textBox1.ReadOnly = false; button_roll.Enabled = false; button_flash.Enabled = true; })); //server.StopListen(); MessageBox.Show("连接断开,请重新连接。", "提示"); }
private void Temp_client_OnServerDisconnect(Client_TCP client) { Clients.Remove(client); if (OnClientDisconnect != null) { OnClientDisconnect(client); } }
private void Temp_client_OnDropped(Client_TCP client) { Clients.Remove(client); if (OnClientDropped != null) { OnClientDropped(client); } }
private void AcceptCallBack(IAsyncResult ar) { try { Client_TCP temp_client = new Client_TCP(Listener.EndAccept(ar)); Listener.BeginAccept(new AsyncCallback(AcceptCallBack), Listener); temp_client.IP = temp_client.Get_RemoteIP(); temp_client.OnServerDisconnect += Temp_client_OnServerDisconnect; temp_client.OnDropped += Temp_client_OnDropped; if (OnReceive != null) { temp_client.OnReceive += OnReceive; } Clients.Add(temp_client); if (OnAddNewClient != null) { OnAddNewClient(temp_client); } temp_client.Receive(); } catch { } }
public void clCMD(Client_TCP client, byte[] ByteArray) { string cmd = ByteArrayToString(ByteArray, Encoding.UTF8); string[] temp = cmd.Split(Split); switch (temp[0]) { case "Come": { Invoke(new EventHandler(delegate { dataGridView2.Rows.Clear(); textBox1.Text = temp[1]; for (int i = 2; i < temp.Length - 1; i++) { dataGridView2.Rows.Add(temp[i], temp[++i]); dataGridView2.ClearSelection(); } })); break; } case "Info"://刷新列表 { Invoke(new EventHandler(delegate { dataGridView1.Rows.Clear(); for (int i = 1; i < temp.Length - 1; i++) { dataGridView1.Rows.Add(temp[i], temp[++i], temp[++i]); } })); break; } case "Roll": { Invoke(new EventHandler(delegate { dataGridView1.Rows.Add(temp[1], temp[2], temp[3]); dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0]; })); break; } case "Reset": { Invoke(new EventHandler(delegate { dataGridView1.Rows.Clear(); })); break; } case "Mess": { Invoke(new EventHandler(delegate { MessageBox.Show(temp[1], "提示"); })); break; } } }
public void seCMD(Client_TCP client, byte[] ByteArray) { string msg = ByteArrayToString(ByteArray, Encoding.UTF8); string[] temp = msg.Split(Split); switch (temp[0]) { case "Info"://刷新列表 { //string ip = ((IPEndPoint)index.socket.RemoteEndPoint).Address.ToString(); name = "无此IP数据"; string ip = client.Get_RemoteIP(); for (int i = 0; i < list.Rows.Count; i++) { if (ip == list.Rows[i][0].ToString()) { name = list.Rows[i][1].ToString(); break; } } string tep = ""; if (dataGridView2.Rows.Count != 0) { for (int i = 0; i < dataGridView2.Rows.Count; i++) { tep += Split + dataGridView2.Rows[i].Cells[0].Value.ToString() + Split + dataGridView2.Rows[i].Cells[1].Value.ToString(); } } server.Multicast(server.Clients, StringToByteArray("Come" + Split + textBox1.Text + tep + Split + name + Split + ip, Encoding.UTF8)); //Thread.Sleep(100); if (dataGridView1.Rows.Count != 0) { string info = "Info" + Split; for (int i = 0; i < dataGridView1.Rows.Count; i++) { info += dataGridView1.Rows[i].Cells[0].Value.ToString() + Split + dataGridView1.Rows[i].Cells[1].Value.ToString() + Split + dataGridView1.Rows[i].Cells[2].Value.ToString() + Split; } //client.Send(info); client.Send(StringToByteArray(info, Encoding.UTF8)); } else { // client.Send("Fail" + Split + "连接成功。"); client.Send(StringToByteArray("Mess" + Split + "连接成功。", Encoding.UTF8)); } break; } case "Roll": { string ip = client.Get_RemoteIP(); Boolean rolled = false; for (int j = 0; j < dataGridView1.Rows.Count; j++) { if (ip == dataGridView1.Rows[j].Cells[0].Value.ToString()) { rolled = true; //index.Send("Fail" + Split + "你已经Roll过点了。"); client.Send(StringToByteArray("Mess" + Split + "你已经Roll过点了。", Encoding.UTF8)); break; } } if (!rolled) { string NO = ro.Next(Int32.Parse(textBox1.Text)).ToString(); for (int i = 0; i < list.Rows.Count; i++) { if (ip == list.Rows[i][0].ToString()) { name = list.Rows[i][1].ToString(); break; } } server.Multicast(server.Clients, StringToByteArray("Roll" + Split + ip + Split + name + Split + NO, Encoding.UTF8)); //server.SendAll("Roll" + Split + ip + Split + name + Split + NO); } break; } } }