Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- namespace WindowsFormsApplication5
- {
- public partial class Form1 : Form
- {
- private TcpClient client;
- public StreamReader STR;
- public StreamWriter STW;
- public string receive;
- public String text_to_send;
- public Form1()
- {
- InitializeComponent();
- IPAddress[] localIP = Dns.GetHostAddresses(Dns.GetHostName());
- //String AdressStr = "";
- foreach(IPAddress address in localIP)
- {
- if (address.AddressFamily == AddressFamily.InterNetwork)
- {
- textBox3.Text = address.ToString();
- }
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- client = new TcpClient();
- IPEndPoint IP_End = new IPEndPoint(IPAddress.Parse(textBox5.Text), int.Parse(textBox6.Text));
- try
- {
- client.Connect(IP_End);
- if (client.Connected)
- {
- textBox2.AppendText("Присоеденен к серверу" + "\n");
- STW = new StreamWriter(client.GetStream());
- STR = new StreamReader(client.GetStream());
- STW.AutoFlush = true;
- backgroundWorker1.RunWorkerAsync();
- backgroundWorker2.WorkerSupportsCancellation = true;
- }
- }catch(Exception x)
- {
- MessageBox.Show(x.Message.ToString());
- }
- }
- private void button2_Click(object sender, EventArgs e) //start
- {
- TcpListener listener = new TcpListener(IPAddress.Any, int.Parse(textBox4.Text));
- listener.Start();
- client = listener.AcceptTcpClient();
- STR = new StreamReader(client.GetStream());
- STW = new StreamWriter(client.GetStream());
- STW.AutoFlush = true;
- backgroundWorker1.RunWorkerAsync(); // Start reveiving data in background
- backgroundWorker2.WorkerSupportsCancellation = true; // Ability to cancel this thread
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) // recive data
- {
- while (client.Connected)
- {
- try
- {
- receive = STR.ReadLine();
- this.textBox2.Invoke(new MethodInvoker(delegate () { textBox2.AppendText("Вы : " + receive + "\n"); }));
- receive = "";
- }catch(Exception x)
- {
- MessageBox.Show(x.Message.ToString());
- }
- }
- }
- private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) // send data
- {
- if (client.Connected)
- {
- STW.WriteLine(text_to_send);
- this.textBox2.Invoke(new MethodInvoker(delegate () { textBox2.AppendText("Я: " + text_to_send + "\n"); }));
- }
- else
- {
- MessageBox.Show("Ошибка отправки!");
- }
- backgroundWorker2.CancelAsync();
- }
- private void button1_Click(object sender, EventArgs e) // Send
- {
- if(textBox1.Text != "")
- {
- text_to_send = textBox1.Text;
- backgroundWorker2.RunWorkerAsync();
- }
- textBox1.Text = "";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement