Sync. socket client and server example

O

Ole

Hi,

I'm going to develop a socket communication between an instrument (running
CE 5.0 with Compact Fraework V2) and a PC. As the instrument should only
connect to one PC at a time I believe that the sync version of the socket
should be the easiest - or what??? (please tell if I'm wrong).

Are there anyone who is able to point to a Server and Client example code in
C# VS2005 (I've only been able to find a C++ version) ???

Thanks
Ole
 
P

Paul G. Tobey [eMVP]

The main reason to use or not to use a sync socket is how your program is
coded and what else might need to happen on the user interface thread (the
thread that controls the display of forms, etc.) If your data transfer code
is executing is a *different* thread from this UI stuff, I'd use a sync
socket. If not, you'll probably *have* to use an async socket to allow user
interface processing and keep the user interface responsive.

Paul T.
 
O

Ole

Hi again,

I'm still a bit confused about when to use sync or async socket
communication so I will descibe my needs instead - hoping someone will
comment:
I have a socket server on a CE device communicating with a socket client on
a PC. The server must be able to receive and handle data from the client PC
while transferring other data to the same client - sort of full duplex.

How should I organize my program (sync or async)??

Thanks
Ole


"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT
com> wrote in message news:[email protected]...
 
K

Kevin Spencer

A Socket is simply an interface to another Socket, on the same machine or
another machine in a network. There is nothing particularly special about
Sockets that makes the rules regarding using them synchronously or
asynchronously in an app any different than any other programming component.
A Socket sends and receives data. So, a synchronous Socket will send and
receive data and block while sending or receiving. An asynchronous Socket
will not block. So, if you think of the sending and receiving as if it were
a method of a class, when would you use a separate thread to call a method
of a class? That's right: when the method blocks execution for a long time,
and the app would work better if the method ran asynchronously. For example,
an email client will send and receive emails asynchronously, so that the
user can perform other tasks with it while it is doing so.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

Ole said:
Hi again,

I'm still a bit confused about when to use sync or async socket
communication so I will descibe my needs instead - hoping someone will
comment:
I have a socket server on a CE device communicating with a socket client
on a PC. The server must be able to receive and handle data from the
client PC while transferring other data to the same client - sort of full
duplex.

How should I organize my program (sync or async)??

Thanks
Ole


"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT
com> wrote in message news:[email protected]...
The main reason to use or not to use a sync socket is how your program is
coded and what else might need to happen on the user interface thread
(the thread that controls the display of forms, etc.) If your data
transfer code is executing is a *different* thread from this UI stuff,
I'd use a sync socket. If not, you'll probably *have* to use an async
socket to allow user interface processing and keep the user interface
responsive.

Paul T.
 
W

William Stacey [MVP]

I would just use sync on another thread as it is much easier to get right.

--
William Stacey [MVP]

| Hi again,
|
| I'm still a bit confused about when to use sync or async socket
| communication so I will descibe my needs instead - hoping someone will
| comment:
| I have a socket server on a CE device communicating with a socket client
on
| a PC. The server must be able to receive and handle data from the client
PC
| while transferring other data to the same client - sort of full duplex.
|
| How should I organize my program (sync or async)??
|
| Thanks
| Ole
|
|
| "Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT
| com> wrote in message | > The main reason to use or not to use a sync socket is how your program
is
| > coded and what else might need to happen on the user interface thread
(the
| > thread that controls the display of forms, etc.) If your data transfer
| > code is executing is a *different* thread from this UI stuff, I'd use a
| > sync socket. If not, you'll probably *have* to use an async socket to
| > allow user interface processing and keep the user interface responsive.
| >
| > Paul T.
| >
| > | >> Hi,
| >>
| >> I'm going to develop a socket communication between an instrument
| >> (running CE 5.0 with Compact Fraework V2) and a PC. As the instrument
| >> should only connect to one PC at a time I believe that the sync version
| >> of the socket should be the easiest - or what??? (please tell if I'm
| >> wrong).
| >>
| >> Are there anyone who is able to point to a Server and Client example
code
| >> in C# VS2005 (I've only been able to find a C++ version) ???
| >>
| >> Thanks
| >> Ole
| >>
| >
| >
|
|
 
O

Ole

Thanks,

Is it "threadsafe" to use socket.send in one thread and socket receive in
another?

Regards,
Ole
 
P

Paul G. Tobey [eMVP]

Yes. It should be obvious, though, that there is no guarantee of ordering
of sends and receives. You can't assume, for example, that no data will be
received until your send is complete, or that either a send or a receive is
atomic and will not return until all data that you think belongs together is
received or sent.

Paul T.

Ole said:
Thanks,

Is it "threadsafe" to use socket.send in one thread and socket receive in
another?

Regards,
Ole

William Stacey said:
I would just use sync on another thread as it is much easier to get right.

--
William Stacey [MVP]

| Hi again,
|
| I'm still a bit confused about when to use sync or async socket
| communication so I will descibe my needs instead - hoping someone will
| comment:
| I have a socket server on a CE device communicating with a socket
client
on
| a PC. The server must be able to receive and handle data from the
client
PC
| while transferring other data to the same client - sort of full duplex.
|
| How should I organize my program (sync or async)??
|
| Thanks
| Ole
|
|
| "Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam
DOT
| com> wrote in message | > The main reason to use or not to use a sync socket is how your
program
is
| > coded and what else might need to happen on the user interface thread
(the
| > thread that controls the display of forms, etc.) If your data
transfer
| > code is executing is a *different* thread from this UI stuff, I'd use
a
| > sync socket. If not, you'll probably *have* to use an async socket
to
| > allow user interface processing and keep the user interface
responsive.
| >
| > Paul T.
| >
| > | >> Hi,
| >>
| >> I'm going to develop a socket communication between an instrument
| >> (running CE 5.0 with Compact Fraework V2) and a PC. As the
instrument
| >> should only connect to one PC at a time I believe that the sync
version
| >> of the socket should be the easiest - or what??? (please tell if I'm
| >> wrong).
| >>
| >> Are there anyone who is able to point to a Server and Client example
code
| >> in C# VS2005 (I've only been able to find a C++ version) ???
| >>
| >> Thanks
| >> Ole
| >>
| >
| >
|
|
 

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