Java Sockets - files transferred simulatively with other data on one connection

I have a multi-threaded server that processes client requests and creates new threads for each connected one. This works great, and I can send text messages back and forth to the server without any problems. Like a chat system.

Now I mean a way to send files through these client connections to the server, but every single example that I see in java always has a hard-coded file name on the server / client side, but I want to set the flexibility for myself. And I would be very pleased if he could not only receive files, but also receive text messages on the same port simulatanously.

I have an idea that at the moment there is a “text” message port, tells the server that the file will be sent from the client, and then the server opens the “file transfer” port, only for transferring files, Thus, the “text” port could specify a name, etc. File. And another port can send files quite happily, without interruptions.

Does anyone know a better way to do this? I really don't want to use two ports, this seems a bit messy. I would prefer to make a separate server-side thread to deal with file transfers, as well as deal with a text message at the same time, if possible?

Thanks in advance, I hope I was clear enough :)

+4
source share
5 answers

Of course, this is easy. You just need to specify the type of message first. Either with a byte or with a line of text.

The easiest way is with one byte:

//to send Socket s = ... OutputStream os = s.getOutputStream(); if(messageIsText()){ os.write(0); //send text else{ os.write(1); //send file } 

then on your server you can do this:

 Socket s = serverSocket.accept(); InputStream in = 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 recommend you read RFC 2616 , which is a specification for HTTP. You do not need to read all this, and just write a simple web server. It is really very simple (HTTP is a really simple protocol in the kernel, although it has many additional features)

If you really want to learn network programming, try writing an HTTP server. It may seem complicated, but don’t worry, it’s actually not so difficult.

+4
source

It looks like you are reinventing FTP. Maybe you should take a look at FTP servers implemented in Java. WebDAV

+1
source

Put the file in pieces and send them along the "normal" line.

Something link:

  • text message
  • start file (id <- id number invented by the client, file name)
  • part of the file (id, with some bytes in the byte [])
  • final file (id)

the server receives the initial file and starts saving the new structure: * client ID * invented id (and why not! :)

add bytes next to each file fragment (possibly writing to disk or something else)

The destination file name will say: ok, flush and close!

The server can save files on the map by matching them with the Key object (clientid + fileid) in the temp structure.

0
source

You describe how FTP works. And yes, this is dirty, especially the part where the server opens the port, because it just doesn’t work at this age of firewalls and NAT. Thus, FTP had to add “passive mode” when the client opens an additional port.

But there really is no reason to have separate ports. Why not just have one text message that says “here comes the file” and immediately sends the file to the same port?

0
source

Well, you have two options: out-of-band transmission (like FTP, yes, the guys and the guys above have already said this), or you can do alternating streams. For example, on the client, you issue a type / size header to the stream, and then upload your file segment or text message (corresponding byte length). On the server, you can read / write file segments through one stream for each connection, while processing text messages in some central consumer stream (with one asynchronous producer per connection stream).

For this cool rotation, you can try either the goob protobuf library - they have nice primitives for reading / writing small pieces of data into a stream, or a plain / old Object (In | Out) putStream .

0
source

Source: https://habr.com/ru/post/1299320/


All Articles