本人分别写了客户端和服务器下的代码,程序的目的是客户端和服务器可以互相发送信息。但程序编译能通过,但总是不能传送信息,希望各位高手能给出指点,谢谢!!
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
服务器端:
服务器是由两个textBox(textBox1用来指示IP地址127.0.0.1,textBox2用来指示服务器端的端口(3456)),一个listBox(listBox用来显示服务器和客户端的连接状况),两个richTextBox(richTextBox1用来显示客户端发送给服务器的信息,richTextBox2用来显示服务器发送给客户端的信息),三个button(button1用来建立连接并接收来自客户端的信息,button2用来关闭连接并停止发送,button3用来发送信息给客户端),代码如下:
private UdpClient server;
private void button1_Click(object sender, System.EventArgs e)
{ start();}
////////////
private void button2_Click(object sender, System.EventArgs e)
{ server.Close();
this.listBox1.Items.Add("The Connection canceled!");
}
////////////
private void start()
{ server=new UdpClient(3456);
IPEndPoint myhost=null;
try
{ this.listBox1.Items.Add("Waitng for a Client……");
byte[] bytes=server.Receive(ref myhost);
this.richTextBox1.Text=System.Text.Encoding.Unicode.GetString(bytes,0,bytes.Length);
this.listBox1.Items.Add("Connection Success!");
}
catch(Exception err)
{ this.listBox1.Items.Add(err.ToString()); }
}
//////////
private void button3_Click(object sender, System.EventArgs e)
{ try
{ this.listBox1.Items.Add("开始发送信息!");
byte[] bytes=Encoding.Unicode.GetBytes(this.richTextBox2.Text);
server.Send(bytes,bytes.Length,this.textBox1.Text,4500);
}
catch(Exception err)
{ MessageBox.Show(err.ToString()); }
}
////////////////////////////////////////////////////////////////////////////////////////////
客户端:
客户端是由由两个textBox(textBox1用来指示IP地址127.0.0.1,textBox2用来指示客户端的端口(4500)),一个listBox(listBox用来显示客户端的状态),两个richTextBox(richTextBox1用来显示服务器发送给客户端的信息,richTextBox2用来显示客户端发送给服务器的信息),两个button(button1用来建立与服务器连接并发送和接收信息,button2用来关闭连接并停止发送),当然在运行时得先运行客户端。代码如下:
private UdpClient client;
//////////////
private void button1_Click(object sender, System.EventArgs e)
{ client=new UdpClient(4500);
try
{ this.listBox1.Items.Add("正在向服务器发送数据……");
byte[] bytes=System.Text.Encoding.Unicode.GetBytes(this.richTextBox2.Text);
IPEndPoint host=null;
byte[] response=client.Receive(ref host);
this.richTextBox1.Text=System.Text.Encoding.Unicode.GetString(response);
}
catch(Exception err)
{ this.listBox1.Items.Add(err.ToString());}
}
////////////////
private void button2_Click(object sender, System.EventArgs e)
{ client.Close();
this.listBox1.Items.Add("The Connection canceled!");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
服务器端:
服务器是由两个textBox(textBox1用来指示IP地址127.0.0.1,textBox2用来指示服务器端的端口(3456)),一个listBox(listBox用来显示服务器和客户端的连接状况),两个richTextBox(richTextBox1用来显示客户端发送给服务器的信息,richTextBox2用来显示服务器发送给客户端的信息),三个button(button1用来建立连接并接收来自客户端的信息,button2用来关闭连接并停止发送,button3用来发送信息给客户端),代码如下:
private UdpClient server;
private void button1_Click(object sender, System.EventArgs e)
{ start();}
////////////
private void button2_Click(object sender, System.EventArgs e)
{ server.Close();
this.listBox1.Items.Add("The Connection canceled!");
}
////////////
private void start()
{ server=new UdpClient(3456);
IPEndPoint myhost=null;
try
{ this.listBox1.Items.Add("Waitng for a Client……");
byte[] bytes=server.Receive(ref myhost);
this.richTextBox1.Text=System.Text.Encoding.Unicode.GetString(bytes,0,bytes.Length);
this.listBox1.Items.Add("Connection Success!");
}
catch(Exception err)
{ this.listBox1.Items.Add(err.ToString()); }
}
//////////
private void button3_Click(object sender, System.EventArgs e)
{ try
{ this.listBox1.Items.Add("开始发送信息!");
byte[] bytes=Encoding.Unicode.GetBytes(this.richTextBox2.Text);
server.Send(bytes,bytes.Length,this.textBox1.Text,4500);
}
catch(Exception err)
{ MessageBox.Show(err.ToString()); }
}
////////////////////////////////////////////////////////////////////////////////////////////
客户端:
客户端是由由两个textBox(textBox1用来指示IP地址127.0.0.1,textBox2用来指示客户端的端口(4500)),一个listBox(listBox用来显示客户端的状态),两个richTextBox(richTextBox1用来显示服务器发送给客户端的信息,richTextBox2用来显示客户端发送给服务器的信息),两个button(button1用来建立与服务器连接并发送和接收信息,button2用来关闭连接并停止发送),当然在运行时得先运行客户端。代码如下:
private UdpClient client;
//////////////
private void button1_Click(object sender, System.EventArgs e)
{ client=new UdpClient(4500);
try
{ this.listBox1.Items.Add("正在向服务器发送数据……");
byte[] bytes=System.Text.Encoding.Unicode.GetBytes(this.richTextBox2.Text);
IPEndPoint host=null;
byte[] response=client.Receive(ref host);
this.richTextBox1.Text=System.Text.Encoding.Unicode.GetString(response);
}
catch(Exception err)
{ this.listBox1.Items.Add(err.ToString());}
}
////////////////
private void button2_Click(object sender, System.EventArgs e)
{ client.Close();
this.listBox1.Items.Add("The Connection canceled!");
}
解决方案 »
- API函数mouse_event问题
- 怎么把数据库中的一张数据表中的图片字段里的2进制转换成图片放到picturebox里?
- 同步Lock()
- 高分求助 DXperience控件自带的Demo Center中的XtraGird Stuite实例中的XtraGird Tutorials中各个例子的所介绍的功能
- MS-SQL游标问题
- 如何将ComboBox中的边框去掉啊,在.Net 2003中没有该属性
- 在用户选择“打开文件”时,如何生成新的窗体。
- 关于directx视频 合成问题
- 字号集合在哪里?
- c# 修改 windows 用户名和密码 方法
- Point[] pt=new Point[7]{Point……出错啦
- 用户控件数据绑定问题?
byte[] bytes=System.Text.Encoding.Unicode.GetBytes(this.richTextBox2.Text);
IPEndPoint host=null;
byte[] response=client.Receive(ref host);
正在向服务器发送数据?
应该是client.send();吧而且start();就执行一次,不能支持多个客户端,也不能支持多次通讯请求建议用多线程解决多客户端问题,用while(true){}解决连续通讯问题。