Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.53 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. using System.Threading;
  9.  
  10. namespace Zad_4_5_Client_serwer_1_
  11. {
  12.     class Program
  13.     {
  14.             private static Socket s;
  15.             private static TcpListener tcpLsn;
  16.  
  17.             private static readonly string stopMsg = "quit";
  18.             private static string stopSerMsg;
  19.  
  20.             private static void Connect()
  21.             {
  22.                 try
  23.                 {
  24.                     s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  25.                     IPAddress hostIp = IPAddress.Parse("127.0.0.1");
  26.  
  27.                     IPEndPoint EPhost = new IPEndPoint(hostIp, 2222);
  28.                     s.Connect(EPhost);
  29.                     Console.WriteLine("Połączono do serwera na porcie: " + 2222);
  30.                 }
  31.                 catch (Exception e)
  32.                 {
  33.                     Console.WriteLine(e.Message);
  34.                     return;
  35.                 }
  36.  
  37.             }
  38.  
  39.             private static void Send(string message)
  40.             {
  41.                 try
  42.                 {
  43.                     Byte[] byteData = Encoding.ASCII.GetBytes(message.ToCharArray());
  44.                     Console.WriteLine("Wysyłano: " + message);
  45.                     s.Send(byteData, byteData.Length, 0);
  46.                 }
  47.                 catch (Exception e)
  48.                 {
  49.                     Console.WriteLine(e.Message);
  50.                     return;
  51.                 }
  52.  
  53.             }
  54.  
  55.             static void RunServer()
  56.             {
  57.                 tcpLsn = new TcpListener(IPAddress.Parse("127.0.0.1"), 2000);
  58.                 tcpLsn.Start();
  59.                 Console.WriteLine("Serwer działa na porcie: " + 2000);
  60.             }
  61.  
  62.             static void Receive()
  63.             {
  64.                 while (true)
  65.                 {
  66.                     try
  67.                     {
  68.                         while (true)
  69.                         {
  70.                             Socket sckt = tcpLsn.AcceptSocket();
  71.                             Byte[] received = new Byte[100];
  72.                             int res = sckt.Receive(received, received.Length, 0);
  73.                             string tmp = null;
  74.                             tmp = System.Text.Encoding.ASCII.GetString(received);
  75.                             Console.WriteLine("Otrzymano: " + tmp);
  76.                             if (tmp.Length > 0)
  77.                             {
  78.                                 Console.WriteLine(tmp);
  79.                             }
  80.                             stopSerMsg = tmp;
  81.                             if (string.Compare(tmp, "quit") == 0)
  82.                             {
  83.                             StopServer();
  84.                             }
  85.                     }
  86.                     }
  87.                     catch (Exception e)
  88.                     {
  89.                         Console.WriteLine(e.Message);
  90.                         return;
  91.                     }
  92.  
  93.                 }
  94.             }
  95.  
  96.             static void StopServer()
  97.             {
  98.                 Console.WriteLine("Zatrzymano Serwer");
  99.                 tcpLsn.Stop();
  100.             }
  101.  
  102.             private static void ServerTask()
  103.             {
  104.                 RunServer();
  105.                 Receive();
  106.                 StopServer();
  107.             }
  108.  
  109.             private static void ClientTask()
  110.             {
  111.                 String input = null;
  112.                     do
  113.                     {
  114.                         Connect();
  115.                         Console.WriteLine("Podaj wiadomość: ");
  116.                         input = Console.ReadLine();
  117.                         Send(input);
  118.                     } while (IsQuit(input));
  119.                 Console.WriteLine(stopMsg);
  120.                 return;
  121.             }
  122.  
  123.             private static Boolean IsQuit(string cmd)
  124.             {
  125.                 return !stopMsg.Equals(cmd);
  126.             }
  127.  
  128.  
  129.  
  130.             static void Main(string[] args)
  131.             {
  132.                 Thread serwer = new Thread(ServerTask);
  133.                 Thread klient = new Thread(ClientTask);
  134.  
  135.                 serwer.Start();
  136.                 klient.Start();
  137.  
  138.             if (string.Compare(stopSerMsg, "quit") == 0)
  139.             {
  140.                 klient.Abort();
  141.             }
  142.             }
  143.     }
  144. }
  145.  
  146. drugi__
  147. using System;
  148. using System.Collections.Generic;
  149. using System.Linq;
  150. using System.Text;
  151. using System.Threading.Tasks;
  152. using System.Net.Sockets;
  153. using System.Net;
  154. using System.Threading;
  155.  
  156. namespace Zad_4_5_Client_serwer_2_
  157. {
  158.     class Program
  159.     {
  160.         private static Socket s;
  161.         private static TcpListener tcpLsn;
  162.  
  163.         private static readonly string stopMsg = "quit";
  164.         private static string stopSerMsg;
  165.  
  166.  
  167.         private static void Connect()
  168.         {
  169.             try
  170.             {
  171.                 s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  172.                 IPAddress hostIp = IPAddress.Parse("127.0.0.1");
  173.  
  174.                 IPEndPoint EPhost = new IPEndPoint(hostIp, 2000);
  175.                 s.Connect(EPhost);
  176.                 Console.WriteLine("Połączono do serwera na porcie: " + 2000);
  177.             }
  178.             catch (Exception e)
  179.             {
  180.                 Console.WriteLine(e.Message);
  181.                 return;
  182.             }
  183.  
  184.         }
  185.  
  186.         private static void Send(string message)
  187.         {
  188.             try
  189.             {
  190.                 Byte[] byteData = Encoding.ASCII.GetBytes(message.ToCharArray());
  191.                 Console.WriteLine("Wysyłano: " + message);
  192.                 s.Send(byteData, byteData.Length, 0);
  193.             }
  194.             catch (Exception e)
  195.             {
  196.                 Console.WriteLine(e.Message);
  197.                 return;
  198.             }
  199.  
  200.         }
  201.  
  202.         static void RunServer()
  203.         {
  204.             tcpLsn = new TcpListener(IPAddress.Parse("127.0.0.1"), 2222);
  205.             tcpLsn.Start();
  206.             Console.WriteLine("Serwer działa na porcie: " + 2222);
  207.         }
  208.  
  209.         static void Receive()
  210.         {
  211.             while (true)
  212.             {
  213.                 try
  214.                 {
  215.                     while (true)
  216.                     {
  217.                         Socket sckt = tcpLsn.AcceptSocket();
  218.                         Byte[] received = new Byte[100];
  219.                         int res = sckt.Receive(received, received.Length, 0);
  220.                         string tmp = null;
  221.                         tmp = System.Text.Encoding.ASCII.GetString(received);
  222.                         Console.WriteLine("Otrzymano: " + tmp);
  223.                         if (tmp.Length > 0)
  224.                         {
  225.                             Console.WriteLine(tmp);
  226.                         }
  227.                         stopSerMsg = tmp;
  228.                         if (string.Compare(tmp, "quit") == 0)
  229.                         {
  230.                             StopServer();
  231.                         }
  232.                     }
  233.                 }
  234.                 catch (Exception e)
  235.                 {
  236.                     Console.WriteLine(e.Message);
  237.                     return;
  238.                 }
  239.  
  240.             }
  241.         }
  242.  
  243.         static void StopServer()
  244.         {
  245.             Console.WriteLine("Zatrzymano Serwer");
  246.             tcpLsn.Stop();
  247.         }
  248.  
  249.         private static void ServerTask()
  250.         {
  251.             RunServer();
  252.             Receive();
  253.             StopServer();
  254.         }
  255.  
  256.         private static void ClientTask()
  257.         {
  258.             String input = null;
  259.             do
  260.             {
  261.                 Connect();
  262.                 Console.WriteLine("Podaj wiadomość: ");
  263.                 input = Console.ReadLine();
  264.                 Send(input);
  265.             } while (IsQuit(input));
  266.  
  267.             Console.WriteLine(stopMsg);
  268.             return;
  269.         }
  270.  
  271.         private static Boolean IsQuit(string cmd)
  272.         {
  273.             return !stopMsg.Equals(cmd);
  274.         }
  275.  
  276.  
  277.  
  278.         static void Main(string[] args)
  279.         {
  280.             Thread serwer = new Thread(ServerTask);
  281.             Thread klient = new Thread(ClientTask);
  282.  
  283.             serwer.Start();
  284.             klient.Start();
  285.  
  286.             if (string.Compare(stopSerMsg, "quit") == 0)
  287.             {
  288.                 klient.Abort();
  289.             }
  290.  
  291.         }
  292.     }
  293. }
  294.  
  295. Hello
  296.  
  297. using System;
  298. using System.Collections.Generic;
  299. using System.Linq;
  300. using System.Text;
  301. using System.Threading.Tasks;
  302. using System.Net.Sockets;
  303. using System.Net;
  304.  
  305. namespace Hello
  306. {
  307.     class Program
  308.     {
  309.         private static TcpListener tcpLan;
  310.         private static Socket s;
  311.  
  312.         private static void polacz()
  313.         {
  314.             s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  315.             IPAddress hostadd = IPAddress.Parse("127.0.0.1");
  316.             int port = 2222;
  317.  
  318.             IPEndPoint EPhost = new IPEndPoint(hostadd, port);
  319.             s.Connect(EPhost);
  320.         }
  321.  
  322.         private static void wyslij(string wiadomosc)
  323.         {
  324.  
  325.             Byte[] byteData = Encoding.ASCII.GetBytes(wiadomosc.ToCharArray());
  326.             s.Send(byteData, byteData.Length, 0);
  327.         }
  328.  
  329.         static void Main(string[] args)
  330.         {
  331.             polacz();
  332.             Console.WriteLine("Wysyłam komunikat...");
  333.             wyslij("ala ma kota");
  334.             System.Threading.Thread.Sleep(3000);
  335.         }
  336.     }
  337. }
  338.  
  339.  
  340. __serwer hello
  341. using System;
  342. using System.Collections.Generic;
  343. using System.Linq;
  344. using System.Text;
  345. using System.Threading.Tasks;
  346. using System.Net.Sockets;
  347. using System.Net;
  348.  
  349. namespace Serwer
  350. {
  351.     class Program
  352.     {
  353.         private static TcpListener tcpLsn;
  354.         private static Socket s;
  355.  
  356.         static void serwuj()
  357.         {
  358.             tcpLsn = new TcpListener(IPAddress.Parse("127.0.0.1"), 2222);
  359.             tcpLsn.Start();
  360.             Socket sckt = tcpLsn.AcceptSocket();
  361.             Byte[] odebraneBajty = new Byte[100];
  362.             int ret = sckt.Receive(odebraneBajty, odebraneBajty.Length, 0);
  363.             string tmp = null;
  364.             tmp = System.Text.Encoding.ASCII.GetString(odebraneBajty);
  365.             if (tmp.Length > 0)
  366.             {
  367.                 Console.WriteLine("Odebrałem komunikat:");
  368.                 Console.WriteLine(tmp);
  369.             }
  370.             tcpLsn.Stop();
  371.         }
  372.  
  373.         static void Main(string[] args)
  374.         {
  375.             Console.WriteLine("Startuję serwer...");
  376.             serwuj();
  377.             System.Threading.Thread.Sleep(3000);
  378.         }
  379.     }
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement