using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; using System.Collections; using System.IO; using MfgSimIP.Forms.Child; namespace MfgSimIP.Classes { public class cNetListener { private static TcpListener listener = null; public static ArrayList threads = new ArrayList(); public static void Start(int port) { listener = new TcpListener(IPAddress.Any, port); listener.Start(); //Inform Log, that the server is now listening on port x frm_log.me.AddEntry(1, "Localhost", "Is now listening on port " + port); Thread th = new Thread(new ThreadStart(Run)); th.Start(); for (IEnumerator e = threads.GetEnumerator(); e.MoveNext(); ) { ServerThread st = (ServerThread)e.Current; st.stop = true; while (st.running) Thread.Sleep(1000); } } public static void Run() { while (true) { TcpClient c = listener.AcceptTcpClient(); threads.Add(new ServerThread(c)); } } } class ServerThread { public bool stop = false; public bool running = false; public TcpClient connection = null; public ServerThread(TcpClient connection) { this.connection = connection; new Thread(new ThreadStart(Run)).Start(); } public void Run() { string m_IP = Convert.ToString(((IPEndPoint)connection.Client.RemoteEndPoint).Address); this.running = true; //User has connected functions go here... frm_tele.me.AddToConnectedList(m_IP); frm_log.me.AddEntryProtect(1, m_IP, "Has connected."); Console.WriteLine(m_IP + " Has connected"); //-------------------------------------- bool loop = true; while (loop) { try { Stream outStream = this.connection.GetStream(); string cData = null; Byte[] bytes = new Byte[256]; int I; while ((I = outStream.Read(bytes, 0, bytes.Length)) != 0) { cData = System.Text.Encoding.ASCII.GetString(bytes, 0, I); //Send Data from Recieved message to where its needed... //outStream.Write(bytes, 0, bytes.Length); Socket socket = this.connection.Client; this.Send(socket, bytes, 0, bytes.Length, 10000); Console.WriteLine("Recieved: {0}, {1}", cData, m_IP); frm_log.me.AddEntryProtect(2, m_IP, "Has send a telegram."); //-------------------------------------- Thread.Sleep(1000); //1 second cooldown for loop, so it doesen't get executed every tick. } connection.Close(); //loops, until stop is set to true. loop = !this.stop; } catch (Exception ex) { Console.WriteLine(ex.Message); loop = false; } } this.connection.Close(); this.running = false; } public void Send(Socket socket, byte[] buffer, int offset, int size, int timeout) { int startTickCount = Environment.TickCount; int sent = 0; // how many bytes is already sent do { if (Environment.TickCount > startTickCount + timeout) throw new Exception("Timeout."); try { sent += socket.Send(buffer, offset + sent, size - sent, SocketFlags.None); } catch (SocketException ex) { if (ex.SocketErrorCode == SocketError.WouldBlock || ex.SocketErrorCode == SocketError.IOPending || ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable) { // socket buffer is probably full, wait and try again Thread.Sleep(30); } else throw ex; // any serious error occurr } } while (sent < size); } } }