Thread: Asynchronous Socket Thread closes after first receive

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Asynchronous Socket Thread closes after first receive

    Good day,

    I have a serious problem and need to get it done. Almost spend two days of frustration...

    I have a Tcp-Server just waiting for incoming connections and forwarding the received data.
    Everything works fine for the first time I receive data, but right after finishing the method my thread closes down - I have no clue why this happens.

    Could you have a look at my code and tell me if there is anything wrong?

    Code:
    private byte[] fByteBuffer = new byte[fConstants.cBUFFERSIZE];
    private Socket fServer;
    
            public void Start(){
    
                try {
                    fServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    IPEndPoint vEndPoint = new IPEndPoint(IPAddress.Any, ServerPort);
                    fServer.Bind(vEndPoint);
                    fServer.Listen(5);
                    fServer.BeginAccept(new AsyncCallback(AcceptConnection), fServer);
                } catch ( Exception e ) {
                    throw new Exception("TCVServer.Start() could not create Server", e);
                }
            }
    
            private void AcceptConnection(IAsyncResult aResult) {
    
                Socket vServer = (Socket)aResult.AsyncState;
                Socket vClient = vServer.EndAccept(aResult);
                Log("Connection: " + vClient.RemoteEndPoint.ToString());
                vClient.BeginReceive(fByteBuffer, 0, fByteBuffer.Length, SocketFlags.None,
                                              new AsyncCallback(ReceiveConnection), vClient);
            }
    
            private void ReceiveConnection(IAsyncResult aResult) {
    
                Socket vClient = (Socket)aResult.AsyncState;
                int vReceivedBytes = vClient.EndReceive(aResult);
    
                if ( vReceivedBytes > 0 ) {
    
                    //These 3 lines handle the received data
                    TXPacket vPacket = new TXPacket();
                    vPacket.ReadFromBuffer(fByteBuffer);
                    fOwner.RemoteAccess(vPacket);
    
                    vClient.BeginReceive(fByteBuffer, 0, fByteBuffer.Length, SocketFlags.None,
                                                  new AsyncCallback(ReceiveConnection), vClient);
                } else {
                    fServer.BeginAccept(new AsyncCallback(AcceptConnection), fServer);
                    Log("Connection lost");
                }
            }

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    What happens when it closes down? Do you get an exception? Does your log output "Connection lost"? Is your client persisting the connection or closing after it sends its payload?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 10:01 AM
  2. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM
  3. Multithreading
    By Cela in forum Windows Programming
    Replies: 13
    Last Post: 01-15-2003, 03:02 PM
  4. Your Best thread and your most stupid thread ??
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 01-03-2003, 07:41 PM
  5. Multi-Thread Programming
    By drdroid in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2002, 02:53 PM