tags:

views:

102

answers:

9

I'm a dotnet propgrammer. recently i wrote aclient server application that use system.net.sockets for connecting and check client is on with a timer that clients send byte.minvalue for checking alive. when a client disconnected i shutdown the socket and close it. this work fine, but when number of clients increased connections can't established and problem occured. I use backlog with 2000 value but don't work correctly? Help Me!

+1  A: 

That's pretty vague, some more detail (the errors that you're getting on the client, and/or the server) or some code (how you're accepting connections on the server?) might help.

In the meantime, I'll throw some random guesses at you...

If you're creating and destroying connections from your clients quickly and you're testing your server by running lots of clients on the same machine then you may be suffering from running out of sockets due to TIME_WAIT. Likewise if you're testing your server by creating lots of client connections (generally more than 4000) from the same windows machine then you may be running into the default MAX_USER_PORT setting which severely limits the number of concurrent outbound connections that you can make at one time.

Len Holgate
A: 

tanx Len Holgate for ur answer. no i'm not in testing mode i release my app and problem occured. i opening socket from client side and wait for reply from server. sometimes "queue is full..." error displayed in client window.

fakhrad
This is not what an answer is for. Please delete this answer and add the text either to your question or as a comment on the answer you're referring to.
C. Ross
As I said before, it might be easier if you give more precise details of the error that you're getting; the error code would be useful but the full error message (assuming it's from FormatMessage() would be good enough). It is, of course, possible that the lack of precision you're showing in asking your question is also present when you're writing code and that may be the underlying cause of your problems.
Len Holgate
A: 

Please show a snippet of your code. Also, tell us what exactly are you experiencing - the stack trace of the exception along with the message.

Also, it will help if you get a tracelog of your repro scenario and post the relevant snippet here, or copy the whole log to pastebin.com and post a link here.

here are the instructions for generating the tracelog: http://ferozedaud.blogspot.com/2009/08/tracing-with-systemnet.html

feroze
+1  A: 

Os Version Win2003 Server service Pack 2 DotNet framework : 2.0 Sample code Here :

        listener = new TcpListener(247);
        listener.Start(1000);
        while (true)
        {
            try
            {
                    **TcpClient clt = this.listener.AcceptTcpClient();**
                    lock (this)
                    {
                        this.OnClientAccepted(clt);
                    }

            }
            catch (SocketException ex)
            {

            }
        }

    void OnClientAccepted(TcpClient clt)
    {
        Client x1;
        Session session = new Session();
        IPEndPoint ep = clt.Client.RemoteEndPoint as IPEndPoint;
        session.IPAddress = ep.Address.ToString();
        session.ClientSidePort = ep.Port;
        session.LastPacketRcvTime = DateTime.Now;
        ServerApplicationManager.Instance.AddSession(session);
        x1 = new Client(clt, session);
        x1.Connected += new Client.ConnectedEventHandler(this.OnClientConnected);
        x1.Disconnected += new Client.DisconnectedEventHandler(this.OnClientDisConnected);
        x1.LineReceived += new Client.LineReceivedEventHandler(this.OnLineReceived);
        x1.SessionID = session.SessionID;
        x1.Send(session.SessionID);

    }

Mostly No Exception Occured. When New Connection Arrived Blocked(Locked) In AcceptTcpClient Method And Memory Usage Increased!! Then No Connection Could Be Stablieshed.

fakhrad
A: 

Why are you locking when calling OnClientAccept? It's a bottle neck.

If you need a lock, do it more finegrained inside OnClientAccept.

Also. Switch to BeginAccept/EndAccept to increase speed.

internal class SocketServer
{
    private readonly IPAddress _address;
    private readonly int _port;
    private TcpListener _listener;

    public SocketServer(IPAddress address, int port)
    {
        _address = address;
        _port = port;
    }

    public void Start(int backlog)
    {
        if (_listener != null)
            return;

        _listener = new TcpListener(_address, _port);
        _listener.Start(backlog);
        _listener.BeginAcceptSocket(OnAccept, null);
    }

    private void OnAccept(IAsyncResult ar)
    {
        TcpClient client = null;
        try
        {
            client = _listener.EndAcceptTcpClient(ar);
        }
        catch(Exception err)
        {
            // log here. Eat all exceptions so the server will not die.
            // i usually have a ExceptionThrown event to let other code
            // debug asynchrounous exceptions.
        }

        // Begin to accept clients asap
        try
        {
            _listener.BeginAcceptTcpClient(OnAccept, null);
        }
        catch(Exception)
        {
            // read above exception comment.
        }


        // this accept failed, lets not do anything with the client.
        if (client == null)
            return;

        try
        {
            OnClientAccepted(client);
        }
        catch(Exception)
        {
            // read above exception comment.
        }
    }

    private void OnClientAccepted(TcpClient client)
    {
        throw new NotImplementedException();
    }


}
jgauffin
A: 

removed, double post.

jgauffin
A: 

Tanx jgauffin Block Occured Automatically When connection arrived. it's a problem!

fakhrad
My code above will never block. If my answer solved your problem, please mark it as an answer or vote on it.
jgauffin
A: 

How quickly are clients connecting/disconnecting? TCP sockets don't close immediately, they go into a TIME_WAIT state and hang around for a while (I think the default on Windows is 120secs). This may lead to all sockets being in use and new connections refused.

MSDN info here: http://msdn.microsoft.com/en-us/library/ms819739.aspx

On the server type:

netstat -a

If you have a large number of TIME_WAIT connections then you need to reduce the time closed sockets hang about.

Paolo
A: 

tanx jgauffin I Used Your SocketServer Class But Problem Exists! Client Cann't Connect(wait on EndAccept.. method) and MemoryUsage Increased extremely!! Help plz!

fakhrad
It's most likely your client code? Subscribing on TcpClient events? Then stop subscribing when a client disconnects.Show us your client handling code.
jgauffin