我做的C/S
目标是 客户端服务端聊天。但客户端发送第一次 。服务端收到了信息。然后返回信息。客户端也收到信息了。
客户端再发送服务端就收不到了。。
以下是服务端代码: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.Threading;
using System.Runtime.InteropServices;namespace WindowsApplication1
{
    public partial class Form1 : Form
    {        static Socket serverSocket;
        Socket client;
        Thread trd;
        delegate void d(String str);        public Form1()
        {
            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            int port = 5700;
            int maxConn = 3;
            IPEndPoint serverIP = new IPEndPoint(ip, port);
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                serverSocket.Bind(serverIP);
                serverSocket.Listen(maxConn);
            }
            catch (SocketException se)
            {
                richTextBox1.AppendText(port + "端口已被使用!\n" + se.StackTrace + "\n");
            }            richTextBox1.AppendText(ip + "正在" + port + "端口进行监听……\n");
            richTextBox1.AppendText("当前最大连接数限制:" + (maxConn == 0 ? "无限制" : "" + maxConn) + "\n");
            trd = new Thread(new ThreadStart(socketHandle));
            trd.Start();
            richTextBox1.AppendText("等待连接……\n");
            button2.Enabled = true;        }        private void 配置服务器ToolStripMenuItem_Click(object sender, EventArgs e)
        {        }        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            try
            {
                serverSocket.Close();
                richTextBox1.AppendText("服务器已经关闭!\n");
            }
            catch (SocketException se)
            {                richTextBox2.AppendText(se.StackTrace);
            }
            button1.Enabled = true;
        }
        //
        //处理客户端发来的请求
        private void socketHandle()
        {
            d eed = new d(dm);
            while (true)
            {
                try
                {
                    client = serverSocket.Accept();
                    Thread t = new Thread(new ThreadStart(handle));
                }
                catch (SocketException se)
                {
                    richTextBox2.Invoke(eed, "socketHandleException!!\n" + se.StackTrace);
                }
            }        }
        private void handle()
        {
            d eed = new d(dm);
            byte[] b = new byte[255];
            byte[] back = System.Text.Encoding.UTF8.GetBytes("收到!");
            client.Receive(b);
            richTextBox2.Invoke(eed, System.Text.Encoding.UTF8.GetString(b));
            client.Send(back);
        }        private void dm(String str)
        {
            richTextBox2.AppendText(str + "\n");
        }    }
}客户单的:
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.Threading;
namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        Thread thr;
        Socket client;
        delegate void d(String str);
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                button1.Enabled = false;
                IPAddress ip = IPAddress.Parse(textBox1.Text);
                int port = int.Parse(textBox2.Text);                IPEndPoint conn = new IPEndPoint(ip, port);
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                richTextBox2.AppendText("尝试连接" + ip.ToString() + "于" + port + "端口\n");
                client.Connect(conn);
                richTextBox2.AppendText("连接成功!!\n");
                button2.Enabled = true;
            }
            catch (Exception ee)
            {
                button1.Enabled = true;
                richTextBox2.AppendText("尝试连接失败!!以下是详细信息:\n" + ee.StackTrace + "\n请重新尝试连接\n");
            }
        }        private void button3_Click(object sender, EventArgs e)
        {
            thr = new Thread(new ThreadStart(send2server));
            thr.Start();
        }
        private void send2server()
        {
            d rich = new d(rich1app);
            byte[] sendByte = new byte[255];
            byte[] receiveByte = new byte[255];
            sendByte = System.Text.Encoding.UTF8.GetBytes(textBox3.Text);
            richTextBox1.Invoke(rich, textBox3.Text + "\n");
            client.Send(sendByte);
            client.Receive(receiveByte);
            richTextBox1.Invoke(rich, System.Text.Encoding.UTF8.GetString(receiveByte) + "\n");        }        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            client.Shutdown(SocketShutdown.Both);
            client.Close();
            richTextBox2.AppendText("连接已经断开!\n");
            button1.Enabled = true;        }
        private void rich1app(String str)
        {
            richTextBox1.AppendText(str);
        }    }
}

解决方案 »

  1.   

    我是发一次关一次的 然后要再发的时候在打开 所以你在send2server()方法最后加个client.close()试试
    private void send2server()
            {
                d rich = new d(rich1app);
                byte[] sendByte = new byte[255];
                byte[] receiveByte = new byte[255];
                sendByte = System.Text.Encoding.UTF8.GetBytes(textBox3.Text);
                richTextBox1.Invoke(rich, textBox3.Text + "\n");
                client.Send(sendByte);
                client.Receive(receiveByte);
                richTextBox1.Invoke(rich, System.Text.Encoding.UTF8.GetString(receiveByte) + "\n");
                client.Close();
            }
      

  2.   

    http://www.codeproject.com/KB/IP/AsyncSocketServerandClien.aspx
      

  3.   

    服务端的问题..只是连接才创建线程... client = serverSocket.Accept();
                        Thread t = new Thread(new ThreadStart(handle));在你handle()你发送后数据你不对SOCKET进行处理 直接方法结束了..如果客户不进行连接请求..只发数据..你当然收不到.
      

  4.   


    客户端可以保持连接,等待下一次Send。每次发送都重新配置连接,太浪费了。