Help with sockets

J

Jason L James

Hi all,

as a VB.Net programmer I am finding it difficult to
find a text on implementing sockets to communicate
with an embedded web server.

Does anyone know of a good resource, either text book
or on-line that I should refer to. I also need to
implement multi-threading to enable me to communicate
with several embedded devices at the same time.

Many thanks,

Jason.
 
C

Carlos J. Quintero [MVP]

Basically you have 3 options, from high level to low level:

- The WebClient class (since you are dealing with Web servers)

- The TcpClient class

- The Socket class: a raw managed wrapper to Winsock API.

The MSDN has documentation to learn about them. For actual samples, there
are a lot on the web. See, for example (although it is C# oriented, the .NET
framework classes are the important point):

http://www.c-sharpcorner.com/Networking.asp

Multithreading is a complex subject. The book of Dan Appleman "Moving to VB
..NET: Strategies, Concepts, and Code",
http://www.amazon.com/exec/obidos/tg/detail/-/1893115976/002-5188660-1176827?v=glance,
has the best multithreading chapter that I have read about threading and its
issues.
 
J

Jason L James

Carlos,
thanks very much. I'll let you know how I get on. My .Net app
will need to multi-thread a TcpClient class to talk to multiple
embedded servers. Does this seem like it will be possible?

Thanks,

Jason.
 
C

Carlos J. Quintero [MVP]

Sure, I am right now developing a multithreaded .NET app the uses the
TcpClient class to talk with a server using TCP/IP through multiple
connections, one for each thread, and it works fine. You can add some
Windows performance counters to see graphically the performance varying the
number of threads and impress your boss.

The idea is:

- You have a single instance of a Controller class which instanciates one or
more Processor objects and stores them in a collection.

- Each Processor class has internally its own Thread member variable, which
is set to execute a method of the class which contains a loop (in my case),
and its own TcpClient connection which is connected when the object is
created and before the thread is started.

- The Processor class exposes a Start method that starts its internal
thread.

- The Processor class exposes a Stop method, which internally sets a
variable to exit the loop.

- When the Controller wants to start or to stop the processors, it calls the
proper method on each instance of its collection of processors.

With this approach each thread runs code of a different Processor instance,
so you don´t have shared variables and write collisions.

I can´t send the code because I have written it in Spanish (and C#) but let
me know if you need more info.
 
J

Jason L James

Carlos,

it sure sounds like you know what you are doing. This area of
VB (multi-threading and sqlClient objects) is very new to me. I have
a lot of learning to do but I am going to start slowly; firstly with
multi-threading and then add sockets once things are looking OK.
This news group thread will probably be removed from most news
servers before I have finished my application. However, if you
don't mind I should like to keep in touch via email (or this group)
to seek advise and suggestions from you as I progress.

Many thanks,

Jason.
 
C

Carlos J. Quintero [MVP]

I didn´t knew anything about threads or .NET networking until 3 weeks ago,
but in .NET all this is very easy. Multithreading is difficult if threads
must share variables, but maybe you are not in that case.

So, post again if you need and feel free to e-mail me if I overlook your
messages (I spend more time in the vsnet newsgroups).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top