Advertisement
PomozMi

PolocznieTCPSerwer

Nov 24th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Net.Sockets;
  11. using System.Net;
  12.  
  13. namespace PolocznieTCPSerwer
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public static Timer t = new Timer();
  18.         public static Timer t2 = new Timer();
  19.         public static Timer t3 = new Timer();
  20.         public NetworkStream ns = null;
  21.         public NetworkStream ctrl_ns = null;
  22.  
  23.         public Form1()
  24.         {
  25.             InitializeComponent();
  26.  
  27.             t.Interval = 1000; //wartość timera wysyłającego komuniakty
  28.             t.Tick += new EventHandler(this.komunikaty); //metoda wysyłana przez timer
  29.             t2.Interval = 500; //wartość timera odbierającego potwierdzenia
  30.             t2.Tick += new EventHandler(this.potwierdzenia); //metoda wysyłana przez timer
  31.             t3.Interval = 200; //wartość timera odbierającego potwierdzenia
  32.             t3.Tick += new EventHandler(this.koniec); //metoda wysyłana przez timer
  33.         }
  34.         private TcpListener serwer = null;
  35.         private TcpClient klient = null;
  36.         private TcpListener ctrl_serwer = null;
  37.         private TcpClient ctrl_klient = null;
  38.         private void label2_Click(object sender, EventArgs e)
  39.         {
  40.  
  41.         }
  42.  
  43.         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  44.         {
  45.  
  46.         }
  47.  
  48.         private void button1_Click(object sender, EventArgs e)
  49.         {
  50.             IPAddress adresIP = null;
  51.             try
  52.             {
  53.                 adresIP = IPAddress.Parse(textBox1.Text);
  54.             }
  55.             catch
  56.             {
  57.                 MessageBox.Show("Błędny format adresu IP!", "Błąd");
  58.                 textBox1.Text = String.Empty;
  59.                 return;
  60.             }
  61.             int port = System.Convert.ToInt16(numericUpDown1.Value);
  62.             try
  63.             {
  64.                 serwer = new TcpListener(adresIP, port);
  65.                 serwer.Start();
  66.                 ctrl_serwer = new TcpListener(adresIP, port+1);
  67.                 ctrl_serwer.Start();
  68.                 klient = serwer.AcceptTcpClient();
  69.                 ctrl_klient = ctrl_serwer.AcceptTcpClient();
  70.                 IPEndPoint IP = (IPEndPoint)klient.Client.RemoteEndPoint;
  71.                 listBox1.Items.Add("["+IP.ToString()+"] :Nawiązano połączenie");
  72.                 button1.Enabled = false;
  73.                 button2.Enabled = true;                              
  74.             }
  75.             catch (Exception ex)
  76.             {
  77.                 listBox1.Items.Add("Błąd inicjacji serwera!");
  78.                 MessageBox.Show(ex.ToString(), "Błąd");
  79.             }
  80.             t.Start();//start timera (wartości w konstruktorze)
  81.             t2.Start();
  82.             ns = klient.GetStream();//Pobranie referencji do obiektu strumienia
  83.             ctrl_ns = ctrl_klient.GetStream();
  84.         }
  85.  
  86.         private void komunikaty(object sender, EventArgs e)
  87.         {            
  88.             //tablice na dane przychodzące i wychodzące
  89.             byte[] outbytes = Encoding.ASCII.GetBytes("polaczenie aktywne");
  90.             //Wysyłanie i odbieranie danych
  91.             ns.Write(outbytes, 0, outbytes.Length);
  92.             //Wyświetlenie łańcucha znaków
  93.             listBox1.Items.Add("wysłano komunikat: polaczenie aktywne");
  94.         }
  95.         private void potwierdzenia(object sender, EventArgs e)
  96.         {
  97.             if (ns.DataAvailable)
  98.             {
  99.                 //tablice na dane przychodzące i wychodzące
  100.                 byte[] inbytes = new byte[1024];
  101.                 //Wysyłanie i odbieranie danych
  102.                 ns.Read(inbytes, 0, inbytes.Length);
  103.                 //Konwersja danych odebranych do postaci łańcucha znaków
  104.                 string tekst = Encoding.ASCII.GetString(inbytes);
  105.                 //Wyświetlenie łańcucha znaków
  106.                 listBox1.Items.Add(tekst);
  107.                 ns.Flush();
  108.             }
  109.             if (ctrl_ns.DataAvailable)
  110.             {
  111.                 //tablice na dane przychodzące i wychodzące
  112.                 byte[] inbytes = new byte[1024];
  113.                 //Wysyłanie i odbieranie danych
  114.                 ctrl_ns.Read(inbytes, 0, inbytes.Length);
  115.                 //Konwersja danych odebranych do postaci łańcucha znaków
  116.                 string tekst = Encoding.ASCII.GetString(inbytes);
  117.  
  118.                 if (tekst.IndexOf("END") != -1)
  119.                 {
  120.                     ctrl_ns.Flush();
  121.                     //tablice na dane przychodzące i wychodzące
  122.                     byte[] outbytes = Encoding.ASCII.GetBytes("ENDACK");
  123.                     //Wysyłanie i odbieranie danych
  124.                     ctrl_ns.Write(outbytes, 0, outbytes.Length);
  125.                     listBox1.Items.Add("odebrano: END");
  126.                     listBox1.Items.Add("wysłano: ENDACK");
  127.                     t.Stop();
  128.                     t2.Stop();
  129.                     t3.Start();
  130.                 }
  131.             }
  132.         }
  133.  
  134.         private void koniec(object sender, EventArgs e)
  135.         {
  136.             if (ctrl_ns.DataAvailable)
  137.             {
  138.                 //tablice na dane przychodzące i wychodzące
  139.                 byte[] inbytes = new byte[1024];
  140.                 //Wysyłanie i odbieranie danych
  141.                 ctrl_ns.Read(inbytes, 0, inbytes.Length);
  142.                 //Konwersja danych odebranych do postaci łańcucha znaków
  143.                 string tekst = Encoding.ASCII.GetString(inbytes);
  144.  
  145.                 if (tekst.IndexOf("ACKOK") != -1)
  146.                 {
  147.                     listBox1.Items.Add("odebrano: ACKOK");
  148.                     t3.Stop();
  149.                     ctrl_ns.Flush();
  150.                     serwer.Stop();
  151.  
  152.                     ctrl_serwer.Stop();
  153.  
  154.                     listBox1.Items.Add("Zakończono pracę serwera ...");
  155.                     button1.Enabled = true;
  156.                     button2.Enabled = false;
  157.                     ns.Close();
  158.                     ctrl_ns.Close();
  159.                 }
  160.             }
  161.         }
  162.  
  163.         private void button2_Click(object sender, EventArgs e)
  164.         {            
  165.             serwer.Stop();
  166.             klient.Close();
  167.             ctrl_serwer.Stop();
  168.             ctrl_klient.Close();
  169.             listBox1.Items.Add("Zakończono pracę serwera ...");
  170.             button1.Enabled = true;
  171.             button2.Enabled = false;
  172.             t.Stop();
  173.             t2.Stop();
  174.             ns.Close();
  175.             ctrl_ns.Close();
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement