Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7.  
  8. namespace Socket
  9. {
  10.     public class CustomSocket
  11.     {
  12.         //members
  13.             //private
  14.         private Socket m_Socket;
  15.         private String m_IPAddress;
  16.         private int m_Port;
  17.         private bool m_Connected = false;
  18.         private CustomBuffer m_buffer;
  19.         public event EventHandler<FullMessageEventArgs> FullMessage;
  20.  
  21.         //constructor
  22.         public CustomSocket(String IPAddress, int Port)
  23.         {
  24.             this.m_IPAddress = IPAddress;
  25.             this.m_Port = Port;
  26.             this.m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  27.             this.m_buffer = new CustomBuffer(this);
  28.         }
  29.  
  30.         //methods
  31.         internal void onFullMessage(object sender, FullMessageEventArgs e)
  32.         {
  33.             if (e.Data != null)
  34.             {
  35.                 FullMessage(sender, e);
  36.             }
  37.         }
  38.        
  39.         public bool Connect()
  40.         {
  41.             try
  42.             {
  43.                 this.m_Socket.Connect(this.m_IPAddress, this.m_Port);
  44.                 new Thread(new ThreadStart(this.ListenThreadMethod)).Start();
  45.             }
  46.             catch (SocketException)
  47.             {
  48.                 return false;
  49.             }
  50.             catch (ArgumentNullException)
  51.             {
  52.                 return false;
  53.             }
  54.             catch (ObjectDisposedException)
  55.             {
  56.                 return false;
  57.             }
  58.             catch (InvalidOperationException)
  59.             {
  60.                 return false;
  61.             }
  62.             this.m_Connected = true;
  63.             return true;
  64.         }
  65.  
  66.         public void Disconnect()
  67.         {
  68.             try
  69.             {
  70.                 this.m_Connected = false;
  71.                 this.m_Socket.Close();
  72.             }
  73.             catch { }
  74.         }
  75.  
  76.         public bool Send(byte[] data)
  77.         {
  78.             try
  79.             {
  80.                 int bytesSent = this.m_Socket.Send(data);
  81.                 return bytesSent == data.Length;
  82.             }
  83.             catch (SocketException)
  84.             {
  85.                 return false;
  86.             }
  87.             catch (ArgumentNullException)
  88.             {
  89.                 return false;
  90.             }
  91.             catch (ObjectDisposedException)
  92.             {
  93.                 return false;
  94.             }
  95.             catch (InvalidOperationException)
  96.             {
  97.                 return false;
  98.             }
  99.         }
  100.  
  101.         private void ListenThreadMethod()
  102.         {
  103.             byte[] recvBuffer = new byte[1024];
  104.             int bytesReceived = 0;
  105.             while (this.m_Connected)
  106.             {
  107.                 try
  108.                 {
  109.                     bytesReceived = this.m_Socket.Receive(recvBuffer);
  110.                     if (bytesReceived == 0)
  111.                     {
  112.                         return;
  113.                     }
  114.                     byte[] data = new byte[bytesReceived];
  115.                     Array.Copy(recvBuffer, 0, data, 0, bytesReceived);
  116.                     if (data != null)
  117.                     {
  118.                         this.m_buffer.WriteBytes(data);
  119.                     }
  120.                 }
  121.                 catch (SocketException)
  122.                 {
  123.                     return;
  124.                 }
  125.                 catch (ArgumentNullException)
  126.                 {
  127.                     return;
  128.                 }
  129.                 catch (ObjectDisposedException)
  130.                 {
  131.                     return;
  132.                 }
  133.                 catch (InvalidOperationException)
  134.                 {
  135.                     return;
  136.                 }
  137.             }
  138.         }
  139.        
  140.         //get
  141.         String IPAddress
  142.         {
  143.             get { return this.m_IPAddress; }
  144.         }
  145.  
  146.         int Port
  147.         {
  148.             get { return this.m_Port; }
  149.         }
  150.  
  151.         bool Connected
  152.         {
  153.             get { return this.m_Connected; }
  154.         }
  155.     }
  156.  
  157.     public class FullMessageEventArgs : EventArgs
  158.     {
  159.         public byte[] Data { get; set; }
  160.  
  161.         public FullMessageEventArgs(byte[] data)
  162.         {
  163.             this.Data = data;
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement