建议你用.net remoting,减少你的烦恼

解决方案 »

  1.   

    TO:Soking(方呈怡心)
    能给出条好的例子来吗!?TO: MH2o(小涛) 
    这个也是偶照别人抄的,就是想试试,能不能用!
      

  2.   

    //serverusing System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Threading;
    using System.Net.Sockets;
    using System.IO;namespace Server
    {
    public class Server : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox inputTextBox;
    private System.Windows.Forms.TextBox displayTextBox;
    private Socket connection;
    private Thread readThread;
    private NetworkStream socketStream;
    private BinaryWriter writer;
    private BinaryReader reader;
    private System.ComponentModel.Container components = null;
    public Server()
    { InitializeComponent(); //创建一个新的线程
    readThread=new Thread(new ThreadStart(RunServer));
    readThread.Start();
    } protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.inputTextBox = new System.Windows.Forms.TextBox();
    this.displayTextBox = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // inputTextBox
    // 
    this.inputTextBox.Dock = System.Windows.Forms.DockStyle.Top;
    this.inputTextBox.Name = "inputTextBox";
    this.inputTextBox.Size = new System.Drawing.Size(292, 21);
    this.inputTextBox.TabIndex = 0;
    this.inputTextBox.Text = "";
    this.inputTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.inputTextBox_KeyDown);
    // 
    // displayTextBox
    // 
    this.displayTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
    this.displayTextBox.Location = new System.Drawing.Point(0, 21);
    this.displayTextBox.Multiline = true;
    this.displayTextBox.Name = "displayTextBox";
    this.displayTextBox.Size = new System.Drawing.Size(292, 252);
    this.displayTextBox.TabIndex = 1;
    this.displayTextBox.Text = "";
    // 
    // Server
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.displayTextBox,
      this.inputTextBox});
    this.Name = "Server";
    this.Text = "Server";
    this.Closing += new System.ComponentModel.CancelEventHandler(this.Server_Closing);
    this.ResumeLayout(false); }
    #endregion
    [STAThread]
    static void Main() 
    {
    Application.Run(new Server());
    } protected void Server_Closing(object sender,CancelEventArgs e)
    {
    System.Environment.Exit(System.Environment.ExitCode);
    } //发送信息到客户端
    protected void inputTextBox_KeyDown(object sender,KeyEventArgs e)
    {
    try
    {
    if(e.KeyCode==Keys.Enter&&connection!=null)
    {
    writer.Write("SERVER>>>"+inputTextBox.Text);
    displayTextBox.Text+="\r\nSERVER>>>"+inputTextBox.Text; if(inputTextBox.Text=="TERMINATE") //断开连接
    connection.Close(); inputTextBox.Clear();
    }
    }
    catch(SocketException)
    {
    displayTextBox.Text+="\nError writing object";
    }
    } public void RunServer()
    {
    TcpListener listener;
    int counter=1; try
    {
    //1 建立TcpListener类对象
    listener=new TcpListener(5000); //2 开始监听
    listener.Start(); //3 建立服务器和客户端的连接
    while(true)
    {
    displayTextBox.Text="Waiting for connection\r\n"; connection=listener.AcceptSocket(); //根据建立的连接返回Socket对象 socketStream=new NetworkStream(connection); //网络流 writer=new BinaryWriter(socketStream); //向网络流写入字节
    reader=new BinaryReader(socketStream); //从网络流读取字节 displayTextBox.Text+="Connection "+counter+" received.\r\n"; writer.Write("SERVER>>>Connection successful"); inputTextBox.ReadOnly=false;
    string theReply=""; //4 从客户端读取信息
    do
    {
    try
    {
    theReply=reader.ReadString(); //读取发送到服务器的字串 displayTextBox.Text+="\r\n"+theReply; //显示
    }
    catch(Exception)
    {
    break;
    }
    }
    while(theReply!="CLIENT>>>TERMINATE"&&connection.Connected); inputTextBox.Text+="\r\nUser termainated connection"; inputTextBox.ReadOnly=true; //5 关闭连接
    writer.Close();
    reader.Close();
    socketStream.Close();
    connection.Close(); ++counter;
    }
    }
    catch(Exception error)
    {
    MessageBox.Show(error.ToString());
    }
    }
    }
    }
      

  3.   

    //clientusing System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Threading;
    using System.Net.Sockets;
    using System.IO;namespace Client
    {
    public class Client : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox inputTextBox;
    private System.Windows.Forms.TextBox displayTextBox; private NetworkStream output;
    private BinaryWriter writer;
    private BinaryReader reader;
    private string message="";
    private Thread readThread;
    private System.ComponentModel.Container components = null; public Client()
    {
    InitializeComponent(); readThread=new Thread(new ThreadStart(RunClient));
    readThread.Start();
    } protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.inputTextBox = new System.Windows.Forms.TextBox();
    this.displayTextBox = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // inputTextBox
    // 
    this.inputTextBox.Dock = System.Windows.Forms.DockStyle.Top;
    this.inputTextBox.Name = "inputTextBox";
    this.inputTextBox.Size = new System.Drawing.Size(292, 21);
    this.inputTextBox.TabIndex = 0;
    this.inputTextBox.Text = "";
    this.inputTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.inputTextBox_KeyDown);
    // 
    // displayTextBox
    // 
    this.displayTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
    this.displayTextBox.Location = new System.Drawing.Point(0, 21);
    this.displayTextBox.Multiline = true;
    this.displayTextBox.Name = "displayTextBox";
    this.displayTextBox.Size = new System.Drawing.Size(292, 252);
    this.displayTextBox.TabIndex = 1;
    this.displayTextBox.Text = "";
    // 
    // Client
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.displayTextBox,
      this.inputTextBox});
    this.Name = "Client";
    this.Text = "Client";
    this.Closing += new System.ComponentModel.CancelEventHandler(this.Client_Closing);
    this.ResumeLayout(false); }
    #endregion [STAThread]
    static void Main() 
    {
    Application.Run(new Client());
    } private void Client_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    System.Environment.Exit(System.Environment.ExitCode);
    } //发送信息到服务器
    private void inputTextBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    try
    {
    if(e.KeyCode==Keys.Enter)
    {
    writer.Write("CLIENT>>>"+inputTextBox.Text);
    displayTextBox.Text+="\r\nCLIENT>>>"+inputTextBox.Text;
    inputTextBox.Clear();
    }
    }
    catch(SocketException ioe)
    {
    displayTextBox.Text+="\nError writing object";
    }
    } //连接到服务器并显示服务器端发送的信息
    public void RunClient()
    {
    TcpClient client; try
    {
    displayTextBox.Text+="Attempting connection\r\n"; //1 创建TcpClient对象
    client=new TcpClient();
    //连接到服务器
    client.Connect("localhost",5000); //2 获得网络流
    output=client.GetStream(); writer=new BinaryWriter(output); reader=new BinaryReader(output); displayTextBox.Text+="\r\nGot I/O streams\r\n"; inputTextBox.ReadOnly=false; do
    {
    try
    {
    //3 处理阶段
    message=reader.ReadString();
    displayTextBox.Text+="\r\n"+message;
    }
    catch(Exception)
    {
    System.Environment.Exit(System.Environment.ExitCode);
    }
    }
    while(message!="SERVER>>>TERMINAATE"); displayTextBox.Text+="\r\nClosing connection.\r\n"; //4 关闭连接
    writer.Close();
    reader.Close();
    output.Close();
    client.Close();
    Application.Exit();
    }
    catch(Exception error)
    {
    MessageBox.Show(error.ToString());
    }
    }
    }
    }
      

  4.   

    TO:lovered()
    不好意思,偶需要的是针对WEBFORM的而不是WEINFORM的!
    谢谢。
      

  5.   

    我的Mail:[email protected]
    我给你一个由我封装的Socket类,支持事件,很好用
      

  6.   

    谢谢!有代码吗!?
    [email protected]
      

  7.   

    rroo(小巫):
    你的实现什么功能?
      

  8.   

    To rroo(小巫)
    能给偶一份吗?谢谢
      

  9.   

    To rroo(小巫)
    能也给我一份吗? 或 [email protected]
    谢谢!!!!!
      

  10.   

    普通Socket通信,通過事件來獲取接收到的數據