Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- namespace Client_ServerTest01
- {
- class Server
- {
- const int Port = 2536;
- static void Main(string[] args)
- {
- Console.WriteLine("Start");
- bool ExitTrigger = true;
- IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Port);
- Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- listenSocket.Bind(ipPoint);
- Console.WriteLine("Bilding");
- listenSocket.Listen(100);
- Console.WriteLine("Listening");
- Socket handler = listenSocket.Accept();
- Console.WriteLine("Ready!");
- while (ExitTrigger)
- {
- byte[] data = new byte[256];
- handler.Receive(data);
- int num = BitConverter.ToInt32(data, 0);
- if (num != 0)
- {
- Console.WriteLine(num);
- handler.Send(BitConverter.GetBytes(factorial(num)));
- }
- else
- {
- handler.Shutdown(SocketShutdown.Both);
- handler.Close();
- Console.WriteLine("Closing");
- ExitTrigger = false;
- }
- }
- }
- static int factorial(int num)
- {
- int result = 1;
- for (int i = 1; i <= num; i++)
- {
- result *= i;
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment