Packet creation in java?
Am working on TCP Sockets and I need to create TCP packets in Java so that I can transfer my file packet by packet over the network & also it would help me add some additional information to the packet along with the data.
Is it poss开发者_如何学运维ible? If yes could you please provide any examples?
Thanks in advance
This is a very strange request, and I very much doubt that you ... really ... need to do it.
Lets start with what you are trying to achieve from the way you seem to be proposing to do it:
... transfer my file [...] over the network & also it would help me add some additional information [...] along with the data.
So you want to transfer a file together with some additional data / metadata. It is not clear if the metadata needs to be embedded in the file data, but I cannot see why, so I'll assume not. (But it really doesn't make a lot of difference.)
Here are some possible ways to do this:
Use the FTP protocol to transfer the file and the metadata. The file and metadata can be treated as one unit (e.g. in a ZIP archive) or as separate files. You can arrange this so that it is a client to server, or server to client transfer. You can use off-the-shelf client and server software. FTPS is a more secure alternative.
Use the HTTP protocol to transfer the file and the metadata. The file and metadata can be treated as one unit (e.g. MIME multipart document) or as separate documents. Rest as for FTP. HTTPS is a more secure alternative.
You can create a custom protocol that runs over a TCP/IP connection, on whatever port you want. You can encode and transmit the file and metadata any way you want. For more security, use a SSL/TLS over TCP/IP connection.
You can split the file into small chunks and send as "packets" over UDP. You have to deal with details of detecting and recovering from packet loss yourself. This is not recommended.
You could (in theory) implement TCP/IP or UDP/IP over raw network packets using JPCAP. Then run one of the above over the TCP/IP or UDP/IP stack you've just created. This is a really bad idea. It is huge amount of (unnecessary) work. It is liable to lead to reliability issues for your application and potentially for your entire local network.
I'd recommend options 1. or 2. as best, with 3. as an alternative if there are particular reasons why the HTTP & FTP protocols won't work for you.
(If you really want to do 5. then I'd be very surprised if you can find any examples of TCP/IP implemented using JPCAP. It is too nutty an idea ...)
You can use JPCAP to construct raw TCP packets, however I would ask why you would need to if the purpose is to transfer a file. To transfer a file, you don't need to determine how it is transferred packet by packet for 99%+ of use cases.
If you want to have control over individual packets, most certainly TCP is the wrong protocol for you - look at UDP. (This is accessible by DatagramSocket or DatagramChannel in Java.)
For TCP, you simply have a byte-Stream (or in fact, two streams - a sending OutputStream and a receiving InputStream), and don't have to care about the underlying packets sent by your TCP implementation.
精彩评论