如题!

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Net;
    using System.Threading;
    using System.Text;
    using System.Net.Sockets;namespace icu
    {
    /// <summary>
    /// Form1 的摘要描述。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBoxName;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox textBoxPort;
    private System.Windows.Forms.ListBox listBoxState;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.ListBox listBox2;
    private System.Windows.Forms.GroupBox groupBox3;
    private System.Windows.Forms.RichTextBox richTextBoxReceive;
    private System.Windows.Forms.Button buttonStart;
    private System.Windows.Forms.Button buttonStop; private Socket serverSocket,clientSocket;
    private const int dataSize=1024;
    private byte [] data=new byte[dataSize];
    // private System.ComponentModel.Container components=null;
    /// <summary>
    /// 設計工具所需的變數。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows Form 設計工具支援的必要項
    //
    InitializeComponent();
    this.textBoxName.Text=Dns.GetHostName();
    this.textBoxPort.Text="6788";
    this.listBoxState.Items.Clear();
    this.richTextBoxReceive.Text=""; //
    // TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
    //
    } /// <summary>
    /// 清除任何使用中的資源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form 設計工具產生的程式碼
    /// <summary>
    /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
    /// 這個方法的內容。
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.textBoxName = new System.Windows.Forms.TextBox();
    this.label2 = new System.Windows.Forms.Label();
    this.textBoxPort = new System.Windows.Forms.TextBox();
    this.listBoxState = new System.Windows.Forms.ListBox();
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.groupBox2 = new System.Windows.Forms.GroupBox();
    this.listBox2 = new System.Windows.Forms.ListBox();
    this.groupBox3 = new System.Windows.Forms.GroupBox();
    this.richTextBoxReceive = new System.Windows.Forms.RichTextBox();
    this.buttonStart = new System.Windows.Forms.Button();
    this.buttonStop = new System.Windows.Forms.Button();
    this.groupBox1.SuspendLayout();
    this.groupBox2.SuspendLayout();
    this.groupBox3.SuspendLayout();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(16, 32);
    this.label1.Name = "label1";
    this.label1.TabIndex = 0;
    this.label1.Text = "服務器名稱:";
    // 
    // textBoxName
    // 
    this.textBoxName.Location = new System.Drawing.Point(80, 32);
    this.textBoxName.Name = "textBoxName";
    this.textBoxName.TabIndex = 1;
    this.textBoxName.Text = "";
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(16, 56);
    this.label2.Name = "label2";
    this.label2.TabIndex = 2;
    this.label2.Text = "監聽端口:";
    // 
    // textBoxPort
    // 
    this.textBoxPort.Location = new System.Drawing.Point(80, 56);
    this.textBoxPort.Name = "textBoxPort";
    this.textBoxPort.TabIndex = 3;
    this.textBoxPort.Text = "";
    // 
    // listBoxState
    // 
    this.listBoxState.Location = new System.Drawing.Point(16, 16);
    this.listBoxState.Name = "listBoxState";
    this.listBoxState.Size = new System.Drawing.Size(280, 95);
    this.listBoxState.TabIndex = 4;
    // 
    // groupBox1
    // 
    this.groupBox1.Controls.Add(this.groupBox2);
    this.groupBox1.Controls.Add(this.listBoxState);
    this.groupBox1.Location = new System.Drawing.Point(192, 16);
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.Size = new System.Drawing.Size(312, 120);
    this.groupBox1.TabIndex = 5;
    this.groupBox1.TabStop = false;
    this.groupBox1.Text = "服務器狀態";
    // 
    // groupBox2
    // 
    this.groupBox2.Controls.Add(this.listBox2);
    this.groupBox2.Location = new System.Drawing.Point(8, 184);
    this.groupBox2.Name = "groupBox2";
    this.groupBox2.Size = new System.Drawing.Size(312, 136);
    this.groupBox2.TabIndex = 6;
    this.groupBox2.TabStop = false;
    this.groupBox2.Text = "服務器狀態";
    // 
    // listBox2
    // 
    this.listBox2.Location = new System.Drawing.Point(16, 16);
    this.listBox2.Name = "listBox2";
    this.listBox2.Size = new System.Drawing.Size(280, 108);
    this.listBox2.TabIndex = 4;
    // 
    // groupBox3
    // 
    this.groupBox3.Controls.Add(this.richTextBoxReceive);
    this.groupBox3.Location = new System.Drawing.Point(24, 144);
    this.groupBox3.Name = "groupBox3";
    this.groupBox3.Size = new System.Drawing.Size(456, 128);
    this.groupBox3.TabIndex = 6;
    this.groupBox3.TabStop = false;
    this.groupBox3.Text = "接收信息";
    // 
    // richTextBoxReceive
    // 
    this.richTextBoxReceive.Location = new System.Drawing.Point(24, 24);
    this.richTextBoxReceive.Name = "richTextBoxReceive";
    this.richTextBoxReceive.Size = new System.Drawing.Size(400, 96);
    this.richTextBoxReceive.TabIndex = 0;
    this.richTextBoxReceive.Text = "";
    // 
    // buttonStart
    // 
    this.buttonStart.Location = new System.Drawing.Point(64, 304);
    this.buttonStart.Name = "buttonStart";
    this.buttonStart.TabIndex = 7;
    this.buttonStart.Text = "開始監聽";
    this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click);
    // 
    // buttonStop
    // 
    this.buttonStop.Location = new System.Drawing.Point(304, 304);
    this.buttonStop.Name = "buttonStop";
    this.buttonStop.TabIndex = 8;
    this.buttonStop.Text = "停止監聽";
    this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(512, 342);
    this.Controls.Add(this.buttonStop);
    this.Controls.Add(this.buttonStart);
    this.Controls.Add(this.groupBox3);
    this.Controls.Add(this.groupBox1);
    this.Controls.Add(this.textBoxPort);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.textBoxName);
    this.Controls.Add(this.label1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.groupBox1.ResumeLayout(false);
    this.groupBox2.ResumeLayout(false);
    this.groupBox3.ResumeLayout(false);
    this.ResumeLayout(false); }
    #endregion
      

  2.   

    /// <summary>
    /// 應用程式的主進入點。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {

    } private void buttonStart_Click(object sender, System.EventArgs e)
    {
    IPAddress myIP;
    IPEndPoint iep;
    try
    {
    IPHostEntry myHost=new IPHostEntry();
    myHost=Dns.GetHostByName(this.textBoxName.Text);
    myIP=IPAddress.Parse(myHost.AddressList[0].ToString());
    iep=new IPEndPoint(myIP,Int32.Parse(this.textBoxPort.Text));
    }
    catch
    {
    MessageBox.Show("輸入的服務器名或端口號不正確,請重新輸入!");
    return;
    }
    this.listBoxState.Items.Add("開始監聽.....");
    this.buttonStart.Enabled=false;
    serverSocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
    serverSocket.Bind(iep);
    serverSocket.Listen(10);
    serverSocket.BeginAccept(new AsyncCallback(AcceptConnection),serverSocket);
    }
    private void AcceptConnection(IAsyncResult ar)
    {
    Socket oldServer=(Socket)ar.AsyncState;
    clientSocket=oldServer.EndAccept(ar);
    this.listBoxState.Items.Add("與客戶"+clientSocket.RemoteEndPoint.ToString()+"建立連接.");
    byte[] message=System.Text.Encoding.Unicode.GetBytes("客戶,你好!");
    clientSocket.BeginSend(message,0,message.Length,SocketFlags.None,new AsyncCallback(SendData),clientSocket);
    }
    private void SendData(IAsyncResult ar)
    {
    Socket client=(Socket)ar.AsyncState;
    try
    {
    clientSocket.EndSend(ar);
    client.BeginReceive(data,0,dataSize,SocketFlags.None,new AsyncCallback(ReceiveData),client);
    }
    catch
    {
    client.Close();
    this.listBoxState.Items.Add("客戶已關閉連接,等待新客戶......");
    serverSocket.BeginAccept(new AsyncCallback(AcceptConnection),serverSocket);
    }
    }
    private void ReceiveData(IAsyncResult ar)
    {
    Socket client=(Socket)ar.AsyncState;
    try
    { int receiveDataLength=client.EndReceive(ar);
    string str=System.Text.Encoding.Unicode.GetString(data,0,receiveDataLength);
    this.richTextBoxReceive.Text=str;
    byte[] message=System.Text.Encoding.Unicode.GetBytes("服務器收到信息:"+str);
    clientSocket.BeginSend(message,0,message.Length,SocketFlags.None,new AsyncCallback(SendData),clientSocket);
    }
    catch
    {
    client.Close();
    this.listBoxState.Items.Add("客戶已關閉連接,等待新客戶......");
    serverSocket.BeginAccept(new AsyncCallback(AcceptConnection),serverSocket);
    }
    } private void buttonStop_Click(object sender, System.EventArgs e)
    { try
    {
    serverSocket.Close();
    this.listBoxState.Items.Add("停止監聽!");
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message,"錯誤");
    }
    }// private void buttonSend_Click(object sender, System.EventArgs e)
    // {
    // string str=this.richTextBoxSend.Rtf;
    // int i=str.Length;
    // if(i==0)
    // {
    // return ;
    //
    // }
    // else
    // {
    // i*=2;
    // }
    // byte[] datasize=new byte[4];
    // datasize=System.BitConverter.GetBytes(i);
    // byte [] sendbytes=System.Text.Encoding.Unicode.GetBytes(str);
    // try
    // {
    // NetworkStream netStream=new NetworkStream(clientSocket);
    // netStream.Write(datasize,0,4);
    // netStream.Write(sendbytes,0,sendbytes.Length);
    // netStream.Flush();
    // this.richTextBoxSend.Rtf="";
    // }
    // catch
    // {
    // MessageBox.Show("無法發送!");
    // }
    // }
    }
    }
      

  3.   

    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.Threading;
    using System.Text;
    namespace uci
    {
    /// <summary>
    /// Form1 的摘要描述。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.TextBox textBoxName;
    private System.Windows.Forms.TextBox textBoxPort;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.ListBox listBoxState;
    private System.Windows.Forms.RichTextBox richTextBoxReceive;
    private System.Windows.Forms.RichTextBox richTextBoxSend;
    private System.Windows.Forms.Button buttonRequest;
    private System.Windows.Forms.Button buttonSend;
    private System.Windows.Forms.Button buttonStop; private Socket clientSocket;
    private const int dataSize=1024;
    private byte[] data=new byte[dataSize]; /// <summary>
    /// 設計工具所需的變數。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows Form 設計工具支援的必要項
    //
    InitializeComponent();
    this.textBoxName.Text=Dns.GetHostName();
    this.textBoxPort.Text="6788";
    this.listBoxState.Items.Clear();
    this.richTextBoxReceive.Text="";
    this.richTextBoxSend.Text="";
    //
    // TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
    //
    } /// <summary>
    /// 清除任何使用中的資源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form 設計工具產生的程式碼
    /// <summary>
    /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
    /// 這個方法的內容。
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.label3 = new System.Windows.Forms.Label();
    this.label4 = new System.Windows.Forms.Label();
    this.textBoxName = new System.Windows.Forms.TextBox();
    this.textBoxPort = new System.Windows.Forms.TextBox();
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.listBoxState = new System.Windows.Forms.ListBox();
    this.richTextBoxReceive = new System.Windows.Forms.RichTextBox();
    this.richTextBoxSend = new System.Windows.Forms.RichTextBox();
    this.buttonRequest = new System.Windows.Forms.Button();
    this.buttonSend = new System.Windows.Forms.Button();
    this.buttonStop = new System.Windows.Forms.Button();
    this.groupBox1.SuspendLayout();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(8, 24);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(72, 23);
    this.label1.TabIndex = 0;
    this.label1.Text = "服務器名稱:";
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(8, 56);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(80, 23);
    this.label2.TabIndex = 1;
    this.label2.Text = "請求端口";
    // 
    // label3
    // 
    this.label3.Location = new System.Drawing.Point(8, 176);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(72, 23);
    this.label3.TabIndex = 2;
    this.label3.Text = "接收信息";
    // 
    // label4
    // 
    this.label4.Location = new System.Drawing.Point(8, 280);
    this.label4.Name = "label4";
    this.label4.Size = new System.Drawing.Size(64, 23);
    this.label4.TabIndex = 3;
    this.label4.Text = "發送信息";
    // 
    // textBoxName
    // 
    this.textBoxName.Location = new System.Drawing.Point(96, 24);
    this.textBoxName.Name = "textBoxName";
    this.textBoxName.TabIndex = 4;
    this.textBoxName.Text = "";
    // 
    // textBoxPort
    // 
    this.textBoxPort.Location = new System.Drawing.Point(96, 56);
    this.textBoxPort.Name = "textBoxPort";
    this.textBoxPort.TabIndex = 5;
    this.textBoxPort.Text = "";
    // 
    // groupBox1
    // 
    this.groupBox1.Controls.Add(this.listBoxState);
    this.groupBox1.Location = new System.Drawing.Point(232, 24);
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.Size = new System.Drawing.Size(304, 120);
    this.groupBox1.TabIndex = 6;
    this.groupBox1.TabStop = false;
    this.groupBox1.Text = "程序狀態";
    // 
    // listBoxState
    // 
    this.listBoxState.Location = new System.Drawing.Point(16, 16);
    this.listBoxState.Name = "listBoxState";
    this.listBoxState.Size = new System.Drawing.Size(280, 95);
    this.listBoxState.TabIndex = 7;
    // 
    // richTextBoxReceive
    // 
    this.richTextBoxReceive.Location = new System.Drawing.Point(120, 168);
    this.richTextBoxReceive.Name = "richTextBoxReceive";
    this.richTextBoxReceive.Size = new System.Drawing.Size(416, 96);
    this.richTextBoxReceive.TabIndex = 7;
    this.richTextBoxReceive.Text = "";
    // 
    // richTextBoxSend
    // 
    this.richTextBoxSend.Location = new System.Drawing.Point(120, 280);
    this.richTextBoxSend.Name = "richTextBoxSend";
    this.richTextBoxSend.Size = new System.Drawing.Size(416, 96);
    this.richTextBoxSend.TabIndex = 8;
    this.richTextBoxSend.Text = "";
    // 
    // buttonRequest
    // 
    this.buttonRequest.Location = new System.Drawing.Point(136, 400);
    this.buttonRequest.Name = "buttonRequest";
    this.buttonRequest.TabIndex = 9;
    this.buttonRequest.Text = "請求連接";
    this.buttonRequest.Click += new System.EventHandler(this.buttonRequest_Click);
    // 
    // buttonSend
    // 
    this.buttonSend.Location = new System.Drawing.Point(288, 400);
    this.buttonSend.Name = "buttonSend";
    this.buttonSend.TabIndex = 10;
    this.buttonSend.Text = "發送消息";
    this.buttonSend.Click += new System.EventHandler(this.buttonSend_Click);
    // 
    // buttonStop
    // 
    this.buttonStop.Location = new System.Drawing.Point(448, 400);
    this.buttonStop.Name = "buttonStop";
    this.buttonStop.TabIndex = 11;
    this.buttonStop.Text = "關閉連接";
    this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(584, 438);
    this.Controls.Add(this.buttonStop);
    this.Controls.Add(this.buttonSend);
    this.Controls.Add(this.buttonRequest);
    this.Controls.Add(this.richTextBoxSend);
    this.Controls.Add(this.richTextBoxReceive);
    this.Controls.Add(this.groupBox1);
    this.Controls.Add(this.textBoxPort);
    this.Controls.Add(this.textBoxName);
    this.Controls.Add(this.label4);
    this.Controls.Add(this.label3);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.label1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.groupBox1.ResumeLayout(false);
    this.ResumeLayout(false); }
    #endregion
      

  4.   

    http://www.baidu.com/s?ie=gb2312&bs=Socket%B4%AB%CB%CD%CE%C4%BC%FE+%C0%FD%D7%D3+%B4%FA%C2%EB+C%23&sr=&z=&wd=Socket+%CE%C4%BC%FE+%C0%FD%D7%D3+%B4%FA%C2%EB+C%23&ct=0&cl=3&f=8
      

  5.   

    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {

    } private void buttonRequest_Click(object sender, System.EventArgs e)
    {
    IPAddress myIP;
    IPEndPoint iep;
    try
    {
    IPHostEntry myHost=new IPHostEntry();
    myHost=Dns.GetHostByName(this.textBoxName.Text);
    myIP=IPAddress.Parse(myHost.AddressList[0].ToString());
    iep=new IPEndPoint(myIP,Int32.Parse(this.textBoxPort.Text));
    }
    catch
    {
    MessageBox.Show("輸入的服務器名稱或端口號不正確,請重新輸入!");
    return;
    }
    Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
    socket.BeginConnect(iep,new AsyncCallback(ConnectServer),socket);
    }
    private void ConnectServer(IAsyncResult ar)
    {
    clientSocket=(Socket)ar.AsyncState;
    try
    { clientSocket.EndConnect(ar);
    this.listBoxState.Items.Add("與服務器"+clientSocket.RemoteEndPoint.ToString()+"連接成功!");
    clientSocket.BeginReceive(data,0,dataSize,SocketFlags.None,new AsyncCallback(ReceiveData),clientSocket);
    }
    catch
    {
    MessageBox.Show("與服務器連接失敗!");
    }
    }
    private void ReceiveData(IAsyncResult ar)
    {
    try
    {
    Socket server=(Socket)ar.AsyncState;
    int receiveDataLength=server.EndReceive(ar);
    string str=System.Text.Encoding.Unicode.GetString(data,0,receiveDataLength);
    this.richTextBoxReceive.Text=str;
    }
    catch
    {}
    } private void buttonSend_Click(object sender, System.EventArgs e)
    {
    try
    {
    byte[] message=System.Text.Encoding.Unicode.GetBytes(this.richTextBoxSend.Text);
    this.richTextBoxSend.Clear();
    clientSocket.BeginSend(message,0,message.Length,SocketFlags.None,new AsyncCallback(SendData),clientSocket); }
    catch
    {
    MessageBox.Show("尚未與服務器建立連接,發送失敗!");
    }
    }
    private void SendData(IAsyncResult ar)
    {
    Socket socket=(Socket)ar.AsyncState;
    int send=socket.EndSend(ar);
    socket.BeginReceive(data,0,dataSize,SocketFlags.None,new AsyncCallback(ReceiveData),socket); } private void buttonStop_Click(object sender, System.EventArgs e)
    {
    try
    {
    clientSocket.Close();
    this.listBoxState.Items.Add("與服務器斷開連接!"); }
    catch
    {
    MessageBox.Show("尚未開始,斷開無效!");
    }
    }


    }
    }