简体   繁体   中英

Sending and receiving data using Sockets

i'm trying to learn more about networking so i decided to do a small project using sockets. I have already been successful is creating an echo server which i know is pretty basic.

The next step would be to send all kinds of files (text files, images maybe even videos?) from the server to the client and vice versa. I have been trying to do so with FIleOutputStream, ObjectOutputStream (and input obviously) and i even tried Byte Buffer but by reading the Java docs i'm starting to understand that i really dont know much about Data....

so my questions are:

  1. Can I convert all type of data (text, images, videos) into bytes, send it and then convert it all into the data that it used to be, or do i need a specific function for each type of data?
  2. Can I convert a text file or an image into an Object and then send it with ObjectOutputFile? If so then how can i decompress it in the other side? I've tried ObjectOutputFile.readObject but i dont really know what to do after that.

Here's what I've been trying to do (I won't post all the code because, only this particular part is important i believe:

Server: (out is a DataOutputStream)

ObjectOutputStream oos = new ObjectOutputStream(out);

Path path = Paths.get("stuff/Folder.jpg");

byte[] data = Files.readAllBytes(path);

oos.write(data);

client (in is a DataInputStream):

ObjectInputStream ois = new ObjectInputStream(in);

ois.readObject();

Thanks for all the help in advance, if you think that you know an article or a tutorial that would help me learn more about this it will also help!

Can I convert all type of data (text, images, videos) into bytes, send it and then convert it all into the data that it used to be?

Sure.

Or do I need a specific function for each type of data?

That's not an "or" - if you want to convert several types of data into bytes yourself, then you probably need a separate function for each type. Note that if you are sending files, you don't need any conversion because the content of a file is already just bytes.

Can I convert a text file or an image into an Object and then send it with ObjectOutputFile?

You could send it with ObjectOutput Stream .

If so then how can i decompress it in the other side?

With ObjectInputStream.

I've tried ObjectOutputFile.readObject but i dont really know what to do after that.`

There is no such method in Java as ObjectOutputFile.readObject.

  1. it is pretty certain that you can convert any data into bytes. However, the problem is how you could convert it back? Typically, we use Base64 encoder to encode image and when clients get that we can convert it back by Base64 decoder. There is a lot of way to do this. Please Google it.
  2. Yes, you can use Object data to send. You may need to define a new class for the object and then call data back by getter. In client side, you need to declare the Object value as ois.readObject(). Like:

    Object object = ois.readObject();

Just add a little bit advice, I feel you are not so familiar with socket right now. So, a text book like Computer Networking(Kurose Ross) will be helpful. Also, if you plan to transmit data between C and Java, then you cannot use Object because C does not support it natively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM