About the Socket.SendPacketsAsync() method...

  • Thread starter Thread starter Reassert
  • Start date Start date
R

Reassert

As title.
If a client send a file via this method,
how do I get the file with Socket.ReceiveAsync() (or other) method on my
server application?
I guess the answer is about the SocketAsyncEventArgs class, but I don't know
how to do...

Thank you:)
 
As title.
If a client send a file via this method,
how do I get the file with Socket.ReceiveAsync() (or other) method on my
server application?

The same way you'd receive the file were it send via any other means. For
network i/o, the API used at one end doesn't dictate the API used at the
other. As long as they are both conforming to the appropriate network
protocol, they can communicate.

In other words, the fact that the data is sent using SendPacketsAsync() is
irrelevant. The data shows up at the other end the same way regardless.
I guess the answer is about the SocketAsyncEventArgs class, but I don't
know how to do...

If you use the Socket.ReceiveAsync() method, you'll have to use the
SocketAsyncEventArgs class, yes. Otherwise, no. You could use the
Socket.Receive() method or the Socket.BeginReceive() method instead and
accomplish the same thing.

As far as "how to do" goes...the basic idea is that you need code that has
a connected socket at the receiving end, from which you'll read bytes as
they arrive. As you receive those bytes, if you want them written to a
file, you need to do that yourself (e.g. using the FileStream class).
When the transmission has been completed (indicated by whatever means you
chose when you used SendPacketsAsync()...typically you'd either preface
the file data with a length, or you'd simply let SendPacketsAsync() close
the connection when done), you close the file.

There are examples of varying quality on MSDN for how to write code that
uses the Socket class. If you have specific questions about those
examples or the documentation, you should state clearly what your specific
questions are.

Pete
 
Thank you for your reply:)
So the answer is SocketAsyncEventArgs.Buffer property...same thing that I
receive a text message from client.


I had this question because the page of SocketAsyncEventArgs.Buffer on MSDN:

--

http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.buffer.aspx

--

In Remark section:

--

This property is used with the Socket.AcceptAsync, Socket.ConnectAsync,
Socket.ReceiveAsync, Socket.ReceiveFromAsync,
Socket.ReceiveMessageFromAsync, Socket.SendAsync, and Socket.SendToAsync
methods.

--

As you can see, there's no Socket.SendPacketsAsync() in this section:)


By the way,
How do I ascertain the data in buffer is a file(for example: bitmap) or text
message?
Thanks again:)
 
Thank you for your reply:)
So the answer is SocketAsyncEventArgs.Buffer property...same thing that
I receive a text message from client.

If you are using one of the "Async" Socket methods that uses
SocketAsyncEventArgs to receive, then yes...otherwise, no.
[...]
http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.buffer.aspx
--
In Remark section:
--
This property is used with the Socket.AcceptAsync, Socket.ConnectAsync,
Socket.ReceiveAsync, Socket.ReceiveFromAsync,
Socket.ReceiveMessageFromAsync, Socket.SendAsync, and Socket.SendToAsync
methods.

That's correct. That property isn't used with the SendPacketsAsync()
method, because that method specifically applies to sending files or lists
of buffers, where the SendPacketsElements property is used instead.
By the way,
How do I ascertain the data in buffer is a file(for example: bitmap) or
text message?

That has to be negotiated with the sender, typically via the application
protocol being used (e.g. HTTP, FTP, XMPP, etc.). You could inspect the
data, trying to parse it as some specific kind of data, but that's going
to work so rarely that it's not worth the trouble. Most network protocols
require advanced identification on and cooperation by both endpoints of
the connection.

Pete
 
Back
Top