Results 1 to 5 of 5

Thread: another sockets question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207

    another sockets question

    This is an example that I found.

    PHP Code:
        public static void Main()
        {    
     
            try
            {    
                
    // Set the TcpListener on port 23.
                
    Int32 port 23;
                
    IPAddress localAddr IPAddress.Parse("127.0.0.1");
                   
                
    // TcpListener server = new TcpListener(port);
                
    TcpListener server = new TcpListener(localAddrport);

                
    // Start listening for client requests.
                
    server.Start();
           
                
    // Buffer for reading data
                
    Byte[] bytes = new Byte[256];
                
    String data null;

                
    // Enter the listening loop.
                
    while(true
                {
                    
    Console.Write("Waiting for a connection... ");
          
                    
    // Perform a blocking call to accept requests.
                    // You could also user server.AcceptSocket() here.
                    
    TcpClient client server.AcceptTcpClient();            
                    
    Console.WriteLine("Connected!");

                    
    data null;

                    
    // Get a stream object for reading and writing
                    
    NetworkStream stream client.GetStream();

                    
    Int32 i;

                    
    // Loop to receive all the data sent by the client.
                    
    while((stream.Read(bytes0bytes.Length))!=0
                    { 
    My question is does anyone know how to modify it so that I can listen on a port for more than one connection.

    Thanks.
    Jeremy

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I'm not too keen on network programming, but wont it listen for all connections to that port?
    Dont gain the world and lose your soul

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    I would think so but if you telnet in to this example more than once, it will only return data to the first connection.

    Jeremy

  4. #4
    Lively Member
    Join Date
    Jul 2001
    Location
    everywhere
    Posts
    111
    I'm new to network programming, but I'm working on an app of my own. I am using asynchronous client/server socket connections and once a new connection is created, I spin off a new class to handle the client and then go back to listen on the port for a new connection.

    Here's an example from MSDN on asynchronous sockets (nonblocking):
    http://msdn.microsoft.com/library/de...ientsocket.asp
    and that's where I got started with my program, hope that helps out.

    Jacob438

  5. #5
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    I have spent the past week trying to find out a way to implement an easy way to send data using ftp in my applications, and so far without luck. I did it quite easily in Visual Basic, but then I used Winsock controls that had events for everything... Just code and it worked...

    With C# it is much harder I think. But when I read the MSDN article on asynchronous sockets, I realized that I could use the delegetes pretty much the same way as vb uses events for it's controls.

    When using ftp, I need 2 sockets, one that listens to a port and one that sends data to port 21. I also need them to operate in two threads due to their asynchronous nature... and that's precicely what async. sockets is all about!!!!!

    Now comes the real question... how do I really start? In the MSDN article, the methods where nice and all.. but I wish there where some kind of full example... not just the methods....


    best regards
    Henrik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width