Advertisement
Nucleo

Untitled

May 12th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.83 KB | None | 0 0
  1. public static void StartListening()
  2.     {
  3.         // Data buffer for incoming data.
  4.         byte[] bytes = new Byte[1024];
  5.  
  6.         // Establish the local endpoint for the socket.
  7.         // The DNS name of the computer
  8.         // running the listener is "host.contoso.com".
  9.         IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
  10.         IPAddress ipAddress = ipHostInfo.AddressList[0];
  11.         IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 4444);
  12.  
  13.         // Create a TCP/IP socket.
  14.         Socket listener = new Socket(AddressFamily.InterNetwork,
  15.         SocketType.Stream, ProtocolType.Tcp);
  16.  
  17.         // Bind the socket to the local endpoint and listen for incoming connections.
  18.         try
  19.         {
  20.             listener.Bind(localEndPoint);
  21.             listener.Listen(100);
  22.  
  23.  
  24.             while (true)
  25.             {
  26.                 // Set the event to nonsignaled state.
  27.                 allDone.Reset();
  28.  
  29.                 // Start an asynchronous socket to listen for connections.
  30.                 Console.WriteLine("Listening...");
  31.  
  32.  
  33.                 listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
  34.  
  35.  
  36.  
  37.                 // Wait until a connection is made before continuing.
  38.                 allDone.WaitOne();
  39.  
  40.  
  41.             }
  42.  
  43.         }
  44.         catch (Exception e)
  45.         {
  46.             Console.WriteLine(e.ToString());
  47.         }
  48.  
  49.         Console.WriteLine("\nPress ENTER to continue...");
  50.         Console.Read();
  51.  
  52.     }
  53.  
  54.     public static void AcceptCallback(IAsyncResult ar)
  55.     {
  56.         // Signal the main thread to continue.
  57.         allDone.Set();
  58.  
  59.         // Get the socket that handles the client request.
  60.         Socket listener = (Socket)ar.AsyncState;
  61.         Socket handler = listener.EndAccept(ar);
  62.  
  63.         StateObject client = new StateObject();
  64.  
  65.         int i = Clients.Add(client);
  66.         Console.WriteLine("Client " + i.ToString() + " Connected");
  67.  
  68.  
  69.         client.workSocket = handler;
  70.         handler.BeginReceive(client.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), client);
  71.  
  72.  
  73.  
  74.     }
  75.  
  76.     public static void ReadCallback(IAsyncResult ar)
  77.     {
  78.         String content = String.Empty;
  79.  
  80.         // Retrieve the state object and the handler socket
  81.         // from the asynchronous state object.
  82.         StateObject state = (StateObject)ar.AsyncState;
  83.         Socket handler = state.workSocket;
  84.  
  85.         // Read data from the client socket.
  86.         int bytesRead = handler.EndReceive(ar);
  87.  
  88.         if (bytesRead > 0)
  89.         {
  90.             // There  might be more data, so store the data received so far.
  91.             state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
  92.  
  93.             // Check for end-of-file tag. If it is not there, read
  94.             // more data.
  95.             content = state.sb.ToString();
  96.            
  97.             if (content.IndexOf("<EOF>") > -1)
  98.             {
  99.                 Console.WriteLine(content);
  100.                 string[] Data;
  101.                 string Message = "";
  102.                 Data = content.Split('|');
  103.                
  104.  
  105.                 if (Data[0] == "0000")
  106.                 {
  107.                
  108.                     Send(client.workSocket, Message);
  109.                 }
  110.  
  111.                    
  112.                 Console.WriteLine("0001");
  113.                
  114.                
  115.                
  116.              
  117.                
  118.             }
  119.             else
  120.             {
  121.  
  122.                 // Not all data received. Get more.
  123.                 Console.WriteLine("Need More Data");
  124.                 handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
  125.                 new AsyncCallback(ReadCallback), state);
  126.  
  127.             }
  128.         }
  129.     }
  130.  
  131.     private static void Send(Socket handler, string data)
  132.     {
  133.         try
  134.         {
  135.             // Convert the string data to byte data using ASCII encoding.
  136.             byte[] byteData = Encoding.ASCII.GetBytes(data);
  137.             Console.WriteLine("Sending Data");
  138.             // Begin sending the data to the remote device.
  139.             handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
  140.         }
  141.         catch (Exception e)
  142.         {
  143.  
  144.             Console.WriteLine("Couldn't Send Data to Socket");
  145.  
  146.  
  147.         }
  148.  
  149.     }
  150.  
  151.     private static void SendCallback(IAsyncResult ar)
  152.     {
  153.         try
  154.         {
  155.  
  156.             // Retrieve the socket from the state object.
  157.             Socket handler = (Socket)ar.AsyncState;
  158.  
  159.  
  160.             // Complete sending the data to the remote device.
  161.             int bytesSent = handler.EndSend(ar);
  162.             //Console.WriteLine("Sent {0} bytes to client.", bytesSent);
  163.             Console.WriteLine("Data Sent");
  164.  
  165.          
  166.  
  167.         }
  168.         catch (Exception e)
  169.         {
  170.  
  171.             Console.WriteLine(e.ToString());
  172.  
  173.         }
  174.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement