简体   繁体   中英

exchange files between clients via socket

How I can send file from one client (A) to another one (B) via socket? and vice versa, send file from B to A. I mean that make the client sender and receiver at the same time. In other word, when muticlient connect to server, how I distinguish between clients ?

It is good way to manage client using their id(ie a unique long or string or any other for each user). At the time of connection to socket client send their id , store that is in collection. And when a user(Client) want to send file send with own id and Id of that user(Client) want to send.

You need to implement you own communication message format in short a simple protocol .

You keep a list of all active sockets in a shared list/map , and based on the request from the message you pick up the apt client and push the desired message to that.

You can implement the actual message format as you want, but this can be the blueprint. In this case lets say your client A sends message : 1. Client Id 2. File Start 3 X . File Content 4. File End

as soon as you get a connection you get the target client id , the file start message lets you understand the next message just needs to be diverted to target and file End message defines the transfer complete.

Also, you may would like to send Acknowledgement message from server to client, in order to eradicate transfer issues.

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