谢谢!

解决方案 »

  1.   

    http://blog.csdn.net/chendazhi/archive/2006/04/21/671627.aspx
      

  2.   

    网上找的
    /************SocketClient****VS C#2003********************/
    using System ;
    using System.Drawing ;
    using System.Collections ;
    using System.ComponentModel ;
    using System.Windows.Forms ;
    using System.Data ;
    using System.Net.Sockets ;
    //使用到TcpListen类
    using System.Net ;
    namespace WindowsApplication3
    {
    /// 
    /// Form1 的摘要说明。
    /// 
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.StatusBar statusBar1;
    private System.Windows.Forms.ListBox listBox1;
    /// 
    /// 必需的设计器变量。
    int port = 8000 ;
    //定义侦听端口号
    private TcpClient tcpc  ;
    //对服务器端创建TCP连接
    private Socket stSend ; 
    //创建发送数据套接字
    private bool tcpConnect = false ;
    //定义标识符,用以表示TCP连接是否建立
    /// 
    private System.ComponentModel.Container components = null;
           
    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }
      /// 
    /// 清理所有正在使用的资源。
    /// 
    protected override void Dispose( bool disposing )
    {
    int iLength = textBox2.Text.Length ;
    //获取要发送的数据的长度
    Byte [ ] bySend = new byte [ iLength ] ;
    //根据获取的长度定义一个Byte类型数组
    bySend = System.Text.Encoding.Default.GetBytes 
    ( textBox2.Text  ) ;
    //按照指定编码类型把字符串指定到指定的Byte数组
    int i = stSend.Send  ( bySend ) ;
    //发送数据
    listBox1.Items.Add  ( textBox2.Text  ) ; if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// 
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// 
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.label2 = new System.Windows.Forms.Label();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.statusBar1 = new System.Windows.Forms.StatusBar();
    this.label3 = new System.Windows.Forms.Label();
    this.button2 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(32, 26);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(99, 38);
    this.label1.TabIndex = 0;
    this.label1.Text = "IP地址:";
    // 
    // textBox1
    // 
    this.textBox1.BorderStyle =  System.Windows.Forms.BorderStyle.FixedSingle;
    this.textBox1.Location = new System.Drawing.Point(125, 23);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(222, 25);
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "";
    // 
    // button1
    // 
    this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    this.button1.Location = new System.Drawing.Point(373, 18);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(83, 36);
    this.button1.TabIndex = 2;
    this.button1.Text = "连接";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(21, 82);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(134, 30);
    this.label2.TabIndex = 3;
    this.label2.Text = "发送信息:";
    // 
    // textBox2
    // 
    this.textBox2.BorderStyle =  System.Windows.Forms.BorderStyle.FixedSingle;
    this.textBox2.Location = new System.Drawing.Point(125, 75);
    this.textBox2.Name = "textBox2";
    this.textBox2.Size = new System.Drawing.Size(222, 25);
    this.textBox2.TabIndex = 4;
    this.textBox2.Text = "";
    // 
    // listBox1
    // 
    this.listBox1.ItemHeight = 15;
    this.listBox1.Location = new System.Drawing.Point(27, 152);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(448, 184);
    this.listBox1.TabIndex = 6;
    // 
    // statusBar1
    // 
    this.statusBar1.Location = new System.Drawing.Point(0, 378);
    this.statusBar1.Name = "statusBar1";
    this.statusBar1.Size = new System.Drawing.Size(493, 29);
    this.statusBar1.TabIndex = 7;
    this.statusBar1.Text = "无连接";
    // 
    // label3
    // 
    this.label3.Location = new System.Drawing.Point(19, 121);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(170, 29);
    this.label3.TabIndex = 8;
    this.label3.Text = "已经发送的信息:";
    // 
    // button2
    // 
    this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    this.button2.Location = new System.Drawing.Point(373, 69);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(83, 36);
    this.button2.TabIndex = 9;
    this.button2.Text = "发送";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
    this.ClientSize = new System.Drawing.Size(493, 407);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.statusBar1);
    this.Controls.Add(this.listBox1);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.label3);
    this.FormBorderStyle =  System.Windows.Forms.FormBorderStyle.FixedSingle;
    this.MaximizeBox = false;
    this.Name = "Form1";
    this.Text = "利用Socket来发送数据";
    this.ResumeLayout(false); }
    #endregion /// 
    /// 应用程序的主入口点。
    /// 
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    //以下代码是判断是否和远程终结点成功连接
    try
    {
    stSend = new Socket ( AddressFamily.InterNetwork , 
    SocketType.Stream , ProtocolType.Tcp ) ;
    //初始化一个Socket实例
    IPEndPoint tempRemoteIP = new IPEndPoint 
    ( IPAddress.Parse ( textBox1.Text ) , port ) ;
    //根据IP地址和端口号创建远程终结点
    EndPoint epTemp =  ( EndPoint ) tempRemoteIP ;
    stSend.Connect ( epTemp ) ;
    //连接远程主机的8000端口号
    statusBar1.Text = "成功连接远程计算机!" ;
    tcpConnect = true ;
    button1.Enabled = false  ;
    button2.Enabled = true  ;
    }
    catch ( Exception )
    {
    statusBar1.Text = "目标计算机拒绝连接请求!" ;
    }  } private void button2_Click(object sender, System.EventArgs e)
    {
    int iLength = textBox2.Text.Length ;
    //获取要发送的数据的长度
    Byte [ ] bySend = new byte [ iLength ] ;
    //根据获取的长度定义一个Byte类型数组
    bySend = System.Text.Encoding.Default.GetBytes 
    ( textBox2.Text  ) ;
    //按照指定编码类型把字符串指定到指定的Byte数组
    int i = stSend.Send  ( bySend ) ;
    //发送数据
    listBox1.Items.Add  ( textBox2.Text  ) ; }
    }
    }
      

  3.   

    这个是服务器端
    //***************SocketServer**VS C#2003*****************//using System ;
    using System.Drawing ;
    using System.Collections ;
    using System.ComponentModel ;
    using System.Windows.Forms ;
    using System.Data ;
    using System.Net.Sockets  ;
    //使用到TcpListen类
    using System.Net ;
    using System.Threading  ;
    //使用到线程namespace SocketlistenWA3
    {
    /// 
    /// Form1 的摘要说明。
    /// 
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.StatusBar statusBar1;
    /// 
    /// 必需的设计器变量。
    int port = 8000 ;
    //定义侦听端口号
    private Thread thThreadRead ;
    //创建线程,用以侦听端口号,接收信息
    private TcpListener tlTcpListen ;
    //侦听端口号
    private bool blistener = true ;
    //设定标示位,判断侦听状态
    private Socket stRead ;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;
      /// 
    private System.ComponentModel.Container components = null;
            
    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// 
    /// 清理所有正在使用的资源。
    /// 
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// 
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// 
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.statusBar1 = new System.Windows.Forms.StatusBar();
    this.label1 = new System.Windows.Forms.Label();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    this.button1.Location = new System.Drawing.Point(120, 56);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(107, 43);
    this.button1.TabIndex = 0;
    this.button1.Text = "监听";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // listBox1
    // 
    this.listBox1.ItemHeight = 15;
    this.listBox1.Location = new System.Drawing.Point(21, 128);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(344, 139);
    this.listBox1.TabIndex = 1;
    // 
    // statusBar1
    // 
    this.statusBar1.Location = new System.Drawing.Point(0, 322);
    this.statusBar1.Name = "statusBar1";
    this.statusBar1.Size = new System.Drawing.Size(389, 28);
    this.statusBar1.TabIndex = 2;
    this.statusBar1.Text = "无连接";
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(56, 16);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(96, 24);
    this.label1.TabIndex = 3;
    this.label1.Text = "地址:";
    this.label1.Click += new System.EventHandler(this.label1_Click);
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(176, 16);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(144, 25);
    this.textBox1.TabIndex = 4;
    this.textBox1.Text = "127.0.0.1";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
    this.ClientSize = new System.Drawing.Size(389, 350);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.statusBar1);
    this.Controls.Add(this.listBox1);
    this.Controls.Add(this.button1);
    this.FormBorderStyle =  System.Windows.Forms.FormBorderStyle.FixedSingle;
    this.MaximizeBox = false;
    this.Name = "Form1";
    this.Text = "利用Socket来接收数据";
    this.ResumeLayout(false); }
    #endregion /// 
    /// 应用程序的主入口点。
     
    /// 
    [STAThread]
    static void Main()  { 
    Application.Run(new Form1());
    }  
       
    private void Listen()
    {
    try
    {
    IPAddress localAddr = IPAddress.Parse (textBox1.Text.Trim());
    tlTcpListen = new TcpListener( localAddr, port )  ;
    //以8000端口号来初始化TcpListener实例
    tlTcpListen.Start ( )  ;
    //开始监听网络的连接请求
    statusBar1.Text = "正在监听..."  ;
    stRead =  tlTcpListen.AcceptSocket ( ) ;

    //通过连接请求,并获得接收数据时使用的Socket实例
    EndPoint tempRemoteEP =  stRead.RemoteEndPoint  ;
    IPEndPoint tempRemoteIP =  ( IPEndPoint ) tempRemoteEP ;
    //获取请求的远程计算机名称
    IPHostEntry host = Dns.GetHostByAddress
    ( tempRemoteIP.Address ) ;
    string sHostName = host.HostName ;
    statusBar1.Text = "已经连接!" ;
    //循环侦听
    while (   blistener  )
    {
    string sTime = DateTime.Now.ToShortTimeString  ( )  ;//获取接收数据时的时间
    Byte [ ] byRead =new Byte [ 80 ] ;
    int iRead =  stRead.ReceiveFrom 
    ( byRead , ref tempRemoteEP ) ;
    //获得接收的字节数目
    Byte [ ] byText = new Byte [ iRead ] ;
    //并根据接收到的字节数目来定义字节数组
    Array.Copy  ( byRead , 0 , byText , 0 , iRead ) ;
    string sTemp = System.Text.Encoding.Default.
    GetString ( byText ) ;
    //判断是否为断开连接控制码
    if  (  sTemp.Trim ( )  == "STOP"  )
    {
    stRead.Close ( ) ;
    tlTcpListen.Stop  (  )  ;
    //关闭侦听
    statusBar1.Text = "连接已经关闭!"  ;
    thThreadRead.Abort  (   )  ;
    //中止线程
    return ;
    }
    else
    listBox1.Items.Add  (  sTime + " " + sTemp   )  ;
      

    }
    catch  (  System.Security.SecurityException  ) 
    {
    MessageBox.Show  (  "侦听失败!"  , "错误"  )  ;
    }
    }
     
    private void listBox1_SelectedIndexChanged(object sender, System.EventArgs  e)
    {
      
    } private void button1_Click(object sender, System.EventArgs e)
    {
    thThreadRead = new Thread ( new ThreadStart( Listen ) ) ;
    //以Listen过程来初始化Thread实例
    thThreadRead.Start (   ) ;
    //启动线程
    button1.Enabled = false  ; } private void label1_Click(object sender, System.EventArgs e)
    {
      
    }
    }
    }
      

  4.   

    我只想知道
    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    IPAddress myIP = IPAddress.Parse("127.0.0.1"); 
    IPEndPoint ipe = new IPEndPoint(myIP,5678);
    下步改做什么了
    怎么才能开始侦听和接受数据