Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.Runtime.InteropServices;
- namespace NetMoveClient
- {
- [StructLayout(LayoutKind.Sequential, Pack = 1)]
- //------------------------------------//
- //------ Struct for sending ----------//
- //------------------------------------//
- public struct SendData
- {
- public int id_player;
- public int x;
- public int y;
- public int count_player;
- public int bird;
- public int reserved1;
- public int reserved2;
- public byte[] ToBytes()
- {
- Byte[] bytes = new Byte[Marshal.SizeOf(typeof(SendData))];
- GCHandle pinStructure = GCHandle.Alloc(this, GCHandleType.Pinned);
- try
- {
- Marshal.Copy(pinStructure.AddrOfPinnedObject(), bytes, 0, bytes.Length);
- return bytes;
- }
- finally
- {
- pinStructure.Free();
- }
- }
- }
- public class NetClient
- {
- public Socket Handle;
- public int rbyte;
- //-----------------------------------------//
- //------ Starting Client ------------------//
- //-----------------------------------------//
- public void Start(string IP_ADDR)
- {
- try
- {
- string IP_ADDRESS = IP_ADDR;
- IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(IP_ADDRESS), 11000);
- Handle = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- Handle.Connect(ipEndPoint);
- Handle.Blocking = false;
- }
- catch (Exception)
- {
- }
- }
- //----------------------------------------//
- public void Send(SendData sd)
- {
- byte[] bytes = new byte[8];
- if (Handle != null)
- if (Handle.Connected)
- {
- if (Handle.Poll(0, SelectMode.SelectWrite))
- {
- Handle.Send(sd.ToBytes());
- }
- }
- }
- //---------------------------------------//
- public void Receive(out SendData sd)
- {
- sd.x = 0;
- sd.y = 0;
- sd = new SendData();
- byte[] bytes = new byte[Marshal.SizeOf(typeof(SendData))];
- if (Handle != null)
- if (Handle.Connected)
- if (Handle.Poll(0, SelectMode.SelectRead))
- {
- int bytesRec = Handle.Receive(bytes);
- rbyte = bytesRec;
- sd.id_player = BitConverter.ToInt32(bytes, 0);
- sd.x = BitConverter.ToInt32(bytes, 4);
- sd.y = BitConverter.ToInt32(bytes, 8);
- sd.count_player = BitConverter.ToInt32(bytes, 12);
- sd.bird = BitConverter.ToInt32(bytes, 16);
- }
- }
- //--------------------------------------//
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement