应该也是用Receive方法吧,谁能告诉我大概怎么写?非常感谢!

解决方案 »

  1.   

    如果直接用SOCKET呢?能大概写一下吗?
      

  2.   

    byte[] byte_Re=new Byte[4096];while(true)
    {
        tempSocket.Receive(byte_Re,0,byte_Re.Length,SocketFlags.None);
        this.txtState.AppendText(Encoding.GetEncoding("gb2312").GetString(byte_Re)+"\n");
    }应该是类似这样吧
      

  3.   

    CSDN文档里就有 用C#开发CMPP2.0协议,自己搜一下就有了。
      

  4.   

    theSocket.BeginReceive(buffer, 0, bufferSize, SocketFlags.None, new AsyncCallback(OnReceive), null);然后定义回调函数OnReceive:
    private void OnReceive(IAsyncResult ar)
    {
      ... // 接受和处理数据
      theSocket.BeginReceive(buffer, 0, bufferSize, SocketFlags.None, new AsyncCallback(OnReceive), null);   // 继续接收
    }
      

  5.   

    /// <summary>
    /// 侦听处理线程
    /// </summary>
    private void accept()
    {
    while(!round)
    {
    try
    {
    m_accSocket = m_Socket.Accept();
    if(m_accSocket.Connected)
    {
    Thread thread=new Thread(new ThreadStart(receive));
    thread.Start();
    m_AcceptCallBack(1);
    }
    }
    catch
    {
    }
    }
      

  6.   

    possible_Y,我的程序是同步的。xlq1314(xlq),是长连接吗?m_Socket.Accept(),这个方法如果不先设为侦听状态,直接用会报错,如果先调Listen()方法,异常:“在一个已经连接的套接字上做了一个连接请求。”还有Thread(new ThreadStart(receive)),你这里的receive是做了什么工作?
    谢谢两位!
      

  7.   

    /// <summary>
    /// Tcp通信服务器类
    /// </summary>
    public class JhTcpServer
    {
    public JhTcpServer(int port):this("",port)
    {
    } public JhTcpServer(string ip,int port)
    {
    IPAddress ipAddress;
    if (ip.ToUpper().Trim() == "")
    {
    ipAddress = Dns.GetHostByName(Dns.GetHostName()).AddressList[0];
    }
    else
    {
    ipAddress = IPAddress.Parse(ip);
    }
    server = new TcpListener(ipAddress,port);
    adm = new CommDataMgr();
    status = new Parm(this);
    }
    ~JhTcpServer()
    {
    Stop();
    }
    /// <summary>
    /// 连接成功事件
    /// </summary>
    public event CommEventHandler OnClientConnected; /// <summary>
    /// 连接断开事件
    /// </summary>
    public event CommEventHandler OnClientDisconnected; /// <summary>
    /// Tcp监听对象
    /// </summary>
    TcpListener server = null; /// <summary>
    /// 客户端序号
    /// </summary>
    int clientindex = 0; /// <summary>
    /// 开始监听
    /// </summary>
    public void Start()
    {
    clients.Clear();
    clientindex = 99999999;
    adm.Start();
    server.Start();
    status.Status = JhThreadStatus.Running;
    listenThread = new Thread(new ThreadStart(this.ListenThread));
    listenThread.Name = "JH tcp listenor";
    listenThread.Start();
    Console.WriteLine("JH tcp listenor started.");
    }
    /// <summary>
    /// 监听线程
    /// </summary>
    Thread listenThread = null; /// <summary>
    /// 停止接受连接
    /// </summary>
    public void Stop()
    {
    try
    {
    int i;
    for (i = 0; i < this.clients.Count; ++i) 
    {
    StopClient(i);
    }
    adm.Stop();
    if (status.Status == JhThreadStatus.Running)
    {
    // listenThread.Abort();
    lock(this)
    {
    status.Status = JhThreadStatus.Stop;
    }
    for (i = 0 ; i < 3; ++i)
    {
    if (status.Status == JhThreadStatus.Running)
    {
    Thread.Sleep(110);
    }
    }
    if (status.Status == JhThreadStatus.Running)
    {
    listenThread.Abort();
    }
    }
    server.Stop();
    }
    catch (Exception e)
    {
    }
    }
    /// <summary>
    /// 断开指定索引的客户
    /// </summary>
    /// <param name="index">客户端索引</param>
    public void StopClient(int index)
    {
    GetClient(index).Exit();
    } /// <summary>
    /// 发送数据
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public void Send(JhClient client,string data)
    {
    if (client != null)
    {
    byte[] dt = System.Text.Encoding.Default.GetBytes(data);
    client.Send(dt);
    }
    } /// <summary>
    /// 用最后一个客户端发送数据
    /// </summary>
    /// <param name="data"></param>
    public void Send(string data)
    {
    Send(GetClient(clients.Count - 1),data);
    } // /// <summary>
    // /// 发送连接消息
    // /// </summary>
    // /// <param name="client"></param>
    // private void SendConnected(JhClient client)
    // {
    // client.SendMessage(JhCommMessages.Connected);
    // } /// <summary>
    /// 数据分发管理者
    /// </summary>
    CommDataMgr adm ;  Parm status = null; /// <summary>
    /// 客户连接线程
    /// </summary>
    void ListenThread()
    {
    try
    {
    while (status.Status == JhThreadStatus.Running)
    {
    if (server.Pending() == true)
    {
    Socket client = server.AcceptSocket();//侦听到连接后创建客户端
    JhClient jhClient = new JhClient(adm,client);
    jhClient.ID = clientindex--;
    jhClient.OnClosed += new CommEventHandler(this.Client_Disconnect);
    jhClient.StartReceive();
    SetClient(jhClient);
    if (OnClientConnected != null)
    {
    OnClientConnected(jhClient,new CommEventArgs("Client connected: " + jhClient.Client.RemoteEndPoint.ToString()));
    }
    }
    Thread.Sleep(100);
    }
    }
    catch (ObjectDisposedException e2)
    {
    MessageBox.Show(e2.Message);
    }
    catch (InvalidOperationException e1)
    {
    MessageBox.Show(e1.Message);
    }
    catch (Exception e3)
    {
    MessageBox.Show(e3.Message);
    }
    finally
    {
    lock(this)
    {
    status.Status = JhThreadStatus.Exit;
    }
    Console.WriteLine("JH tcp listenor exited");
    }
    }
    /// <summary>
    /// 客户端连接端开处理
    /// </summary>
    /// <param name="sender">客户端对象</param>
    /// <param name="e">事件对象</param>
    void Client_Disconnect(object sender,CommEventArgs e)
    {
    if (clients.ContainsValue(sender))
    {
    clients.Remove(((JhClient)sender).ID);
    }
    if (this.OnClientDisconnected != null)
    {
    OnClientDisconnected(sender,e);
    }
    }
    /// <summary>
    /// 添加观察者
    /// </summary>
    /// <param name="ob"></param>
    public void AddOb(IObserver ob)
    {
    adm.Add(ob);
    }
    Hashtable clients = new Hashtable(); /// <summary>
    /// 根据内部id获取客户端
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public JhClient GetClient(int id)
    {
    return (JhClient)clients[id];
    }
    /// <summary>
    /// 设置客户端对象
    /// </summary>
    /// <param name="client"></param>
    public void SetClient(JhClient client)
    {
    int i;
    for (i = 0 ; i < clients.Count; ++i)
    {
    if (((JhClient)clients[i]).ID == client.ID)
    {
    break;
    }
    }
    clients[i] = client;
    } }