Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace point_talk_to_face
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Thread th;
UdpClient udp, udp2;
bool l = true;
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
th = new Thread(new ThreadStart(Listen));
th.Start();
}
private void button1_Click(object sender, EventArgs e)
{
/*****************************SEND****************************************************/
udp2 = new UdpClient(textBox3.Text,9999);
Byte[] data = Encoding.ASCII.GetBytes(textBox2.Text);
udp2.Send(data, data.Length);
textBox1.AppendText("我对"+textBox3.Text+"说:"+textBox2.Text+"\n");
udp2.Close();
this.textBox2.Clear();
textBox2.Focus();
/**********************************SEND*****************************************************/
}
private void Listen()
{
/********************************************RECEIVE**************************************************/
udp = new UdpClient(9999);
IPEndPoint remoteiep = new IPEndPoint(IPAddress.Any, 9999);
while (l)
{
try
{
Byte[] data = udp.Receive(ref remoteiep);
if (data.Length > 0)
{
string word = Encoding.ASCII.GetString(data);
label1.Text = remoteiep.Address.ToString();
label2.Text = remoteiep.Port.ToString();
textBox1.AppendText(label1.Text + "对我说:" + word.ToString() + "\n");
}
else
{
}
}
catch (Exception ex)
{
// textBox1.AppendText(ex.Message);
// MessageBox.Show(ex.Message.ToString());
}
}
/*******************************************RECEIVE***********************************************************/
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void close(object sender, FormClosingEventArgs e)
{
th.Abort();
l = false;
udp.Close();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace point_talk_to_face
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Thread th;
UdpClient udp, udp2;
bool l = true;
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
th = new Thread(new ThreadStart(Listen));
th.Start();
}
private void button1_Click(object sender, EventArgs e)
{
/*****************************SEND****************************************************/
udp2 = new UdpClient(textBox3.Text,9999);
Byte[] data = Encoding.ASCII.GetBytes(textBox2.Text);
udp2.Send(data, data.Length);
textBox1.AppendText("我对"+textBox3.Text+"说:"+textBox2.Text+"\n");
udp2.Close();
this.textBox2.Clear();
textBox2.Focus();
/**********************************SEND*****************************************************/
}
private void Listen()
{
/********************************************RECEIVE**************************************************/
udp = new UdpClient(9999);
IPEndPoint remoteiep = new IPEndPoint(IPAddress.Any, 9999);
while (l)
{
try
{
Byte[] data = udp.Receive(ref remoteiep);
if (data.Length > 0)
{
string word = Encoding.ASCII.GetString(data);
label1.Text = remoteiep.Address.ToString();
label2.Text = remoteiep.Port.ToString();
textBox1.AppendText(label1.Text + "对我说:" + word.ToString() + "\n");
}
else
{
}
}
catch (Exception ex)
{
// textBox1.AppendText(ex.Message);
// MessageBox.Show(ex.Message.ToString());
}
}
/*******************************************RECEIVE***********************************************************/
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void close(object sender, FormClosingEventArgs e)
{
th.Abort();
l = false;
udp.Close();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
}
}
}