日期:2014-05-18  浏览次数:20805 次

C# socket 异步收发问题
我想实现一个简单的C/S版的聊天室,客户端向服务器发送一个信息,服务器接收到后返回一个同样的消息,
我下面的代码只能向服务器发送信息,但是收不到服务器返回的消息。请高手帮我看看,指点一下。
///ServerForm
C# code

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;

namespace ChatServer
{
    public partial class Form1 : Form
    {
        Socket s;
        byte[] msg = new byte[1024];
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int port = 1000;
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            IPEndPoint ipe = new IPEndPoint(ip, port);
            s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Bind(ipe);
            s.Listen(5);
            s.BeginAccept(new AsyncCallback(OnAccept), null);
        }

        private void OnAccept(IAsyncResult ar)
        {
            Socket cs = s.EndAccept(ar);
            s.BeginAccept(new AsyncCallback(OnAccept), null);
            msg = new byte[1024];
            cs.BeginReceive(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(OnReceive), cs);
        }

        private void OnReceive(IAsyncResult ar)
        {
            Socket cs = (Socket)ar.AsyncState;
            cs.EndReceive(ar);
            cs.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(OnSend), cs);
            string strTemp = Encoding.GetEncoding("gb2312").GetString(msg) + "\r\n";
            cs.BeginReceive(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(OnReceive), cs);
            this.richTextBox1.Text += strTemp;
        }

        private void OnSend(IAsyncResult ar)
        {
            ((Socket)ar.AsyncState).EndSend(ar);
        }
    }
}



///clientForm
C# code

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;

namespace ChatClient
{
    public partial class Form1 : Form
    {
        Socket cs;
        byte[] msg;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            msg = new byte[1024];
            msg = Encoding.GetEncoding("gb2312").GetBytes(textBox1.Text);
            cs.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
        }

        private void OnConnect(IAsyncResult ar)
        {
            cs.EndConnect(ar);
            byte[] strSend = new byte[1024];
            string strTemp = "请求连接!";
            strSend = System.Text.Encoding.GetEncoding("gb2312").GetBytes(strTemp);
            cs.BeginSend(strSend, 0, strSend.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
        }

        private void OnSend(IAsyncResult ar)
        {
            cs.EndSend(ar);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cs = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1000);
            cs.BeginConnect(ipe, new AsyncCallback(OnConnect), null);
            msg = new byte[1024];
            cs.BeginReceive(msg, 0, msg.Length,  SocketFlags.None,new AsyncCallback(OnReceive), null);
        }

        private void OnReceive(IAsyncResult ar)
        {
            cs.EndReceive(ar);
            msg = new b