Advertisement
Konark

Untitled

Dec 3rd, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 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;
  11. using System.Net.Sockets;
  12. using System.IO;
  13.  
  14. namespace WindowsFormsApplication5
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         private TcpClient client;
  19.         public StreamReader STR;
  20.         public StreamWriter STW;
  21.         public string receive;
  22.         public String text_to_send;
  23.  
  24.         public Form1()
  25.         {
  26.             InitializeComponent();
  27.             IPAddress[] localIP = Dns.GetHostAddresses(Dns.GetHostName());
  28.             //String AdressStr = "";
  29.             foreach(IPAddress address in localIP)
  30.             {
  31.                 if (address.AddressFamily == AddressFamily.InterNetwork)
  32.                 {
  33.                     textBox3.Text = address.ToString();
  34.                 }
  35.             }
  36.         }
  37.  
  38.         private void button3_Click(object sender, EventArgs e)
  39.         {
  40.             client = new TcpClient();
  41.             IPEndPoint IP_End = new IPEndPoint(IPAddress.Parse(textBox5.Text), int.Parse(textBox6.Text));
  42.  
  43.             try
  44.             {
  45.                 client.Connect(IP_End);
  46.                 if (client.Connected)
  47.                 {
  48.                     textBox2.AppendText("Присоеденен к серверу" + "\n");
  49.                     STW = new StreamWriter(client.GetStream());
  50.                     STR = new StreamReader(client.GetStream());
  51.                     STW.AutoFlush = true;
  52.  
  53.                     backgroundWorker1.RunWorkerAsync();
  54.                     backgroundWorker2.WorkerSupportsCancellation = true;
  55.                 }
  56.             }catch(Exception x)
  57.             {
  58.                 MessageBox.Show(x.Message.ToString());
  59.             }
  60.         }
  61.  
  62.         private void button2_Click(object sender, EventArgs e) //start
  63.         {
  64.             TcpListener listener = new TcpListener(IPAddress.Any, int.Parse(textBox4.Text));
  65.             listener.Start();
  66.             client = listener.AcceptTcpClient();
  67.             STR = new StreamReader(client.GetStream());
  68.             STW = new StreamWriter(client.GetStream());
  69.             STW.AutoFlush = true;
  70.  
  71.             backgroundWorker1.RunWorkerAsync();                  // Start reveiving data in background
  72.             backgroundWorker2.WorkerSupportsCancellation = true; // Ability to cancel this thread
  73.  
  74.  
  75.         }
  76.  
  77.         private void label1_Click(object sender, EventArgs e)
  78.         {
  79.  
  80.         }
  81.  
  82.         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) // recive data
  83.         {
  84.             while (client.Connected)
  85.             {
  86.                 try
  87.                 {
  88.                     receive = STR.ReadLine();
  89.                     this.textBox2.Invoke(new MethodInvoker(delegate () { textBox2.AppendText("Вы : " + receive + "\n"); }));
  90.                     receive = "";
  91.                 }catch(Exception x)
  92.                 {
  93.                     MessageBox.Show(x.Message.ToString());
  94.                 }
  95.             }
  96.         }
  97.  
  98.         private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) // send data
  99.         {
  100.             if (client.Connected)
  101.             {
  102.                 STW.WriteLine(text_to_send);
  103.                 this.textBox2.Invoke(new MethodInvoker(delegate () { textBox2.AppendText("Я: " + text_to_send + "\n"); }));
  104.             }
  105.             else
  106.             {
  107.                 MessageBox.Show("Ошибка отправки!");
  108.             }
  109.             backgroundWorker2.CancelAsync();
  110.         }
  111.  
  112.         private void button1_Click(object sender, EventArgs e) // Send
  113.         {
  114.             if(textBox1.Text != "")
  115.             {
  116.                 text_to_send = textBox1.Text;
  117.                 backgroundWorker2.RunWorkerAsync();
  118.             }
  119.             textBox1.Text = "";
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement