void receive() { try { byte[] bByte = new byte[Marshal.SizeOf(__user)]; byte[] bInt = new byte[4]; byte[] bUshort = new byte[2]; byte[] bmsg = new byte[4]; while (connect_flag) { string receive_data = ""; int arr_idx = 0; if (0 < ns.Read(bByte, 0, Marshal.SizeOf(typeof(_User)))) { ushort us_header = 0; ushort us_s_header = 0; int id; string rcv_str; int target_player_hp; Array.Copy(bByte, arr_idx, bUshort, 0, 2); arr_idx += 2; us_header = byte2UShort(bUshort); switch (us_header) { case (ushort)e_Packet_header.P_MOVE_POS: Array.Copy(bByte, arr_idx, bUshort, 0, 2); arr_idx += 2; us_s_header = byte2UShort(bUshort); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __user.x = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __user.y = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __user.zone = byte2Int(bInt); if (us_s_header == (ushort)e_Packet_move_pos.P_S_MOVE_RAND_TELEPORT) { for (int i = 0; i < 1024; i++) { __monster_list[i].on_off = false; __other_player_list[i].on_off = false; } } receive_data = "move position user!! " + "X: " + __user.x.ToString() + ", Y: " + __user.y.ToString(); break; case (ushort)e_Packet_header.P_SEND_MSG: Array.Copy(bByte, arr_idx, bmsg, 0, 4); rcv_str = Encoding.Default.GetString(bmsg); // byte -> string receive_data = "sent message to other user !!"; break; case (ushort)e_Packet_header.P_BROAD_CAST_MSG: Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; id = byte2Int(bInt); Array.Copy(bByte, arr_idx, bmsg, 0, 4); rcv_str = Encoding.Default.GetString(bmsg); // byte -> string receive_data = "Notify[" + id.ToString() + "]" + ": " + rcv_str; break; case (ushort)e_Packet_header.P_SCAN_MONSTER_ON_SAME_ZONE: Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; id = byte2Int(bInt); __monster_list[id].id = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __monster_list[id].zone = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __monster_list[id].x = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __monster_list[id].y = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __monster_list[id].state.hp = byte2Int(bInt); if (__monster_list[id].state.hp > 0) { __monster_list[id].on_off = true; } else { __monster_list[id].on_off = false; this.Invalidate(); } receive_data = "Incoming monster[" + id.ToString() + "]" + " zone: " + __monster_list[id].zone.ToString() + " X: " + __monster_list[id].x.ToString() + ", Y: " + __monster_list[id].y.ToString() + " HP: " + __monster_list[id].state.hp.ToString(); break; case (ushort)e_Packet_header.P_SCAN_OTHER_PLAYER_ON_SAME_ZONE: Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; id = byte2Int(bInt); __other_player_list[id].id = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __other_player_list[id].zone = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __other_player_list[id].x = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __other_player_list[id].y = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __other_player_list[id].state.hp = byte2Int(bInt); if (__other_player_list[id].state.hp > 0) { __other_player_list[id].on_off = true; } else { __other_player_list[id].on_off = false; this.Invalidate(); } receive_data = "find other player in same zone[" + id.ToString() + "]"; break; case (ushort)e_Packet_header.P_USER_ACTIVITY: Array.Copy(bByte, arr_idx, bUshort, 0, 2); arr_idx += 2; us_s_header = byte2UShort(bUshort); if (us_s_header == (ushort)e_Packet_user_activity_pos.P_S_ACTIVITY_ATTACK_TO_MONSTER) { Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; id = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __monster_list[id].state.hp = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __user.state.hp = byte2Int(bInt); Array.Copy(bByte, arr_idx, bUshort, 0, 2); arr_idx += 2; __user.state.level = byte2UShort(bUshort); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __user.state.experience = byte2Int(bInt); if (__monster_list[id].state.hp > 0) { __monster_list[id].on_off = true; } else { __monster_list[id].on_off = false; this.Invalidate(); } /* displayer received data */ receive_data = "Attacked! Monster ID [" + id.ToString() + "] \r\n"; receive_data += "Monster HP [" + __monster_list[id].state.hp.ToString() + "] \r\n"; receive_data += "Your HP [" + __user.state.hp.ToString() + "]\r\n"; receive_data += "Your LV [" + __user.state.level.ToString() + "]\r\n"; receive_data += "Your ExP [" + __user.state.experience.ToString() + "]"; } else if (us_s_header == (ushort)e_Packet_user_activity_pos.P_S_ACTIVITY_ATTACK_TO_PLAYER) { Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; id = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; target_player_hp = byte2Int(bInt); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __user.state.hp = byte2Int(bInt); Array.Copy(bByte, arr_idx, bUshort, 0, 2); arr_idx += 2; __user.state.level = byte2UShort(bUshort); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __user.state.experience = byte2Int(bInt); /* displayer received data */ receive_data = "Attacked! Player ID [" + id.ToString() + "] \r\n"; receive_data += "Target Player HP [" + target_player_hp.ToString() + "] \r\n"; receive_data += "Your HP [" + __user.state.hp.ToString() + "]\r\n"; receive_data += "Your LV [" + __user.state.level.ToString() + "]\r\n"; receive_data += "Your ExP [" + __user.state.experience.ToString() + "]"; } else if (us_s_header == (ushort)e_Packet_user_activity_pos.P_S_ACTIVITY_SKILL) { Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __user.state.mp = byte2Int(bInt); Array.Copy(bByte, arr_idx, bUshort, 0, 2); arr_idx += 2; __user.state.level = byte2UShort(bUshort); Array.Copy(bByte, arr_idx, bInt, 0, 4); arr_idx += 4; __user.state.experience = byte2Int(bInt); /* displayer received data */ receive_data = "Invoke Skill!"; } break; default: receive_data = ""; break; } #if false //string receive_data = streader.ReadLine(); string receive_data; ns.Read(bByte, 0, Marshal.SizeOf(typeof(_User))); GCHandle handle = GCHandle.Alloc(bByte, GCHandleType.Pinned); __user = (_User)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(_User)); handle.Free(); switch ((e_Packet_header)__user.p_h) { case e_Packet_header.P_SEND_MSG: break; case e_Packet_header.P_MOVE_POS: break; case e_Packet_header.P_BROAD_CAST_MSG: break; case e_Packet_header.P_MAX: break; } receive_data = "[M]" + __user.id.ToString() + ": " + __user.zone.ToString() + " X:" + __user.x.ToString() + " Y:" + __user.y.ToString() + "[" + __user.msg + "]"; #endif if (receive_data != null) { tb_recevie_text(receive_data, 7777); //stringData = Encoding.ASCII.GetString(data, 0, recv); } } } } catch (IOException) { if (connect_flag) { _thread_receive = new Thread(receive); _thread_receive.Start(); } else { connect_flag = false; } } catch (Exception ex) { connect_flag = false; //MessageBox.Show(ex.ToString()); textBox1.AppendText(ex.ToString() + "\r\n"); /* or Environment.NewLine */ } }
public tcp_client() { byte[] data = new byte[1024]; InitializeComponent(); try { Assembly assem = this.GetType().Assembly; bm_hero = new Bitmap(tcp_client_new.Properties.Resources.hero); bm_gong = new Bitmap(tcp_client_new.Properties.Resources.gong); for (int i = 0; i < 1024; i++) { __monster_list[i].on_off = false; __other_player_list[i].on_off = false; } if (!connect_flag) { this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true); this.UpdateStyles(); #if true ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4000); server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); server.Connect(ipep); ns = new NetworkStream(server); #else server = new TcpClient("127.0.0.1", 4000); ns = server.GetStream(); #endif streader = new StreamReader(ns); stwriter = new StreamWriter(ns); //server.ReceiveTimeout = 1000; // Time Out = 1s string receive_data; byte[] bByte = new byte[Marshal.SizeOf(__user)]; ns.Read(bByte, 0, Marshal.SizeOf(typeof(_User))); GCHandle handle = GCHandle.Alloc(bByte, GCHandleType.Pinned); __user = (_User)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(_User)); handle.Free(); receive_data = "[Joined]" + __user.id.ToString() + ": " + __user.zone.ToString() + " X:" + __user.x.ToString() + " Y:" + __user.y.ToString() + "[" + __user.msg + "]"; _thread_receive = new Thread(receive); _thread_receive.Start(); connect_flag = true; textBox1.Font = new Font("Tahoma", 8, FontStyle.Bold); textBox1.Text += ("connected server" + Environment.NewLine); textBox1.Text += (receive_data + Environment.NewLine); richTextBox1.Text = ""; richTextBox1.Font = new Font("Tahoma", 8, FontStyle.Bold); richTextBox1.Text += (receive_data + Environment.NewLine); } } catch (SocketException) { connect_flag = false; textBox1.Text += ("Unable to connect to server" + Environment.NewLine); return; } }