using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;//程序中使用到编码
using System.Windows.Forms;
//add 2009-2-16using System.Net;
using System.Net.Sockets;
using System.Threading;//程序中使用到线程
namespace UDP对时服务器端
{
public partial class Form1 : Form
{
private UdpClient server ;
private IPEndPoint receivePoint ;
private int port = 8080 ; //定义端口号
private string ip = "127.0.0.1" ;//设定本地IP地址
private Thread startServer ; public Form1()
{
InitializeComponent();
}
public void start_server ( )
{
while ( true )
{
//接收从远程主机发送到本地8080端口的数据
byte[] recData = server.Receive ( ref receivePoint ) ;
ASCIIEncoding encode = new ASCIIEncoding ( ) ;
//获得客户端请求数据
string Read_str = encode.GetString ( recData ) ;
//提取客户端的信息,存放到定义为temp的字符串数组中
string[] temp = Read_str.Split('/') ;
//显示端口号的请求信息
listBox1.Items.Add("时间:" + DateTime.Now.ToLongTimeString() + " 接收信息如下:");
listBox1.Items.Add("客户机:" + temp[0]);
listBox1.Items.Add("端口号:" + temp[1]);
//发送服务器端时间和日期
byte[] sendData =encode.GetBytes ( System.DateTime.Now.ToString ( ) ) ; listBox1.Items.Add("发送服务器时间!"); //对远程主机的指定端口号发送服务器时间
server.Send ( sendData , sendData.Length , temp[0] , Int32.Parse ( temp[1] ) ) ;
}
}
public void run ( )
{
//利用本地8080端口号来初始化一个UDP网络服务
server = new UdpClient ( port ) ;
IPAddress a = IPAddress.Parse(ip);
receivePoint = new IPEndPoint ( a , port ) ;
//开一个线程
startServer = new Thread ( new ThreadStart ( start_server ) ) ;
//启动线程
startServer.Start ( ) ;
} private void button1_Click(object sender, EventArgs e)
{
//清除服务器端程序日志
listBox1.Items.Clear ( ) ; } private void Form1_Load(object sender, EventArgs e)
{
//启动对时服务
run ( ) ; }
}
}
红色部分出现问题,删去则可以,
如何解决在一个线程中往主线程窗体中列表框写入信息?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;//程序中使用到编码
using System.Windows.Forms;
//add 2009-2-16using System.Net;
using System.Net.Sockets;
using System.Threading;//程序中使用到线程
namespace UDP对时服务器端
{
public partial class Form1 : Form
{
private UdpClient server ;
private IPEndPoint receivePoint ;
private int port = 8080 ; //定义端口号
private string ip = "127.0.0.1" ;//设定本地IP地址
private Thread startServer ; public Form1()
{
InitializeComponent();
}
public void start_server ( )
{
while ( true )
{
//接收从远程主机发送到本地8080端口的数据
byte[] recData = server.Receive ( ref receivePoint ) ;
ASCIIEncoding encode = new ASCIIEncoding ( ) ;
//获得客户端请求数据
string Read_str = encode.GetString ( recData ) ;
//提取客户端的信息,存放到定义为temp的字符串数组中
string[] temp = Read_str.Split('/') ;
//显示端口号的请求信息
listBox1.Items.Add("时间:" + DateTime.Now.ToLongTimeString() + " 接收信息如下:");
listBox1.Items.Add("客户机:" + temp[0]);
listBox1.Items.Add("端口号:" + temp[1]);
//发送服务器端时间和日期
byte[] sendData =encode.GetBytes ( System.DateTime.Now.ToString ( ) ) ; listBox1.Items.Add("发送服务器时间!"); //对远程主机的指定端口号发送服务器时间
server.Send ( sendData , sendData.Length , temp[0] , Int32.Parse ( temp[1] ) ) ;
}
}
public void run ( )
{
//利用本地8080端口号来初始化一个UDP网络服务
server = new UdpClient ( port ) ;
IPAddress a = IPAddress.Parse(ip);
receivePoint = new IPEndPoint ( a , port ) ;
//开一个线程
startServer = new Thread ( new ThreadStart ( start_server ) ) ;
//启动线程
startServer.Start ( ) ;
} private void button1_Click(object sender, EventArgs e)
{
//清除服务器端程序日志
listBox1.Items.Clear ( ) ; } private void Form1_Load(object sender, EventArgs e)
{
//启动对时服务
run ( ) ; }
}
}
红色部分出现问题,删去则可以,
如何解决在一个线程中往主线程窗体中列表框写入信息?
解决方案 »
- 急急急!请教高手怎么用C#合并五个bin文件并排序?
- 求助,这段代码的逆运算,应该有点难度~
- [WinForms - C#]自定义DataGridViewCell如何响应丢失焦点自动提交的功能?
- 关于WPF的问题,关于WPF的疑问,学习WPF的一起探讨。
- 在线等一个棘手的问题,请大家帮忙啊
- 我要实现像资源管理器中那可以拖动的两边,请问在c# winForm vs2005中怎么实现?我想在中间加一条线,当鼠标到那里变掉,然后可以拖动,请问
- TreeView不能正常显示(所有节点都排在了同一行)
- 这里人气真足,散分啦
- 不惜重分求e书,大家也来一起共享!
- 那里有《ASP.NET高级编程-C# 编程》下载啊
- 有没有比较好点的操作数据库接口代码,拿出来分享分享哈
- 不能调式了
{
InitializeComponent();
Check................... = false;
} check开头的一个属性 具体既不清楚了
public void Add(string Mess)
{
if (listBox1.InvokeRequired)
{
AddString add = new AddString(Add);
listBox1.Invoke(Add, new object[] { Mess });
}
else
{
listBox1.Items.Add(Mess);
}
}
调用Add添加
public void Add(string Mess)
{
if (listBox1.InvokeRequired)
{
AddString addmess = new AddString(Add);
listBox1.Invoke(addmess, new object[] { Mess });
}
else
{
listBox1.Items.Add(Mess);
}
}