/// <summary> /// Initial functionality of the form /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { int PortNumber = 1200; IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; serverIPAddress.Text = ipAddress.ToString(); serverPortNumber.Text = PortNumber.ToString(); ADOHandler.CreateDatabase(); }
/// <summary> /// A method that starts server and runs it's methods /// </summary> /// <param name="ipAddress"></param> /// <param name="portNumber"></param> public void Run(IPAddress ipAddress, int portNumber) { IPEndPoint localEndPoint = new IPEndPoint(ipAddress, portNumber); // A server socket Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Data = null; listener.Bind(localEndPoint); listener.Listen(10); while (true) { // A socket that accepts data transferred from a client Socket handler = listener.Accept(); while (true) { byte[] bytes = new byte[1024]; // Receiving data in binary representation int bytesRec = handler.Receive(bytes); // Transforming binary data to string Data += Encoding.ASCII.GetString(bytes, 0, bytesRec); if (Data.IndexOf("<EOF>") > -1) { break; } } Thread.Sleep(1000); // Adding received data to the database ADOHandler.AddEntry(Form1.Manager.Parse(Form1.Manager.Data).Name, Form1.Manager.Parse(Form1.Manager.Data).Message); List <string> entries = ADOHandler.GetAllEntries(); // transforming data received from database into bytes List <ArraySegment <byte> > buffers = new List <ArraySegment <byte> >(); foreach (string entry in entries) { ArraySegment <byte> msg = new ArraySegment <byte>(Encoding.ASCII.GetBytes(entry + "\n")); buffers.Add(msg); } // sending binary data back to the client / clients handler.Send(buffers); // Deactivating the socket handler.Shutdown(SocketShutdown.Both); handler.Close(); } }