PDA

View Full Version : [Qt] File transfering and others



Xandareva
5th November 2010, 16:40
Hello there,

I am creating a advanced Chat with client and server. I want to create file transfering into application. For example every client connected in chat, can choose and send a file to the server or choose the user online on the chat and send a file to him. How can I do it? I have seen few codes but I don't know how can I use it. I think that file transfering can be done easier than methods which I saw. I have seen some code which is easy to implements, to use and not too long, but I can't find it again.

Client sending message to the server, send packets to server, in every "message to server" is header of type message (like user connected, user send message, user disconnected..). I send that method before the main message like: "type|main message". Server interprets that message from client and looks at "type" so server knows what do. But if I send file how can I inform that I will send a file?

Can you give me some code of implement file transfering?

Best regards.

Talei
5th November 2010, 18:33
Judging from Your question I think Your solution won't work on actual network.
You probably testing Your server/clients in the LAN network and everything is working fine, but on actual network You need to use NAT Traversal. The problem is that most users are behinde routers (they don't have external IP assigned to them) so You don't know IP of particular user/client in LAN and thus can't send anything to them (not only a file but a messages also - if Your server supports sending messages arrived to the server while user was off-line).

Example 2 users use Your clients, they are behind router with internal/LAN Ip's 192.168.0.2 - 192.168.0.3. How do You send a file file/message to one of them?

See/study udp hole punching, UPnP, NAT-PMP, NAT traversal etc. Or simply use ready solution/class.
Sorry for not specific answer but your question is broad.

Xandareva
5th November 2010, 18:56
Sending messages works FINE. I don't have problems with that, all works good, I can send messages through LAN. But I don't know how transfer file from client to server :D

Talei
5th November 2010, 19:21
Are You sure that You can send message to client that is on different LAN? Example 1st client external ip xxx.xxx.xxx.xxx LAN IP 192.168.0.2 sends the data to 2nd client yyy.yyy.yyy.yyy LAN IP 192.168.0.2. Sorry for asking but net traversal is much harder then simply sending file so that's why I'm asking.

You have client Ip then simply send the data using QSocket or whatever class You are using.
Basically You read the data, prepare a header (it's not TCP header but Your own) with file size - add (first on data part) information about current position of the data chunk (see max data size for tcp), send small chunk, if EOF finish. Client receive the data, parse header, read file size, counts current file size (present with data received - parse it out), then if (current size) == (file size) finish.
See chunked transfer encoding of the HTTP proto. You can do the same, simply prepare different header that will meet Your needs.
At least this is how I would go about doing something like this so if I'm wrong correct me :)

Xandareva
5th November 2010, 19:33
yes, I am sure because I tested it. OMG. I want only good code example.. Only this.