/// <summary> /// Set the server settings that declared as object fields /// </summary> private static void SetupServer() { Console.WriteLine("Setting up server..."); serverSocket.Bind(new IPEndPoint(IPAddress.Any, PORT)); serverSocket.Listen(0); serverSocket.BeginAccept(AcceptCallback, null); Config c = new Config(); Server s = new Server(); m.Mat = s.GenerateMatrix(c.getVertices()); mNew.Mat = new int[c.getVertices(), c.getVertices()]; Console.WriteLine("Generated graph vertices: {0}", c.getVertices()); Console.WriteLine("Waiting for {0} clients to send matrix", c.getClients()); Console.WriteLine("Server setup complete"); }
/// <summary> /// Function sends response to client /// </summary> private static void ReceiveCallback(IAsyncResult AR) { Socket current = (Socket)AR.AsyncState; int received; try { received = current.EndReceive(AR); } catch (SocketException) { Console.WriteLine("Client forcefully disconnected"); // Don't shutdown because the socket may be disposed and its disconnected anyway. current.Close(); clientSockets.Remove(current); return; } byte[] recBuf = new byte[received]; Array.Copy(buffer, recBuf, received); string text = Encoding.ASCII.GetString(recBuf); Message msg = new Message(); msg.matrix = new Matrix(); msg = JsonConvert.DeserializeObject <Message>(text); switch (msg.type) { case 0: // First request from client Server s = new Server(); Config c = new Config(); Message me = new Message(); if (c.getClients() > clientSockets.Count) { me.type = 0; me.message = "Not enough clients connected to server."; string jsonmess = JsonConvert.SerializeObject(me, Formatting.Indented); byte[] data = Encoding.ASCII.GetBytes(jsonmess); current.Send(data); } else { me.type = 1; me.matrix = m; string json = JsonConvert.SerializeObject(me, Formatting.Indented); byte[] data = Encoding.ASCII.GetBytes(json); me.message = "Server sending length of matrix to clients"; me.length = data.Length; me.matrix = null; me.type = 2; int j = 0; foreach (Socket value in clientSockets) { me.from = s.calculateRanges(c.getClients(), c.getVertices(), j)[0]; me.to = s.calculateRanges(c.getClients(), c.getVertices(), j)[1]; json = JsonConvert.SerializeObject(me, Formatting.Indented); data = Encoding.ASCII.GetBytes(json); value.Send(data); Console.WriteLine("Information send to client {0}.", ((IPEndPoint)(value.RemoteEndPoint)).Address.ToString()); j++; } Message msa = new Message(); msa.type = 1; msa.matrix = m; string jsonz = JsonConvert.SerializeObject(msa, Formatting.Indented); byte[] dataz = Encoding.ASCII.GetBytes(jsonz); j = 0; Console.WriteLine("Serialized object size: {0} BYTE", me.length); foreach (Socket value in clientSockets.ToList()) { SendChunks(value, me.length, dataz); j++; } Console.WriteLine("Generated Matrix Send to Clients"); } break; case 1: // Response with calulcated array Console.WriteLine("Received array from client (IP: {0})", ((IPEndPoint)(current.RemoteEndPoint)).Address.ToString()); int L = (int)Math.Floor((double)Math.Pow(msg.matrix.Mat.Length, 0.5)); for (int i = 0; i <= L - 1; i++) { for (int j = msg.from; j <= msg.to; j++) { mNew.Mat[i, j] = msg.matrix.Mat[i, j]; } } responses++; Config sc = new Config(); if (responses == sc.getClients()) { Console.WriteLine("Merging all solutions"); Server sxaz = new Server(); sxaz.SaveMatrixToFile(m.Mat, "Matrix.txt"); sxaz.SaveMatrixToFile(mNew.Mat, "NewMatrix.txt"); Console.WriteLine("Generated matrix saved to file Matrix.txt and calculated matrix to NewMatrix.txt. Press Enter to Exit"); } break; case 2: // Exit request Console.WriteLine("Client disconnected (IP: {0})", ((IPEndPoint)(current.RemoteEndPoint)).Address.ToString()); current.Shutdown(SocketShutdown.Both); current.Close(); clientSockets.Remove(current); return; } current.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current); }