Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Sockets;
  7. using System.Net;
  8. namespace Serwer
  9. {///////////////////////////////////// S E R W E R //////////////
  10.     class Program
  11.     {
  12.        
  13.    
  14.      private static TcpListener tcpLsn;
  15.  
  16.     private static Socket s;
  17.  
  18.  
  19.     private static void slij(string mess)
  20.     {
  21.  
  22.         Byte[] byteData = Encoding.ASCII.GetBytes(mess.ToCharArray());
  23.         s.Send(byteData, byteData.Length, 0);
  24.      
  25.     }
  26.        
  27.  
  28.     static void serwuj()
  29.     {
  30.         tcpLsn = new TcpListener(IPAddress.Parse("127.0.0.1"), 2222);
  31.         tcpLsn.Start();
  32.         Socket sckt = tcpLsn.AcceptSocket();
  33.  
  34.         Byte[] odebraneBajty = new Byte[100];
  35.         int ret = sckt.Receive(odebraneBajty, odebraneBajty.Length, 0);
  36.         string tmp = null;
  37.         tmp = System.Text.Encoding.ASCII.GetString(odebraneBajty);
  38.        
  39.       for(;;)
  40.           try
  41.           {
  42.  
  43.               if (tmp.Length > 0 || tmp != "quit")
  44.               {
  45.                   Console.WriteLine("odebrano komunikat");
  46.                   Console.WriteLine(tmp);
  47.                   String Cope;
  48.                   Cope = Console.ReadLine();
  49.                  
  50.                   if (Cope != "w")
  51.                   {
  52.                       String eko;
  53.                       Console.WriteLine("Wyslij komunikat do klienta");
  54.                       eko = Console.ReadLine();
  55.                       slij(eko);
  56.  
  57.                       Console.WriteLine("Wysłano do klienta");
  58.  
  59.                       Console.ReadKey();
  60.                   }
  61.               }
  62.           }
  63.  
  64.           catch
  65.           {
  66.               Environment.Exit(0);
  67.           }
  68.  
  69.         tcpLsn.Stop();
  70.    
  71.     }
  72.  
  73.             static void Main(string[] args)
  74.         {
  75.             Console.WriteLine("Startuje serwer....");
  76.            
  77.                 serwuj();
  78.  
  79.         }
  80.  
  81.  
  82.  
  83.     }
  84.  
  85.  
  86.  
  87.        
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement