Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 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. {   /////////////////////// K L I E N T ///////////////////////
  10.     class Program
  11.     {
  12.         private static TcpListener tcpLsn;
  13.  
  14.         private static Socket s;
  15.  
  16.  
  17.  
  18.         private static void polacz()
  19.         {
  20.             s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  21.             IPAddress hostadd = IPAddress.Parse("127.0.0.1");
  22.             int port = 2222;
  23.  
  24.  
  25.             IPEndPoint EPhost = new IPEndPoint(hostadd, port);
  26.             s.Connect(EPhost);
  27.  
  28.  
  29.         }
  30.  
  31.         private static void odbierz()
  32.         {
  33.             tcpLsn = new TcpListener(IPAddress.Parse("127.0.0.1"), 2222);
  34.             tcpLsn.Start();
  35.             Socket s = tcpLsn.AcceptSocket();
  36.  
  37.             Byte[] odebraneBajty = new Byte[100];
  38.             int red = s.Receive(odebraneBajty, odebraneBajty.Length, 0);
  39.             string eko= null;
  40.             eko = System.Text.Encoding.ASCII.GetString(odebraneBajty);
  41.             Console.WriteLine(eko);
  42.         }
  43.  
  44.         private static void odbierzrr()
  45.         {
  46.  
  47.             Byte[] odebraneBajty = new Byte[100];
  48.             int ret = sckt.Receive(odebraneBajty, odebraneBajty.Length, 0);
  49.              string tmp = null;
  50.              tmp = System.Text.Encoding.ASCII.GetString(odebraneBajty);
  51.              if (tmp.Length > 0)
  52.               Console.WriteLine("Odebrałem komunikat:");
  53.                  Console.WriteLine(tmp);
  54.              
  55.  
  56.         }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.         private static void wyslij(string wiadomosc)
  64.         {
  65.  
  66.             Byte[] byteData = Encoding.ASCII.GetBytes(wiadomosc.ToCharArray());
  67.             s.Send(byteData, byteData.Length, 0);
  68.         }
  69.  
  70.         static void Main(string[] args)
  71.         {
  72.  
  73.  
  74.             polacz();
  75.  
  76.  
  77.             string Line;
  78.            
  79.  
  80.         for(;;)
  81.  
  82.                 try
  83.             {
  84.                
  85.                     Console.WriteLine("Podaj komunikat klient 1 ");
  86.                 Line = Console.ReadLine();
  87.  
  88.                 if (Line != "quit" )
  89.                 {
  90.                     Console.WriteLine("Wysyłam komunikat na serwer");
  91.                     wyslij(Line);
  92.                     Console.WriteLine("Oczekuje na serwer");
  93.                     Console.ReadKey();
  94.                     tcpLsn = new TcpListener(IPAddress.Parse("127.0.0.1"), 2222);
  95.                     tcpLsn.Start();
  96.                     Socket s = tcpLsn.AcceptSocket();
  97.  
  98.                     Byte[] odebraneBajty = new Byte[100];
  99.                     int red = s.Receive(odebraneBajty, odebraneBajty.Length, 0);
  100.                     string eko = null;
  101.                     eko = System.Text.Encoding.ASCII.GetString(odebraneBajty);
  102.                     Console.WriteLine(eko);
  103.                  
  104.                    
  105.                     Console.WriteLine("odebrano komunikat");
  106.  
  107.                     Console.ReadKey();
  108.                 }
  109.                
  110.                
  111.                
  112.                 }
  113.                catch
  114.                 {
  115.                    Environment.Exit(0);
  116.                 }
  117.  
  118.                 tcpLsn.Stop();
  119.            
  120.  
  121.  
  122.         }
  123.  
  124.  
  125.         //      Console.WriteLine("Wysyłam komunikat...");
  126.  
  127.  
  128.         //    wyslij("ala ma kota");
  129.     }
  130.  
  131.  
  132.  
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement