Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / November 2003

Tip: Looking for answers? Try searching our database.

Problem with Socket.Select()

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jerry M - 19 Nov 2003 07:27 GMT
I am having a problem with my Socket.Select statment. I
have included the code below for reference. I basically
accept new sockets from clients and add them to an
ArrayList. I execute a Select() to see which ones have
data to be read. What is happening if at some point, when
my arraylist is at a certain number, and all of these
sockets have something to be read, I get an Index out of
range option when the Select() method is called. Anyway to
fix this?

    public static void Main()
    {
        ArrayList sockList = new ArrayList();
        ArrayList copyList = new ArrayList();
        Socket main = new Socket
(AddressFamily.InterNetwork,
            SocketType.Stream,
ProtocolType.Tcp);

        IPEndPoint iep = new IPEndPoint
(IPAddress.Any, 9050);
        byte[] data = new byte[1024];
        string stringData;
        int recv;

        TcpListener l = new TcpListener
(IPAddress.Any, 9050);
        //main.Bind(iep);
        //main.Listen(1);

        l.Start();
        try
        {
            while (true)
            {
               
           
    //System.Threading.Thread.Sleep(1000);
                //if (main.Poll
(1000000,SelectMode.SelectRead))
                if (l.Pending())
                {
                    Socket client1 =
l.AcceptSocket();
                    //    client1.set
                    IPEndPoint iep1 =
(IPEndPoint)client1.RemoteEndPoint;
                    Console.WriteLine
("Connected to {0}", iep1.ToString());
                    sockList.Add
(client1);
                }

                if (sockList.Count > 0)
                {
                    copyList = new
ArrayList(sockList);
                    Console.WriteLine
("Monitoring {0} sockets...", copyList.Count);
                    Socket.Select
(copyList, null, null, 10000000);

                    foreach(Socket
client in copyList)
                    {
                       
                        data = new
byte[1024];
                        recv =
client.Receive(data);
                        stringData
= Encoding.ASCII.GetString(data, 0, recv);
                   
    Console.WriteLine("Received: {0}", stringData);
                        if (recv
== 0)
                        {
                       
    iep = (IPEndPoint)client.RemoteEndPoint;
                       
    Console.WriteLine("Client {0} disconnected.",
iep.ToString());
                       
    client.Close();
                       
    sockList.Remove(client);
                            if
(sockList.Count == 0)
                            {
                           
    Console.WriteLine("Last client disconnected, bye");
                           
    return;
                            }
                        }
                    }
                }
            }
        }
        catch (SocketException ex)
        {}
        finally
        {
            //main.Close();
            l.Stop();
        }
    }

Please help!
osaleh - 20 Nov 2003 22:13 GMT
I feel your pain man. Same problem we have here. Try to see if you
have a way to change the FD_SETSIZE in c# to be more than the default
64 sockets. That is the reason why you have the index out of range
exception. If you figure out, share with the rest of us.

Thx
osaleh

> I am having a problem with my Socket.Select statment. I
> have included the code below for reference. I basically
[quoted text clipped - 105 lines]
>
> Please help!
Lloyd Dupont - 25 Nov 2003 04:20 GMT
just a dummy thought which don't answer your question straight away ...
I write my own channel with pooled socket and many other things, and select
seems akward to use.

finally, after much reading, the better solution I found, which "might" not
seem as elegant (short) but prove to be much more reliable and flexible has
been to write my my own thread (1 per socket) continuously reading.
therefore I know immediately when the connection break or has available data
and many other advantages ...

> I am having a problem with my Socket.Select statment. I
> have included the code below for reference. I basically
[quoted text clipped - 104 lines]
>
> Please help!

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.