Advertisement
PomozMi

PoloczenieTCPKlient

Nov 24th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 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.  
  12. namespace PoloczenieTCPKlient
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public static Timer t = new Timer();
  17.         public static Timer t2 = new Timer();
  18.         public NetworkStream ns = null;
  19.         public NetworkStream ctrl_ns = null;
  20.         public TcpClient klient = null;
  21.         public TcpClient ctrl_klient = null;
  22.  
  23.         public Form1()
  24.         {
  25.             InitializeComponent();
  26.  
  27.             t.Interval = 100; //wartość timera wysyłającego komuniakty
  28.             t.Tick += new EventHandler(this.komunikaty); //metoda wysyłana przez timer
  29.             t2.Interval = 200; //wartość timera wysyłającego komuniakty
  30.             t2.Tick += new EventHandler(this.endack); //metoda wysyłana przez timer
  31.         }
  32.  
  33.         private void button1_Click(object sender, EventArgs e)
  34.         {
  35.             string host = textBox1.Text;
  36.             int port = System.Convert.ToInt16(numericUpDown1.Value);            
  37.             try
  38.             {
  39.                 klient = new TcpClient(host, port);
  40.                 ctrl_klient = new TcpClient(host, port+1);
  41.                 listBox1.Items.Add("Nawiązano połączenie z " + host + " na porcie: " + port);
  42.                 ns = klient.GetStream(); //Pobranie referencji do obiektu strumienia
  43.                 ctrl_ns = ctrl_klient.GetStream(); //Pobranie referencji do obiektu strumienia
  44.             }
  45.             catch(Exception ex)
  46.             {
  47.                 listBox1.Items.Add("Błąd: Nie udało się nawiązać połączenia!");
  48.              
  49.                 MessageBox.Show(ex.ToString()); // wyświetlenie iformacji o błędzie połączenia
  50.             }
  51.            
  52.             t.Start();//start timera (wartości w konstruktorze)
  53.         }
  54.         private void komunikaty(object sender, EventArgs e)
  55.         {
  56.             if (ns.DataAvailable)
  57.             {
  58.                 //tablice na dane przychodzące i wychodzące
  59.                 byte[] inbytes = new byte[1024];
  60.                 //Wysyłanie i odbieranie danych
  61.                 ns.Read(inbytes, 0, inbytes.Length);
  62.                 //Konwersja danych odebranych do postaci łańcucha znaków
  63.                 string tekst = Encoding.ASCII.GetString(inbytes);
  64.                 //Wyświetlenie łańcucha znaków
  65.                 listBox1.Items.Add("Odebrano: " + tekst);
  66.                 ns.Flush();
  67.  
  68.                 //potwierdzenie
  69.                 //tablice na dane przychodzące i wychodzące
  70.                 byte[] outbytes = Encoding.ASCII.GetBytes("ok");
  71.                 //Wysyłanie i odbieranie danych
  72.                 ns.Write(outbytes, 0, outbytes.Length);
  73.             }
  74.         }
  75.  
  76.         private void button2_Click(object sender, EventArgs e)
  77.         {
  78.             //tablice na dane przychodzące i wychodzące
  79.             byte[] outbytes = Encoding.ASCII.GetBytes("END");
  80.             //Wysyłanie i odbieranie danych
  81.             ctrl_ns.Write(outbytes, 0, outbytes.Length);
  82.             listBox1.Items.Add("wysłano: END");
  83.             t2.Start();
  84.         }
  85.  
  86.         private void endack(object sender, EventArgs e)
  87.         {
  88.             if (ctrl_ns.DataAvailable)
  89.             {
  90.                 //tablice na dane przychodzące i wychodzące
  91.                 byte[] inbytes = new byte[1024];
  92.                 //Wysyłanie i odbieranie danych
  93.                 ctrl_ns.Read(inbytes, 0, inbytes.Length);
  94.                 //Konwersja danych odebranych do postaci łańcucha znaków
  95.                 string tekst = Encoding.ASCII.GetString(inbytes);
  96.  
  97.                 if (tekst.IndexOf("ENDACK") != -1)
  98.                 {
  99.                     ctrl_ns.Flush();                    
  100.                     listBox1.Items.Add("odebrano: ENDACK");
  101.                     //tablice na dane przychodzące i wychodzące
  102.                     byte[] outbytes = Encoding.ASCII.GetBytes("ACKOK");
  103.                     //Wysyłanie i odbieranie danych
  104.                     ctrl_ns.Write(outbytes, 0, outbytes.Length);
  105.                     listBox1.Items.Add("wysłano: ACKOK");
  106.                     klient.Close();
  107.                     ctrl_klient.Close();
  108.                     t.Stop();
  109.                     t2.Stop();
  110.                 }
  111.             }
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement