Results 1 to 16 of 16

Thread: [RESOLVED] TcpListener Multi-threaded

  1. #1

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Resolved [RESOLVED] TcpListener Multi-threaded

    I've created a multi-threaded TcpListener to receive request from clients.

    I created a background worker to handle the endless loop that checks if there is a new connection.
    Code:
    bw.WorkerSupportsCancellation = true;
                    bw.WorkerReportsProgress = true;
                    bw.DoWork += new DoWorkEventHandler(bw_start);
    
                    if (!bw.IsBusy)
                        bw.RunWorkerAsync();
    This is the bw_start:
    Code:
    private void bw_start(object sender, DoWorkEventArgs e)
            {
                BackgroundWorker worker = sender as BackgroundWorker;
                
                TcpClient clientSocket = default(TcpClient);
                tListener.Start();
    
                int counter = 0;
                
                counter = 0;
                while (true)
                {
                    counter += 1;
                    clientSocket = tListener.AcceptTcpClient();
                    HandleClient client = new HandleClient();
                    client.startClient(clientSocket, Convert.ToString(counter));
                }
    
                clientSocket.Close();
                tListener.Stop();
            }
    HandleClient Methods:
    Code:
    public void startClient(TcpClient inClientSocket, string clineNo)
            {
                this.clientSocket = inClientSocket;
                this.clNo = clineNo;
                
                ThreadPool.QueueUserWorkItem(new WaitCallback(doTransact));            
            }
    Code:
    private void doTransact(object state)
            {
    //some declarations here
    
    while (True)
                {
    try
                    {
                        if (clientSocket.Connected)
                        {
    
    recv = networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize); //this part receives an error that is realted about threading
    //some processing here
    
    //send response
    
    }
    else
    {
    break;
    }
    }
    else
    {
    break;
    }
    }
    }

  2. #2

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: TcpListener Multi-threaded

    Then I've created a multi-threaded client that sends transactions with random interval between 0-1000ms.
    Then after 100+ sending it stucks in:
    Code:
    recv = networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
    When I restart the client, it says that the server isn't listening.

  3. #3

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: TcpListener Multi-threaded

    I'm trying to reproducing the error and post the exact exception here.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: TcpListener Multi-threaded

    The TcpListener inherently supports asynchronous processing via the BeginAcceptTcpClient and EndAcceptTcpClient methods.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: TcpListener Multi-threaded

    Quote Originally Posted by jmcilhinney View Post
    The TcpListener inherently supports asynchronous processing via the BeginAcceptTcpClient and EndAcceptTcpClient methods.
    I'll try it.
    But is it possible for a Java-based client to connect to an asynchronous .NET-based server?
    Thanks.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: TcpListener Multi-threaded

    Quote Originally Posted by eSPiYa View Post
    I'll try it.
    But is it possible for a Java-based client to connect to an asynchronous .NET-based server?
    Thanks.
    TCP is TCP. It's a standard protocol. It doesn't have to be a .NET TcpClient at the other end. It just has to be something talking TCP.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: TcpListener Multi-threaded

    Quote Originally Posted by jmcilhinney View Post
    TCP is TCP. It's a standard protocol. It doesn't have to be a .NET TcpClient at the other end. It just has to be something talking TCP.
    Thanks.
    But I've tried before to use an asynchronous TcpClient to connect to a Java-based server and I can't connect.
    Anyway, I'll try to test it.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: TcpListener Multi-threaded

    What's your definition of an asynchronous TcpClient? Are you talking about calling BeginConnect instead of Connect? How exactly did you do it? Did you provide a callback and then call EndConnect in that method?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: TcpListener Multi-threaded

    Quote Originally Posted by jmcilhinney View Post
    What's your definition of an asynchronous TcpClient? Are you talking about calling BeginConnect instead of Connect? How exactly did you do it? Did you provide a callback and then call EndConnect in that method?
    I can't remember how I did it exactly but I used BeginConnect then when I tried to send any message; the connection was dropped.

  10. #10

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: TcpListener Multi-threaded

    It is working with Java, but after several connect-send-receive-disconnect(500+); the server refuses another connection.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: TcpListener Multi-threaded

    Define "refuses".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: TcpListener Multi-threaded

    It seems that the socket connection drops.
    This is the error message from client:
    Code:
    No connection could be made because the target machine actively refused it
    On the server side, it doesn't throw any exception.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: TcpListener Multi-threaded

    I can't tell you much more I'm afraid as I've not really used TCP comms much myself. Hopefully the extra info will enable someone else to help you though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: TcpListener Multi-threaded

    Thanks.
    I think I just need to limit incoming connections.

  15. #15
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: TcpListener Multi-threaded

    Have you googled for that error message - first link I saw had good info

    https://www.google.com/webhp?sourcei...bih=1087&ion=1

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  16. #16

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: [RESOLVED] TcpListener Multi-threaded

    Sorry, forgot to tag this thread as resolved.

    If I remembered it correctly, after 3years, it is the .NET and Java [int] variable. One of them is using unsigned integer as default int, while the other one is signed integer.

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