0

This is the situation:

I'm working on a project where I need to be able to send one or more images once in a while to/from the server as well as a lot of other types of data represented with text. The way it is currently done, is by sending a message with that says "incoming image of size x to be used as y" (It's not "formulated" that way of course), and then I call a method that reads the next x bytes through a DataInputStream. At first I met some problems with latency screwing things up, but I made the server spawn a new thread to send the "incoming image" message, and then wait for a flag that is set when the client responds with a "I'm ready for the image" message. It works in a way now, but if anything else, for instance a chat message, is sent while the image is being transfered, that message meant for a BufferedReader will be read as raw bytes and used as part of the image. So I will have to block all outgoing data (and add it to a queue) when there is an image that is being sent. But this seems very wrong and annoying, as users of the application will not be able to chat while receiving/ uploading a big image.

This is what I need: So, I either need to set up an independent channel to use for raw data. Which, as far as I understand from some tinkering, I will have to set up a new socket over a new port, which seems unnecessary. The other way I can see to solve this, would be to somehow use tag each packet with a "this is text/raw data" bit, but I have no idea how to do this with java? Can you add information to the packet-header when you write something to the stream (that every packet containing that info will contain) and then read the packet data on the other end and act accordingly?

As you can see, I do not have much experienced with networking, nor have I used Java for a long time. This is also my first post here, so be kind. If anything was unclear, please ask, and I'll specify. All ideas are welcome! (There is possibly a standard way to do this?)

Thanks a lot!

4

2 に答える 2

3

TCPプロトコル自体には、役立つものは何もありません。新しいソケット接続を開くか(同じサーバーポートに接続できます)、画像を小さなチャンクに分割し、これらのチャンクを封筒に入れて、画像またはチャットのメッセージの種類を示します。次に、これらのチャンクから受信側のイメージを再構築します。しかし、これは帯域幅を浪費し、それ自体の複雑さを追加します(たとえば、そのイメージのチャンクをどのくらい大きくしますか?)。
別のバイナリデータ接続を使用します。

于 2010-12-22T21:06:09.823 に答える
2

JavaはHTTPプロトコルを標準でサポートしている必要があります。ヘッダーで送信されるデータのタイプを設定できるため、HTTPを使用して画像を転送します。基本的に、クライアント/サーバーアーキテクチャに、新しいデータ転送(テキストまたは画像)ごとに個別の要求を確立させることで、単純なループで処理を実行できるようになります。

これはあなたの助けになるかもしれません:java.net.URLConnectionを使用してHTTPリクエストを起動して処理する方法は?

于 2010-12-22T21:06:15.440 に答える