Java Sockets - Files transfered simulatanously with other data over one connection -


I have a multi-threaded server that handles client requests, and creates new threads for each connected. This is working great and I am sending a "text" message to the server without problems. Like chat system

Now I have a way to be able to send the file to the server in this client connection, but every single example I see in Java, but always has a hard-coded file name server / client side - But I want to set my own for flexibility. And if I receive not only the files but also the "text" message on the same port, then I like it.

I have an idea by which I have a "text" message port, the moment the server tells that a customer is being sent from a file, then the server opens the "File Transfer" port Given, just for file transfer. In this way "text" can specify the name of the port file etc. And the other port could send the file without any interruption with great pleasure.

Do anyone know the best way to do this? I really do not want to use two ports, it seems something wrong. I would like to create a separate thread on the server side to deal with file transfers and at the same time, I will also deal with the "text" message, even if it is possible?

Thank you in advance, I hope I am quite clear :)

this is easy. You must specify the message type first. Either byte or with a line of text

The easiest way would be to do this with a single byte:

  // to send socket s =. .. OutputStream OS = s.getOutputStream (); If (messiceext ()) {os.write (0); // Send Text other {OS} Type (1); // send file}  

Then you can do this in your server:

  socket s = serverSocket.accept (); InputStream = s.getInputStream (); Int firstbyte = in.read (); If (firstbyte = 0) {// read text} else {// read file}  

Now, it is not very flexible, but there are many things you can do I would really recommend you to read, this is the specification for HTTP. You do not need to read the whole thing, and just enough to write a simple web server it is actually really easy (one in the original HTTP Very simple protocol, though it has many advanced features)

If you enjoy In fact want to learn network programming, it can be difficult to write an HTTP server, but do not worry, it is not really difficult at all.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -