下面这个是server端:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
// 定义新的命名空间。
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace TalkServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // 定义私有变量。
        public bool m_bConnected = false;
        public IPEndPoint p = null;
        public Socket s = null;
        public NetworkStream ns = null;
        public TextReader r = null;
        public TextWriter w = null;
        public Thread t = null;
        public void ThreadListen()
        {
            Socket recv = null;
            recv = s.Accept();
            if (recv != null)
                m_bConnected = true;
            ns = new NetworkStream(recv);
            r = new StreamReader(ns);
            w = new StreamWriter(ns);
            while (m_bConnected)
            {
                string tmp;
                try
                {
                    tmp = r.ReadLine();
                    if (tmp.Length != 0)
                    {
                        lock (this)
                        {
                            richTextBox1.Text = "他说:" + tmp + "\n" + richTextBox1.Text;
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("连接断开!!");
                }
            }
            recv.Shutdown(SocketShutdown.Both);
            recv.Close();
            s.Shutdown(SocketShutdown.Both);
            s.Close();
        }        private void button1_Click(object sender, EventArgs e)
        {
            p = new IPEndPoint(IPAddress.Any, 13002);
            s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Blocking = true;
            s.Bind(p);
            s.Listen(0);
            t = new Thread(new ThreadStart(this.ThreadListen));
            t.Start();
            button1.Enabled = false;
        }        private void richTextBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            byte[] data;
            if (e.KeyChar == (char)13)
            {
                if (m_bConnected)
                {
                    try
                    {
                        lock (this)
                        {
                            richTextBox1.Text = "我说:" + richTextBox2.Text + richTextBox1.Text;
                            w.WriteLine(richTextBox2.Text);
                            w.Flush();
                        }
                    }
                    catch
                    {
                        MessageBox.Show("连接断开!!");
                    }
                }
                else
                {
                    MessageBox.Show("对不起,还没有与对方连接,不能通信!");
                }
                richTextBox2.Text = "";
                richTextBox2.Focus();
            }
        }
    }
}
另一个是client端using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
// 添加新的命名空间。
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace TalkClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // 定义私有变量。
        public bool m_bConnected = false;
        public Thread t = null;
        public IPEndPoint p = null;
        public Socket s = null;
        public NetworkStream ns = null;
        public TextReader r = null;
        public TextWriter w = null;
        public void ThreadListen()
        {
            string tmp;
            while (m_bConnected)
            {
                try
                {
                    tmp = r.ReadLine();
                    if (tmp.Length != 0)
                    {
                        lock (this)
                        {
                            richTextBox1.Text = "他说:" + tmp + "\n" + richTextBox1.Text;
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("连接断开!!");
                }
            }
            s.Shutdown(SocketShutdown.Both);
            s.Close();
        }        private void button1_Click(object sender, EventArgs e)
        {
          
            p = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
            s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Connect(p);
            if (s.Connected)
            {
                ns = new NetworkStream(s);
                r = new StreamReader(ns);
                w = new StreamWriter(ns);
                t = new Thread(new ThreadStart(this.ThreadListen));
                t.Start();
                m_bConnected = true;
                button1.Enabled = false;
            }
        }        private void richTextBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (m_bConnected)
                {
                    lock (this)
                    {
                        try
                        {
                            richTextBox1.Text = "我说:" + richTextBox2.Text + richTextBox1.Text;
                            w.WriteLine(richTextBox2.Text);
                            w.Flush();
                        }
                        catch
                        {
                            MessageBox.Show("连接断开!!");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("对不起,还没有与对方连接,不能通信!");
                }
                richTextBox2.Text = "";
                richTextBox2.Focus();
            }
        }
    }
}
这里的服务器地址和端口号都是由两个textbox获取,另有一个button按钮,用于连接服务器,但按下后出现了
“由于目标计算机积极拒绝,无法连接。 127.0.0.1:8888”de提示,我是初学的,请教各位了。