Advertisement
MagnusArias

PS1 | Serwer Echo

Mar 28th, 2017
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Net.Sockets;
  4. using System.Net;
  5.  
  6. namespace Serwer_ECHO
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             bool tryAgain = true;
  13.             byte[] data = new byte[1024];
  14.             string input, stringData;
  15.             Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  16.  
  17.             while (tryAgain)
  18.             {
  19.                 Console.Write("Which port you want to create server [7]? : ");
  20.                 string port = Console.ReadLine(); if (port == "") port = "7";
  21.  
  22.                 try
  23.                 {
  24.                     IPAddress ipAd = IPAddress.Parse("127.0.0.1");
  25.                     TcpListener listen = new TcpListener(ipAd, int.Parse(port));
  26.  
  27.                     listen.Start();
  28.                     Console.WriteLine("The local Endpoint is : " + listen.LocalEndpoint);
  29.                     Console.WriteLine("Waiting for connection...");
  30.                     tryAgain = false;
  31.                     socket = listen.AcceptSocket();
  32.                     String message;
  33.                     Console.WriteLine("Connection accepted from " + socket.RemoteEndPoint);
  34.                     while (true)
  35.                     {
  36.                         int k = socket.Receive(data);
  37.  
  38.                         if (k == 0) break;  // jeżeli wysłano pusty pakiet
  39.                         else
  40.                         {
  41.                             message = Encoding.ASCII.GetString(data, 0, k);
  42.                             if (message.Equals("App:CloseConnection", StringComparison.InvariantCultureIgnoreCase)) break;  // jeżeli wysłano komende zamkniecia polaczenia
  43.                             Console.WriteLine(message);
  44.  
  45.                             StringBuilder res = new StringBuilder();
  46.                             res.Append(message);
  47.  
  48.                             byte[] buffer = Encoding.ASCII.GetBytes(res.ToString());
  49.                             socket.Send(buffer);
  50.                         }
  51.                        
  52.                     }
  53.                     Console.WriteLine("\nClient has disconnected from server");
  54.                     socket.Close();
  55.                     listen.Stop();
  56.                     Console.ReadLine();
  57.                 }
  58.                 catch (FormatException e)
  59.                 {
  60.                     Console.Write("Unable to create server or port is not number, want to try with another port? [y/n]: ");
  61.                     string ca = Console.ReadLine();
  62.                     if (ca.ToLower() == "y" || ca.ToLower() == "yes") tryAgain = true;
  63.                     else tryAgain = false;
  64.                 }
  65.                 catch (SocketException e)
  66.                 {
  67.                     Console.WriteLine("Client has closed connection unproperly");
  68.                     Console.WriteLine("or server has another instance running on this port.");
  69.                     Console.ReadLine();
  70.                 }
  71.  
  72.             } //while (tryAgain)
  73.         }   // main
  74.     }   // class
  75. }   // namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement