Answered by:
Simultaneous send/receive using Stream Socket (TCP like)

Question
-
In case of Datagram Socket same socket provide functionality for both sending(connecting) and receiving(listening) data. But in the Stream socket listening is done in a separate class(StreamSocketListener).
1) How can we map between two classes(StreamSocket & StreamSocketListener) for multiple times send/receive ?
2) How StreamSocket knows when data arrives ?
2) Is it possible to use same socket for sending and receiving data multiple times ? If possible how ?
3) Is it possible to use a single socket for simultaneous sending/receiving at the same time(multiple times) ?
Monday, August 13, 2012 6:39 PM
Answers
-
Hello Mokarrom,
I'm not sure that I understand your question. If you are making an outgoing connection to a server you can use the StreamSocket class. Once the connection is established you can call StreamSocket.InputStream to get any incoming data from the server and StreamSocket.OutputStream to send data to the server. The calls to the underlying APIs are asynchronous so you can theoretically send and receive data simultaneously and let the framework handle any synchronization issues.
StreamSocket class
http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.streamsocket.aspxStreamSocket.InputStream
http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.streamsocket.inputstream.aspxStreamSocket.OutputStream
http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.streamsocket.outputstream.aspxI hope this helps,
James
Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/
- Proposed as answer by James Dailey - MSFTMicrosoft employee, Moderator Wednesday, August 15, 2012 12:49 AM
- Marked as answer by James Dailey - MSFTMicrosoft employee, Moderator Friday, August 17, 2012 12:40 AM
Wednesday, August 15, 2012 12:49 AMModerator
All replies
-
Hello Mokarrom,
I'm not sure that I understand your question. If you are making an outgoing connection to a server you can use the StreamSocket class. Once the connection is established you can call StreamSocket.InputStream to get any incoming data from the server and StreamSocket.OutputStream to send data to the server. The calls to the underlying APIs are asynchronous so you can theoretically send and receive data simultaneously and let the framework handle any synchronization issues.
StreamSocket class
http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.streamsocket.aspxStreamSocket.InputStream
http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.streamsocket.inputstream.aspxStreamSocket.OutputStream
http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.streamsocket.outputstream.aspxI hope this helps,
James
Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/
- Proposed as answer by James Dailey - MSFTMicrosoft employee, Moderator Wednesday, August 15, 2012 12:49 AM
- Marked as answer by James Dailey - MSFTMicrosoft employee, Moderator Friday, August 17, 2012 12:40 AM
Wednesday, August 15, 2012 12:49 AMModerator -
Dailey,
Thanks for reply.
My above problem is resolved. As read operation is asynchronous, how can i make it synchronous? For instance, in my application I need to wait only 2 s for reading. After 2s I am not interested to read anymore.
If I run both send & receive operation in two separate thread and perform sending & receiving data continuously using a single socket. Is there any chance to crash or missing data?
Thursday, August 16, 2012 10:15 PM -
Hello,
As read operation is asynchronous, how can I make it synchronous? You will need to use C++ lambdas and you can cancel the operation after 2 seconds if you wish. Please take a look at the document below.
As I stated earlier all send and receive operations are asynchronous and independent. The framework synchronizes where necessary to avoid lost data and crashes. If you do find a scenario where the APIs are causing a crash or loss of data please let us know.
http://msdn.microsoft.com/en-us/library/windows/apps/Hh780559.aspx
I hope this helps,
James
Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/
Friday, August 17, 2012 12:39 AMModerator