-1

我有 2 个程序,客户端和服务器,客户端程序通过TCP协议使用特定端口(EX:1370)发送数据。
我使用以下代码在我的服务器程序中等待客户端。

IPAddress IP = (my IP Address);
IPEndPoint ipep = new IPEndPoint(IP, 1370);

listenSocket = new Socket(AddressFamily.InterNetwork, 
                          SocketType.Stream, 
                          ProtocolType.Tcp);
listenSocket.Bind((EndPoint) ipep);
listenSocket.BeginReceive(clientData, 0, clientData.Length,
                          SocketFlags.None, new AsyncCallback(OnReceiveClient), null);

我在最后一行有一个错误,套接字无法接收 TCP 协议中的数据。这段代码在 UDP 协议中工作得很好。你能帮助我吗?!(谢谢)

4

3 回答 3

2

长话短说,TCP/IP 协议有连接建立阶段。所以服务器必须调用bind(),客户端listen()accept()必须调用connect()。建立连接后,服务器accept()返回一个新的客户端-服务器套接字。该套接字允许服务器与客户端通信(即服务连接)。

我想向您推荐以下示例:

  1. 同步服务器套接字示例
  2. 同步客户端套接字示例
于 2012-08-03T09:51:33.843 回答
1

代码应该是这样的

IPAddress IP = (my IP Address);
listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

IPEndPoint ipep = new IPEndPoint(IP, 1370);
listenSocket.Bind((EndPoint) ipep);

listenSocket.Listen(4);
// Create the call back for any client connections...
listenSocket.BeginAccept(new AsyncCallback (OnClientConnect), null);

并且一旦客户端连接

// This is the call back function, which will be invoked when a client is connected
 public void OnClientConnect(IAsyncResult asyn)
 {
   try
   {
        Socket workerSocket = m_mainSocket.EndAccept (asyn);        
        workerSocket.BeginReceive();                    
        // Since the main Socket is now free, it can go back and wait for
        // other clients who are attempting to connect
        m_mainSocket.BeginAccept(new AsyncCallback ( OnClientConnect ),null);
   }
   catch(ObjectDisposedException)
    {
    }
    catch(SocketException se)
    {
    }

  }
于 2012-08-03T09:31:45.593 回答
0

试试这个代码

           public void Listen()
            {
            string portStr = "5656";
            int port = System.Convert.ToInt32(portStr);
            // Create the listening socket...
            m_mainSocket = new Socket(AddressFamily.InterNetwork,                            SocketType.Stream,ProtocolType.Tcp);
            IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);
            // Bind to local IP Address...
            m_mainSocket.Bind(ipLocal);
            // Start listening...
             m_mainSocket.Listen(4);
             btn_start.Enabled = false;
             lbl_connect.Visible = true;
            // Create the call back for any client connections...
            m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
           }



             public void OnClientConnect(IAsyncResult asyn)
             {
             m_workerSocket[m_clientCount] = m_mainSocket.EndAccept(asyn);
            // Let the worker Socket do the further processing for the 
            // just connected client
            WaitForData(m_workerSocket[m_clientCount]);
            // Now increment the client count
            ++m_clientCount;
            // Display this client connection as a status message on the GUI    


            // Since the main Socket is now free, it can go back and wait for
            // other clients who are attempting to connect
            m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);

//** 等待客户端数据

            public void WaitForData(System.Net.Sockets.Socket soc)
            {
                try
                {
                   if (pfnWorkerCallBack == null)
                   {
                       // Specify the call back function which is to be 
                      // invoked when there is any write activity by the 
                      // connected client
                      pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
                   }
                    SocketPacket theSocPkt = new SocketPacket();
                    theSocPkt.m_currentSocket = soc;
                   // Start receiving any data written by the connected client
                   // asynchronously
                    soc.BeginReceive(theSocPkt.dataBuffer, 0,                                                      theSocPkt.dataBuffer.Length,
                               SocketFlags.None,
                               pfnWorkerCallBack,
                               theSocPkt);
                    }
                    catch (SocketException se)
                    {
                        MessageBox.Show(se.Message);
                    }

               } 
于 2012-08-03T10:55:58.483 回答