Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 23rd, 2012  |  syntax: C#  |  size: 4.76 KB  |  views: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Threading;
  8. using System.Collections;
  9. using System.IO;
  10. using MfgSimIP.Forms.Child;
  11.  
  12. namespace MfgSimIP.Classes
  13. {
  14.     public class cNetListener
  15.     {
  16.  
  17.         private static TcpListener listener = null;
  18.         public static ArrayList threads = new ArrayList();
  19.  
  20.         public static void Start(int port)
  21.         {
  22.  
  23.             listener = new TcpListener(IPAddress.Any, port);
  24.             listener.Start();
  25.  
  26.             //Inform Log, that the server is now listening on port x
  27.             frm_log.me.AddEntry(1, "Localhost", "Is now listening on port " + port);
  28.  
  29.             Thread th = new Thread(new ThreadStart(Run));
  30.             th.Start();
  31.  
  32.             for (IEnumerator e = threads.GetEnumerator(); e.MoveNext(); )
  33.             {
  34.                 ServerThread st = (ServerThread)e.Current;
  35.  
  36.                 st.stop = true;
  37.                 while (st.running)
  38.                 Thread.Sleep(1000);
  39.             }
  40.         }
  41.  
  42.         public static void Run()
  43.         {
  44.             while (true)
  45.             {
  46.                 TcpClient c = listener.AcceptTcpClient();
  47.                 threads.Add(new ServerThread(c));
  48.             }
  49.         }
  50.     }
  51.  
  52.  
  53.  
  54.     class ServerThread
  55.     {
  56.         public bool stop = false;
  57.         public bool running = false;
  58.         public TcpClient connection = null;
  59.         public ServerThread(TcpClient connection)
  60.         {
  61.             this.connection = connection;
  62.             new Thread(new ThreadStart(Run)).Start();
  63.         }
  64.         public void Run()
  65.         {
  66.             string m_IP = Convert.ToString(((IPEndPoint)connection.Client.RemoteEndPoint).Address);
  67.             this.running = true;
  68.  
  69.             //User has connected functions go here...
  70.             frm_tele.me.AddToConnectedList(m_IP);
  71.             frm_log.me.AddEntryProtect(1, m_IP, "Has connected.");
  72.             Console.WriteLine(m_IP + " Has connected");
  73.  
  74.             //--------------------------------------
  75.  
  76.             bool loop = true;
  77.             while (loop)
  78.             {
  79.                 try
  80.                 {
  81.                     Stream outStream = this.connection.GetStream();
  82.                    
  83.                     string cData = null;
  84.                     Byte[] bytes = new Byte[256];
  85.  
  86.                     int I;
  87.                     while ((I = outStream.Read(bytes, 0, bytes.Length)) != 0)
  88.                     {
  89.                         cData = System.Text.Encoding.ASCII.GetString(bytes, 0, I);
  90.  
  91.                         //Send Data from Recieved message to where its needed...
  92.                         //outStream.Write(bytes, 0, bytes.Length);
  93.                         Socket socket = this.connection.Client;
  94.                         this.Send(socket, bytes, 0, bytes.Length, 10000);
  95.  
  96.                         Console.WriteLine("Recieved: {0}, {1}", cData, m_IP);
  97.                         frm_log.me.AddEntryProtect(2, m_IP, "Has send a telegram.");
  98.  
  99.                         //--------------------------------------
  100.  
  101.                         Thread.Sleep(1000); //1 second cooldown for loop, so it doesen't get executed every tick.
  102.                     }
  103.                     connection.Close();
  104.  
  105.                     //loops, until stop is set to true.
  106.                     loop = !this.stop;
  107.                 }
  108.                 catch (Exception ex)
  109.                 {
  110.                     Console.WriteLine(ex.Message);
  111.                     loop = false;
  112.                 }
  113.             }
  114.             this.connection.Close();
  115.             this.running = false;
  116.         }
  117.         public void Send(Socket socket, byte[] buffer, int offset, int size, int timeout)
  118.         {
  119.             int startTickCount = Environment.TickCount;
  120.             int sent = 0;  // how many bytes is already sent
  121.             do
  122.             {
  123.                 if (Environment.TickCount > startTickCount + timeout)
  124.                     throw new Exception("Timeout.");
  125.                 try
  126.                 {
  127.                     sent += socket.Send(buffer, offset + sent, size - sent, SocketFlags.None);
  128.                 }
  129.                 catch (SocketException ex)
  130.                 {
  131.                     if (ex.SocketErrorCode == SocketError.WouldBlock ||
  132.                         ex.SocketErrorCode == SocketError.IOPending ||
  133.                         ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  134.                     {
  135.                         // socket buffer is probably full, wait and try again
  136.                         Thread.Sleep(30);
  137.                     }
  138.                     else
  139.                         throw ex;  // any serious error occurr
  140.                 }
  141.             } while (sent < size);
  142.         }
  143.     }
  144. }