Server:namespace WindowsFormsApplication3
{
public partial class Server : Form { UdpClient uc = null; //声明UDPClient public Server() { //屏蔽跨线程改控件属性那个异常 CheckForIllegalCrossThreadCalls = false; InitializeComponent(); //注意此处端口号要与发送方相同 uc = new UdpClient(7777);
uc.Connect(IPAddress.Parse("192.168.0.10"), 8888); //开一线程 Thread th = new Thread(new ThreadStart(listen)); //设置为后台 th.IsBackground = true; th.Start(); } private void listen() {
this.listBox1.Items.Add("等待客户端连接...");
//声明终结点 IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.11"), 7777); while (true) { //获得Form1发送过来的数据包 string text = System.Text.Encoding.UTF8.GetString(uc.Receive(ref iep)); //加入ListBox this.listBox1.Items.Add(text);
this.listBox1.SelectedIndex = listBox1.Items.Count - 1; } } private void Form2_Load(object sender, EventArgs e)
{
//Form1 form1 = new Form1();
//form1.Show();
//将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes("已连接到服务器!"); //向本机的8888端口发送数据 uc.Send(b, b.Length);
} private void button1_Click(object sender, EventArgs e)
{ Client form1 = new Client();
form1.Show();
} private void button2_Click(object sender, EventArgs e)
{
string temp = this.textBox1.Text; //保存TextBox文本 //将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes(temp); //向本机的8888端口发送数据 uc.Send(b, b.Length);
} }}Client:namespace WindowsFormsApplication3
{
public partial class Client : Form
{ UdpClient uc; //声明UDPClient public Client()
{
//屏蔽跨线程改控件属性那个异常 CheckForIllegalCrossThreadCalls = false; InitializeComponent(); //注意此处端口号要与发送方相同 uc = new UdpClient(8888);
uc.Connect(IPAddress.Parse("192.168.0.11"), 7777); //开一线程 Thread th = new Thread(new ThreadStart(listen)); //设置为后台 th.IsBackground = true; th.Start();
} private void listen()
{
this.listBox1.Items.Add("连接服务器...");
//声明终结点 IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.10"), 8888);
while (true)
{ //获得Form1发送过来的数据包 string text = System.Text.Encoding.UTF8.GetString(uc.Receive(ref iep)); //加入ListBox this.listBox1.Items.Add(text);
this.listBox1.SelectedIndex = listBox1.Items.Count - 1; } }
private void Form1_Load(object sender, EventArgs e)
{
//Form2 form2 = new Form2();
//form2.Show(); //将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes("客户端已连接!"); //向本机的8888端口发送数据 uc.Send(b, b.Length);
} private void button1_Click_1(object sender, EventArgs e)
{ string temp = this.textBox1.Text; //保存TextBox文本 //将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes(temp); //向本机的8888端口发送数据 uc.Send(b, b.Length);
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes("客户端已断开!"); //向本机的8888端口发送数据 uc.Send(b, b.Length);
} }}
我想做个聊天程序,用的是UDP,两边都有listbox和textbox,但是连不通,谁知道是问什么啊??
{
public partial class Server : Form { UdpClient uc = null; //声明UDPClient public Server() { //屏蔽跨线程改控件属性那个异常 CheckForIllegalCrossThreadCalls = false; InitializeComponent(); //注意此处端口号要与发送方相同 uc = new UdpClient(7777);
uc.Connect(IPAddress.Parse("192.168.0.10"), 8888); //开一线程 Thread th = new Thread(new ThreadStart(listen)); //设置为后台 th.IsBackground = true; th.Start(); } private void listen() {
this.listBox1.Items.Add("等待客户端连接...");
//声明终结点 IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.11"), 7777); while (true) { //获得Form1发送过来的数据包 string text = System.Text.Encoding.UTF8.GetString(uc.Receive(ref iep)); //加入ListBox this.listBox1.Items.Add(text);
this.listBox1.SelectedIndex = listBox1.Items.Count - 1; } } private void Form2_Load(object sender, EventArgs e)
{
//Form1 form1 = new Form1();
//form1.Show();
//将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes("已连接到服务器!"); //向本机的8888端口发送数据 uc.Send(b, b.Length);
} private void button1_Click(object sender, EventArgs e)
{ Client form1 = new Client();
form1.Show();
} private void button2_Click(object sender, EventArgs e)
{
string temp = this.textBox1.Text; //保存TextBox文本 //将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes(temp); //向本机的8888端口发送数据 uc.Send(b, b.Length);
} }}Client:namespace WindowsFormsApplication3
{
public partial class Client : Form
{ UdpClient uc; //声明UDPClient public Client()
{
//屏蔽跨线程改控件属性那个异常 CheckForIllegalCrossThreadCalls = false; InitializeComponent(); //注意此处端口号要与发送方相同 uc = new UdpClient(8888);
uc.Connect(IPAddress.Parse("192.168.0.11"), 7777); //开一线程 Thread th = new Thread(new ThreadStart(listen)); //设置为后台 th.IsBackground = true; th.Start();
} private void listen()
{
this.listBox1.Items.Add("连接服务器...");
//声明终结点 IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.10"), 8888);
while (true)
{ //获得Form1发送过来的数据包 string text = System.Text.Encoding.UTF8.GetString(uc.Receive(ref iep)); //加入ListBox this.listBox1.Items.Add(text);
this.listBox1.SelectedIndex = listBox1.Items.Count - 1; } }
private void Form1_Load(object sender, EventArgs e)
{
//Form2 form2 = new Form2();
//form2.Show(); //将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes("客户端已连接!"); //向本机的8888端口发送数据 uc.Send(b, b.Length);
} private void button1_Click_1(object sender, EventArgs e)
{ string temp = this.textBox1.Text; //保存TextBox文本 //将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes(temp); //向本机的8888端口发送数据 uc.Send(b, b.Length);
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes("客户端已断开!"); //向本机的8888端口发送数据 uc.Send(b, b.Length);
} }}
我想做个聊天程序,用的是UDP,两边都有listbox和textbox,但是连不通,谁知道是问什么啊??
IPAddress.Parse("127.0.0.1"), 8888
用这个试试