我写了一个udp程序,运行时两端都收不到对方发送的信息。小弟刚刚学socket编程,弄了好久没有搞定。
以下是源代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace UDP程序开发
{
public partial class Form1 : Form
{
private UdpClient client2;
private IPEndPoint remotehost;
private int port = 2456;
private Thread startServer;
private string sendMessage;
private string ipadd = "192.168.0.19";
public Form1()
{
InitializeComponent();
} private void start_server()
{
while (true)
{
Byte[] recData = client2.Receive(ref remotehost);
string Message = System.Text.Encoding.BigEndianUnicode.GetString(recData);
this.richTextBox1.AppendText(Message);
}
} private void run()
{
client2 = new UdpClient(port);
remotehost = new IPEndPoint(IPAddress.Parse(ipadd),0);
startServer=new Thread(new ThreadStart(start_server));
startServer.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
run();
} private void send_Click(object sender, EventArgs e)
{
if (this.richTextBox2.Text.Equals(""))
{
MessageBox.Show("不能为空,请输入要发送的文字!");
}
else
{
//run();
sendMessage = this.richTextBox2.Text;
Byte[] sendData = System.Text.Encoding.BigEndianUnicode.GetBytes(sendMessage.ToCharArray());
client2.Connect(remotehost);
client2.Send(sendData, sendData.Length);
DateTime time = DateTime.Now;
this.richTextBox1.AppendText("Client2:"+Convert.ToString(time) + "\n" + sendMessage + "\n");
}
} private void sendData()
{
try
{
Byte[] sendData = System.Text.Encoding.BigEndianUnicode.GetBytes(sendMessage.ToCharArray());
client2.Connect(remotehost);
client2.Send(sendData, sendData.Length);
DateTime time = DateTime.Now;
//this.richTextBox1.AppendText(Convert.ToString(time) + "\n" + sendMessage + "\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
} private void close_Click(object sender, EventArgs e)
{
try
{
client2.Close();
}
catch
{
}
this.Dispose();
this.Close();
}
}
}
以下是源代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace UDP程序开发
{
public partial class Form1 : Form
{
private UdpClient client2;
private IPEndPoint remotehost;
private int port = 2456;
private Thread startServer;
private string sendMessage;
private string ipadd = "192.168.0.19";
public Form1()
{
InitializeComponent();
} private void start_server()
{
while (true)
{
Byte[] recData = client2.Receive(ref remotehost);
string Message = System.Text.Encoding.BigEndianUnicode.GetString(recData);
this.richTextBox1.AppendText(Message);
}
} private void run()
{
client2 = new UdpClient(port);
remotehost = new IPEndPoint(IPAddress.Parse(ipadd),0);
startServer=new Thread(new ThreadStart(start_server));
startServer.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
run();
} private void send_Click(object sender, EventArgs e)
{
if (this.richTextBox2.Text.Equals(""))
{
MessageBox.Show("不能为空,请输入要发送的文字!");
}
else
{
//run();
sendMessage = this.richTextBox2.Text;
Byte[] sendData = System.Text.Encoding.BigEndianUnicode.GetBytes(sendMessage.ToCharArray());
client2.Connect(remotehost);
client2.Send(sendData, sendData.Length);
DateTime time = DateTime.Now;
this.richTextBox1.AppendText("Client2:"+Convert.ToString(time) + "\n" + sendMessage + "\n");
}
} private void sendData()
{
try
{
Byte[] sendData = System.Text.Encoding.BigEndianUnicode.GetBytes(sendMessage.ToCharArray());
client2.Connect(remotehost);
client2.Send(sendData, sendData.Length);
DateTime time = DateTime.Now;
//this.richTextBox1.AppendText(Convert.ToString(time) + "\n" + sendMessage + "\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
} private void close_Click(object sender, EventArgs e)
{
try
{
client2.Close();
}
catch
{
}
this.Dispose();
this.Close();
}
}
}
解决方案 »
- 请问C#怎么打印mschart的图表区啊?
- PictureBox图片尺寸拉伸问题
- Excel表中如何快速查找
- 数据结构单链表倒置问题
- [求助]我写的程序有点问题,请大家指教一下,感激不尽啊!
- 谢谢回答 java手机开发
- 兄弟们大家的VS2005是什么版本号
- winform中datagrid中增加一checkbox列问题,看看我的代码,为什么还是没加上呢?
- 请问在WORD中怎么用手写笔,我是用C#来开发WORRD的二次开发的,来者有分
- 我在工程中添加了一个bmp图象,作为嵌入资源?我如何用编程的方式把图像取出来,放到一个picture中?
- word 2003 与部分软件冲突问题,造成鼠标在word工作区不能使用,但是键盘上还是可以用的,鼠标在word 工作区外,比如word工具栏仍能用,
- datagridview赋值
{
//在本机指定的端口接收
IPEndPoint remoteIpEndIPoint = new IPEndPoint(IPAddress.Any,1000);
udpReceive = new UdpClient(remoteIpEndIPoint);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);
//接收从远程主机发送过来的信息
while (true)
{
//ref表示引用类型的 IPPoint实例接收消息
byte[] receiveBytes = udpReceive.Receive(ref iep);
for (int i = 0; i < receiveBytes.Length; i++)
{
RetStr += String.Format("{0:X2} ", receiveBytes[i]);
}
MessageBox.Show(RetStr);
}
}
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.123.168"), 10006);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(this.textBox3.Text);
//设置重传次数
int retry = 0;
while (true)
{
//发送组播信息
udpSend.Send(bytes, bytes.Length, iep);
break;
if (retry < 3)
{
retry++;
continue;
}
else
{
DialogResult result = MessageBox.Show("发送失败!");
break;
}
}
你把两个richtextbox分别加进去,
再把接受函数写入线程里面,就可以了