Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Runtime.InteropServices;
  7.  
  8. namespace NetMoveClient
  9. {
  10.  
  11.     [StructLayout(LayoutKind.Sequential, Pack = 1)]
  12.     //------------------------------------//
  13.     //------ Struct for sending ----------//
  14.     //------------------------------------//
  15.  
  16.     public struct SendData
  17.     {
  18.         public int id_player;
  19.         public int x;
  20.         public int y;
  21.         public int count_player;
  22.         public int bird;
  23.         public int reserved1;
  24.         public int reserved2;
  25.  
  26.         public byte[] ToBytes()
  27.         {
  28.             Byte[] bytes = new Byte[Marshal.SizeOf(typeof(SendData))];
  29.             GCHandle pinStructure = GCHandle.Alloc(this, GCHandleType.Pinned);
  30.             try
  31.             {
  32.                 Marshal.Copy(pinStructure.AddrOfPinnedObject(), bytes, 0, bytes.Length);
  33.                 return bytes;
  34.             }
  35.             finally
  36.             {
  37.                 pinStructure.Free();
  38.             }
  39.         }
  40.     }
  41.     public class NetClient
  42.     {
  43.         public Socket Handle;
  44.         public int rbyte;
  45.         //-----------------------------------------//
  46.         //------ Starting Client ------------------//
  47.         //-----------------------------------------//
  48.         public void Start(string IP_ADDR)
  49.         {
  50.             try
  51.             {
  52.                 string IP_ADDRESS = IP_ADDR;
  53.                 IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(IP_ADDRESS), 11000);
  54.                 Handle = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  55.                 Handle.Connect(ipEndPoint);
  56.                 Handle.Blocking = false;
  57.             }
  58.             catch (Exception)
  59.             {
  60.  
  61.             }
  62.         }
  63.         //----------------------------------------//
  64.         public void Send(SendData sd)
  65.         {
  66.             byte[] bytes = new byte[8];
  67.             if (Handle != null)
  68.                 if (Handle.Connected)
  69.                 {
  70.                     if (Handle.Poll(0, SelectMode.SelectWrite))
  71.                     {
  72.  
  73.                         Handle.Send(sd.ToBytes());
  74.                     }
  75.                 }
  76.         }
  77.         //---------------------------------------//
  78.         public void Receive(out SendData sd)
  79.         {
  80.             sd.x = 0;
  81.             sd.y = 0;
  82.             sd = new SendData();
  83.             byte[] bytes = new byte[Marshal.SizeOf(typeof(SendData))];
  84.             if (Handle != null)
  85.                 if (Handle.Connected)
  86.                     if (Handle.Poll(0, SelectMode.SelectRead))
  87.                     {
  88.                         int bytesRec = Handle.Receive(bytes);
  89.                         rbyte = bytesRec;
  90.                         sd.id_player = BitConverter.ToInt32(bytes, 0);
  91.                         sd.x = BitConverter.ToInt32(bytes, 4);
  92.                         sd.y = BitConverter.ToInt32(bytes, 8);
  93.                         sd.count_player = BitConverter.ToInt32(bytes, 12);
  94.                         sd.bird = BitConverter.ToInt32(bytes, 16);
  95.                     }
  96.         }
  97.         //--------------------------------------//
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement