高手们,我正在和几个学机械的研究生做一个项目,我需要边一个程序,一台服务器,三台客户机。客户机每10秒钟向服务器发一次数据。这个每10秒应该怎么实现啊,。其中客户机发数据我暂时用button_Click实现,能用时间事件实现吗???
  还有就是一台服务器响应三台服务器是不是要用到多线程技术,应该怎么实现??
我的程序如下:using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Text;namespace AsyncClient
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListBox results;
private System.Windows.Forms.TextBox conStatus;
private Socket client;
private byte[] data=new byte[1024];
private int size=1024;
private System.Windows.Forms.Button button3;
Random randomObject=new Random();
private System.Windows.Forms.Button button4; /// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.results = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.conStatus = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(56, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "连接服务器";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(192, 24);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "传输数据";
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// results
// 
this.results.ItemHeight = 12;
this.results.Location = new System.Drawing.Point(0, 88);
this.results.Name = "results";
this.results.Size = new System.Drawing.Size(576, 208);
this.results.TabIndex = 2;
this.results.SelectedIndexChanged += new System.EventHandler(this.results_SelectedIndexChanged);
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(64, 312);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 3;
this.label1.Text = "连接状态";
this.label1.Click += new System.EventHandler(this.label1_Click);
// 
// conStatus
// 
this.conStatus.Location = new System.Drawing.Point(152, 312);
this.conStatus.Name = "conStatus";
this.conStatus.Size = new System.Drawing.Size(224, 21);
this.conStatus.TabIndex = 4;
this.conStatus.Text = "";
// 
// button3
// 
this.button3.Location = new System.Drawing.Point(344, 24);
this.button3.Name = "button3";
this.button3.TabIndex = 5;
this.button3.Text = "断开服务器";
this.button3.Click += new System.EventHandler(this.button3_Click);
// 
// button4
// 
this.button4.Location = new System.Drawing.Point(480, 24);
this.button4.Name = "button4";
this.button4.TabIndex = 6;
this.button4.Text = "关闭客户机";
this.button4.Click += new System.EventHandler(this.button4_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(584, 342);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.conStatus);
this.Controls.Add(this.label1);
this.Controls.Add(this.results);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void label1_Click(object sender, System.EventArgs e)
{

} private void Form1_Load(object sender, System.EventArgs e)
{

} private void button1_Click(object sender, System.EventArgs e)
{
conStatus.Text="连接服务器......";
Socket newsock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint iep=new IPEndPoint(IPAddress.Parse("127.0.0.1"),9050);
newsock.BeginConnect(iep,new AsyncCallback(Connected),newsock);
} private void button2_Click(object sender, System.EventArgs e)
{
int randValue=randomObject.Next(1,100);
string stingForm=randValue.ToString();
byte[] message=Encoding.ASCII.GetBytes(stingForm);
client.BeginSend(message,0,message.Length,SocketFlags.None,new AsyncCallback(SendData),client);
} private void button3_Click(object sender, System.EventArgs e)
{
client.Close();
conStatus.Text="已断开服务器"; }
void Connected(IAsyncResult iar)
{
client=(Socket)iar.AsyncState;
try
{
client.EndConnect(iar);
conStatus.Text="连接到:"+client.RemoteEndPoint.ToString();
client.BeginReceive(data,0,size,SocketFlags.None,new AsyncCallback(ReceiveData),client);
}//try
catch(SocketException)
{
conStatus.Text="连接错误";
}//catch
}//Conn
void ReceiveData(IAsyncResult iar)
{
Socket remote=(Socket)iar.AsyncState;
int recv=remote.EndReceive(iar);
string stringData=Encoding.ASCII.GetString(data,0,recv);
results.Items.Add(stringData);
}
void SendData(IAsyncResult iar)
{
Socket remote=(Socket)iar.AsyncState;
int sent=remote.EndSend(iar);
remote.BeginReceive(data,0,size,SocketFlags.None,new AsyncCallback(ReceiveData),remote);
}//Sen private void results_SelectedIndexChanged(object sender, System.EventArgs e)
{

} private void button4_Click(object sender, System.EventArgs e)
{
Close();
}//Rece
}
}
看这个private void button2_Click(object sender, System.EventArgs e)
{
int randValue=randomObject.Next(1,100);
string stingForm=randValue.ToString();
byte[] message=Encoding.ASCII.GetBytes(stingForm);
client.BeginSend(message,0,message.Length,SocketFlags.None,new AsyncCallback(SendData),client);
}
能不能用能用时间事件代替????  谢谢!!!

解决方案 »

  1.   

    用System.Timers.Timer的对象。
    处理你申明的对象的Elapsed事件。
    应该可以满足你的要求。
      

  2.   

    先放个timer控件// 设置timer1类的时间间隔为一秒 1000毫秒为1秒.tiemer为一个控件
    timer1.Interval = 1000;// 3600000;
    // 将timer设为可用
    timer1.Enabled = true;//在这个时间中写你的语句
    private void timer1_Tick(object sender, System.EventArgs e)


    }
      

  3.   

    用System.Timers.Timer的对象。
    处理你申明的对象的Elapsed事件。
    应该可以满足你的要求。
    能说具体点吗??怎么用呢???
      

  4.   

    新建timer对象
    timer.Interval = 10000;
    timer.Elapsed += (vs.net给你自动生成了,按tab键就可以啦)
      

  5.   

    先放个timer控件// 设置timer1类的时间间隔为一秒 1000毫秒为1秒.tiemer为一个控件
    timer1.Interval = 1000;// 3600000;
    // 将timer设为可用
    timer1.Enabled = true;//在这个时间中写你的语句
    private void timer1_Tick(object sender, System.EventArgs e)


    }
    这个方法好像产生不了事件啊,不会传送,用简单的程序试了一下也不行啊!!!
    程序如下不知道有什么问题???
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace Timer_try2
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>


    private System.Windows.Forms.Timer timer1;
    private System.ComponentModel.IContainer components; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    this.timer2 = new System.Windows.Forms.Timer(this.components);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    this.timer1.Interval= 1000;
    this.timer1.Enabled = true;

    }
    private void timer1_Tick(object sender, System.EventArgs e)
    {
    MessageBox.Show("1s");

        } }
    }