Guest User

Untitled

a guest
Feb 18th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 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.  
  7. namespace jM2
  8. {
  9.     class Client
  10.     {
  11.         public Socket clientSocket;
  12.         public string clientIP;
  13.         private byte[] clientBuffer = new byte[1024];
  14.  
  15.         /// <summary>
  16.         /// Client constructor
  17.         /// </summary>
  18.         /// <param name="connectionSocket"></param>
  19.         public Client(Socket connectionSocket)
  20.         {
  21.             clientSocket = connectionSocket;
  22.             clientIP = connectionSocket.RemoteEndPoint.ToString();
  23.             clientSocket.BeginReceive(clientBuffer, 0, clientBuffer.Length, SocketFlags.None, new AsyncCallback(dataArrival), null);
  24.         }
  25.  
  26.         /// <summary>
  27.         /// Close client socket
  28.         /// </summary>
  29.         public void Disconnect()
  30.         {
  31.             try
  32.             {
  33.                 Server.freeConnection(this);
  34.                 clientSocket.Close();
  35.             }
  36.             catch
  37.             {
  38.  
  39.             }
  40.         }
  41.  
  42.         /// <summary>
  43.         /// Data arrived on client socket
  44.         /// </summary>
  45.         /// <param name="iar"></param>
  46.         private void dataArrival(IAsyncResult iar)
  47.         {
  48.             try
  49.             {
  50.                 int bytesReceived = clientSocket.EndReceive(iar);
  51.  
  52.                 if (bytesReceived > 0)
  53.                 {
  54.                     Console.WriteLine("[" + clientIP + "](" + bytesReceived.ToString() + "): " + BitConverter.ToString(clientBuffer, 0, bytesReceived));
  55.                 }
  56.             }
  57.             catch (Exception ex)
  58.             {
  59.                 Console.WriteLine("[" + ex.HelpLink + "]: " + ex.Message);
  60.                 Console.ReadLine();
  61.             }
  62.  
  63.             clientSocket.BeginReceive(clientBuffer, 0, clientBuffer.Length, SocketFlags.None, new AsyncCallback(dataArrival), null);
  64.         }
  65.  
  66.         /// <summary>
  67.         /// Send some data to client
  68.         /// </summary>
  69.         /// <param name="data"></param>
  70.         public void Send(byte[] data)
  71.         {
  72.             clientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(dataSent), null);
  73.         }
  74.  
  75.         /// <summary>
  76.         /// Executed when data has been sent to client
  77.         /// </summary>
  78.         /// <param name="iar"></param>
  79.         private void dataSent(IAsyncResult iar)
  80.         {
  81.             clientSocket.EndSend(iar);
  82.         }
  83.  
  84.         /// <summary>
  85.         /// Convert string to byte array
  86.         /// </summary>
  87.         /// <param name="str"></param>
  88.         /// <returns></returns>
  89.         public byte[] stringToByteArray(string str)
  90.         {
  91.             System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
  92.             return encoding.GetBytes(str);
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment