简体   繁体   中英

C# Server - Socket not connecting and out of memory exception

I am trying to implement a simple TCP server and I basically copied the example on MSDN sans a couple of lines and tried to make it work. I have an external client trying to connect already. This is my code:

IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
 IPEndPoint localEP = new IPEndPoint(ipHostInfo.AddressList[0], 4001);

 Socket listener = new Socket(localEP.Address.AddressFamily,
     SocketType.Stream, ProtocolType.Tcp);

 try
 {
     listener.Bind(localEP);
     listener.Listen(1000);

    while (true)
     {

         listener.BeginAccept(new AsyncCallback(AcceptCnxCallback), listener);
     }
 }
 catch (Exception e)
 {
   //Log here
 }

This is my callback:

private void AcceptCnxCallback(IAsyncResult iar)
{

    MensajeRecibido msj = new MensajeRecibido();

    Socket server = (Socket)iar.AsyncState;
    msj.workSocket = server.EndAccept(iar);

}

And this is the information of one of the incoming packages:

TCP:[SynReTransmit #1727889]Flags=......S., SrcPort=57411, DstPort=4001, PayloadLen=0, Seq=673438964, Ack=0, Win=5840 ( Negotiating scale factor 0x4 ) = 5840  

Source: 10.0.19.65 Destination: 10.0.19.59

I basically have two issues:

  1. If I use the while loop I get an OutOfMemoryException

  2. I never do manage to connect to the client

Any tips on either of the two problems? Thank you in advance!

Your problem is, that you use asynchronous calls all the time. There is no wait mechanism or similar, so generally you are just creating new asynchronous callbacks in an infinite loop.

For a basic TCP I would recommend to use the simple approach and use the synchronous methods. Accept() is blocking, so the program flow will stop until there is an ingoing connection.

while (true)
{
    Socket s = listener.Accept();

    buffer = new byte[BUFFER_SIZE];
    s.Receive(buffer);
    //Do something
    s.Send(...);

}

Noe that this is just a basic example. If you want to keep your connection you might consider a new Thread for each accepted Socket, that continoues with receiving and sending data.

First problem You are using an infinite loop to call an async method.

try it like this:

listener.BeginAccept(new AsyncCallback(AcceptCnxCallback), listener);

//add your code here (this part will be executed wile the listner is waiting for a connection.
while (true)
     {


         Thread.Sleep(100);

     } 

and change the Callbackmethod to:

private void AcceptCnxCallback(IAsyncResult iar)
{

    MensajeRecibido msj = new MensajeRecibido();

    Socket server = (Socket)iar.AsyncState;
    msj.workSocket = server.EndAccept(iar);

//call again the listener after you get a message
listener.BeginAccept(new AsyncCallback(AcceptCnxCallback), listener);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM