Java Streams

Hey guys, well im currently developing an online game and im wondering. I can use an Object output stream to send objects from the server to the client. But is there a way to send an entire file through the stream and be able to read it? Kinda like an ftp accept the server will make a txt file with all the info of the server in on the fly and send it to the client who will then send a txt file back to convert it on the fly. Is this possible without using a ftp method?

BTW im using sockets for connections.

sorry if this doesnt make that much sence, but any help would be very much appreciated.

Is the problem just that you need to write a file to the server? If so, you need to write a servlet. Servlets are a part of J2EE, and I’ve only used them once or twice.

Alternatively, you might be able to write a CGI or PHP script. Or you might be able to use any other kind of server-side language.

I’m not so sure why you’d do anything HTTP based? He just wants to send a file over a stream. We have the Socket class (getInputStream/getOutputStream) and the FileInputStream for that.

For the OP, you should check the javadocs of the mentioned classes.

Just read the file in as bytes using a ByteArrayInputStream wrapped around a FileInputStream. Then write it to your socket’s stream using ByteArrayOutputStream. The server can read the bytes similarly & reconstruct the file.

If its just server deatils you want to send, why no just use DataIn/OutputStream & its methods like writeUTF to send the server info back & forth?

The ByteArrayInputStream might be my best approach. (im just really really new with bytes havnt covered them in much detail) so to me changing bytes is kinda sketchy) but thanks for the idea, ill look into using it.

basically im gonna have the server create a small txt file on the fly that has all of my enemy information (location etc) then send it to a bytearrayoutputstream (im guessing) and then just have a bytearrayinputstream waiting on the other end to take the bytes, decode and then exec.

thanks for the help

My pleasure, but there’s no need to make text files & send them back & forth. Use DataIn/OutputStream to send the relevant info. Eg on server:
out.write(enemyXCoord);
out.write(enemyYCoord);

on client:
enemyXCoord = in.read();
etc.

thats the way im currently doing it. but (correct me if im wrong) doesnt that connect, get, tell server it got, then get the next? to me it makes more sence just to send 1 txt file with ALL the information that is a max of like 1k than sending and recieveing multiple connections. thats why i think it would be faster to send 1 file rather than 20ish objects.

No, using ObjectStream is just fine. There is only one connect and the Objects are continuously streamed like the bytes of a textfile would be. The only difference is how the ObjectInputStream builds Objects out of the raw bytes sent over the network. Sending and parsing a textfile could very well be a major overhead.

Use DataInputStream & DataOutputStream like I said, they send only p[rimitives, no objects except Strings. No garbage collection then either. Maybe you should swot up on Sun’s The Java Tutorial for I/O streams or just look a bit closer at the API docs to find out more (java.io.*).

Text files are not necessary. Just send the primtives by turning them into byte arrays using the above streams & send that to & from the server.

thanks for the input guys. Now if i plan on keeping my objectoutputstreams and objectinputstreams as is should i create 2 threads one that brings data in and one that sends data out? example right now the server in its sending loop does this

wait for client change -> send updated world(thats only visible on client screen btw)

OR

should i set it up like this

thread -> always recieve
thread -> always send
both on the client and server (only problem is this is turning into a mmo and so more threads on the server would screw the server in the long run)?