可以: http://www.csharphelp.com/archives/archive133.html

解决方案 »

  1.   

    在你的机器的.net安装目录下的Sample下有个小例子,找下
      

  2.   

    //客户端:
    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;namespace client
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label ts;
    private System.Windows.Forms.TextBox fs;
    private System.Windows.Forms.Button button1;
    private static IPAddress ip=IPAddress.Parse("127.0.0.1");
    private IPEndPoint po=new IPEndPoint(ip,8000);
    private Socket socket; /// <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.ts = new System.Windows.Forms.Label();
    this.fs = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // ts
    // 
    this.ts.Location = new System.Drawing.Point(48, 40);
    this.ts.Name = "ts";
    this.ts.TabIndex = 0;
    this.ts.Text = "label1";
    // 
    // fs
    // 
    this.fs.Location = new System.Drawing.Point(56, 120);
    this.fs.Multiline = true;
    this.fs.Name = "fs";
    this.fs.Size = new System.Drawing.Size(152, 96);
    this.fs.TabIndex = 1;
    this.fs.Text = "";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(216, 136);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(64, 24);
    this.button1.TabIndex = 2;
    this.button1.Text = "发送";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.fs);
    this.Controls.Add(this.ts);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
    Thread t=new Thread(new ThreadStart(fsff));
    t.Start();
    }
    private void fsff()
    {
    socket.Connect(po);
    if (socket.Connected)
    {
    ts.Text = "联接成功";
    string fss="我是客户端";

     Byte[] fsbyte= System.Text.Encoding.Unicode.GetBytes(fss);
    socket.Send(fsbyte,fsbyte.Length,0);
    int a=1;
    } }
    }
    }
      

  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.Diagnostics;
    namespace server
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label ts;
    private static IPAddress ip=IPAddress.Parse("127.0.0.1");
    private IPEndPoint po=new IPEndPoint(ip,8000);
    private Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
    private Socket tempsocket;
    private System.Windows.Forms.TextBox jsxx;
    /// <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.ts = new System.Windows.Forms.Label();
    this.jsxx = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // ts
    // 
    this.ts.Location = new System.Drawing.Point(80, 32);
    this.ts.Name = "ts";
    this.ts.Size = new System.Drawing.Size(136, 24);
    this.ts.TabIndex = 0;
    // 
    // jsxx
    // 
    this.jsxx.Location = new System.Drawing.Point(56, 112);
    this.jsxx.Multiline = true;
    this.jsxx.Name = "jsxx";
    this.jsxx.Size = new System.Drawing.Size(224, 144);
    this.jsxx.TabIndex = 1;
    this.jsxx.Text = "jsxx";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(416, 349);
    this.Controls.Add(this.jsxx);
    this.Controls.Add(this.ts);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Click += new System.EventHandler(this.Form1_Click);
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Process[] p=Process.GetProcessesByName("server");
    if (p!=null && p.Length>1)
    for (int i=1;i<p.Length;i++)
    p[i].Kill();
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {

    }
    private void js()
    {
    while(true)  
    {
    tempsocket=socket.Accept();
    if (tempsocket.Connected)
    {
    ts.Text="连接成功....";

    Byte[] b=new byte[100];
    //这个缓冲区长了没事,如果比发送端缓冲区短就会把信息截去
    //排个例子中发送方缓冲区是10
    tempsocket.Receive(b,b.Length,0);
    string str=System.Text.Encoding.Unicode.GetString(b);
    jsxx.AppendText(str);
    }
    }
    } private void Form1_Click(object sender, System.EventArgs e)
    {
    socket.Bind(po);
    socket.Listen(50);
    ts.Text="开始监听...";

    Thread t=new Thread(new ThreadStart(js));
    t.Start();
    }
    }
    }
      

  4.   

    还可以使用虚拟机器 VMware Workstation
      

  5.   

    可以!使用IPAddress IP=IPAddress.Parse("127.0.0.1")
    localhost的IP