CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2001
    Location
    North Carolina, USA
    Posts
    11

    Multithreading & Winsock

    I know multithreading in VB or even VC++ has been hot topics recently. I am trying to write an application which uses winsock technology to implement a tcp/ip server. I want to accept non-blocking connections on several different protocols such as TELNET, FTP, NNTP, SMTP, etc... Can someone explain to me how this can be done in VB if possible.

    Thanks!
    Chris Cranford

  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Multithreading & Winsock

    in order to implement non-blocking, you will need to use winsock version 2, which provides a sub-set of API specially for asyn (non-blocking) connection. refer MSDN, winsock programming.

    I did one with MFC CAsyncSocket, however, if u do multithreading, then u wouldn't bother whether it's non-blocking or blocking as you will have another thread for reading the data.

    Anyway, back to VB, the simple way to do non-blocking connection is using Winsock control. I did use it for POP3, SMTP programming. you can find sample in vbPop3 in vbLib (http://vblib.virtualave.net). However, it's only tested in my server as I use it to check, send & receive mail. In order to implement FTP, Telnet, you will need rfc on it, where you can find from http://www.freesoft.org/CIE/RFC/. In vblib.virtualave.net, here is a sample in multithreading (section work in progress), but, beware, you just need to be more careful in program multithreading program.

    HTH


  3. #3
    Join Date
    Apr 2001
    Location
    North Carolina, USA
    Posts
    11

    Re: Multithreading & Winsock

    The way that I am trying to write my program is 1 thread per connection. Therefore, each thread will be responsible for Reading and Sending of data. Therefore, it would be important to use Asyncronous sockets so that events can be fired when data comes in or when data needs to be sent to the remote system. (bidirectional).

    The multithreading example on that website, is there a way I could get a copy of the source for vbLibDll.Dll? I would like to make some modifications but would be willing to credit the author for their code ...

    Thanks!
    Chris Cranford

  4. #4
    Join Date
    Apr 2000
    Posts
    737

    Re: Multithreading & Winsock

    as u can see from the work in progress section, I got it into a class vbMultiThreading, which doesn't use the vbLibDll anymore, please send me ur email, and I will send it to u.


  5. #5
    Join Date
    Apr 2001
    Location
    North Carolina, USA
    Posts
    11

    Re: Multithreading & Winsock

    [email protected] is the email address. Let me ask you, how will I be able to implement winsock using this class method? Now the only question I have is whether there is a way to implement winsock using your vbMultithreading class?

    Thanks!
    Chris Cranford

  6. #6
    Join Date
    Apr 2000
    Posts
    737

    Re: Multithreading & Winsock

    what u need to do is declare a function in module (not class module) like this

    public function test(param as long) as long

    winsock function for receiving data .....

    end function

    handle = vbMultiThreading.RunThread(Addressof test)

    remember to save the handle for terminating the thread.

    but, multithreading in vb is really not proven, if not, microsoft would have implement it, don't really have to wait until VB.Net.

    I haven't used that class for any program yet, as I am more keen to use VC++ MFC for multithreading program. And as a matter of fact, winsock control in VB doesn't need any multithreading in order to do Async winsock, why bother implement it yourself ?? If u look at the vbPop3 class, it's really an Async class and I got it work with my server. So, If I use VB, I rather use winsock control and forget about all those troublesome stuff.

    HTH

    hope u receive the class file already


  7. #7
    Join Date
    Apr 2001
    Location
    North Carolina, USA
    Posts
    11

    Re: Multithreading & Winsock

    It's important that my server be able to accept multiple connections for a particular port. I need to be able to accept like 16 TELNET concurrent sessions without 1 connection blocking or suspending or causing a timeout on another connection (such as sending/receiving files). understand why I need multithreading?

    Thanks!
    Chris Cranford

  8. #8
    Join Date
    Apr 2000
    Posts
    737

    Re: Multithreading & Winsock

    yap, I understand now. u want to listen to a socket and create extra socket for individual connection, I guess.

    Anyway, I hope I have gave u some hints on doing that.


  9. #9
    Join Date
    Apr 2001
    Location
    North Carolina, USA
    Posts
    11

    Re: Multithreading & Winsock

    Is there anyway to pass winsock information between threads in Visual Basic?

    Thanks!
    Chris Cranford

  10. #10
    Join Date
    Apr 2000
    Posts
    737

    Re: Multithreading & Winsock

    declare public variable in module and it will be accessible to several threads. e.g.

    global i as long

    assume you got two threads,

    public function test(param as long) as long
    you can use i here ...
    end function

    public function test2(param as long) as long
    you can use i here also...
    end function

    it might not work because of vb apartment-thread model, i am not sure, but i used it before, it seems to work.

    HTH


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

Featured