[SOLVED] File contents printing with the filename when sending a string from client to server
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
File contents printing with the filename when sending a string from client to server
I am trying to establish a connection between client and server to send a file to the server from a client. I have been successfully able to send files to the server but i am facing a problem with the the filename whenever i try to send any string to the server and use it in naming the filename at the server side, the string is successfully concatenated but it saves the filecontents in the filename. I have been pulling my hair out for this problem but couldn't reach a solution
for example:
i am sending a file hello1.txt to server and the server has to save it as abcxyz.txt as i am sending the "xyz" from the client. BUT Whenever i am doing this ,the file saves as abcxyzfilecontents.txt
If i saved in the .txt file "you123" ,my file at server side would save as abcxyzyou123.txt
Here are my codes:
Know that the server code implements a multi threaded server. The functionality to be discussed is defined in myfunc
server.c:
If you use the code tags i.e. [code][/code] when posting code it will be much easier to read.
When sending data across a network you need to establish a protocol. The receiving socket has no idea you've made two separate writes. You need to think of a way to delineate your data.
If you read some network protocols you'll notice they provide a means to separate the distinct parts of the protocol. If the protocol is textual it needs to be something that won't appear in the message portion. HTTP headers are separated from each other by a new line and the headers are separated from the body by a new line without any other characters on the line.
An email can have newlines in the body so to signal an end to the body a period is sent on a new line followed by a new line. Since the message could legitimately have a period and newline as it's last line the protocol has a means of handling that as well (sorry, I don't remember what it is at the moment).
So, if you are going to send a filename and file contents you need a means for the receiver to interpret what it is receiving. i.e. The filename will be sent first followed by a newline; then the size of the file will be sent, followed by a new line; then the file contents will be sent.
Thanks a lot! That was the main issue. The server wasn't knowing where do the file contents start and when to stop naming the file. So i figured out a solution. I sent a random integer and i waited at the client side for that integer before the client starts sending the file. At the server side i sent the integer after i had finished naming the file. Now i have stumbled upon another issue, suppose i have multiple clients connected to the server at a time,and each connected client is assigned a request number for instance 1,2,3,4 etc. I want such a functionality that When server finishes serving client # 1 and it wants to inform all other connected clients that i have finished serving client # 1 and consequently updates them on their request numbers ,for example the client which was assigned req # 2 is allotted req # 1, the client which was assigned req # 3 is re-assigned req # 2 .How do i achieve this?
ad1: Sending the length of the filename would be better than sending a random integer...
ad2: In the common client/server architecture clients aren't interested in each other's activities, they only deal with the server. Of course the server mush have connection-specific data-areas to keep track with clients. Also you have to learn about non-blocking mode and functions select/poll.
Thanks a lot! That was the main issue. The server wasn't knowing where do the file contents start and when to stop naming the file. So i figured out a solution. I sent a random integer and i waited at the client side for that integer before the client starts sending the file. At the server side i sent the integer after i had finished naming the file. Now i have stumbled upon another issue, suppose i have multiple clients connected to the server at a time,and each connected client is assigned a request number for instance 1,2,3,4 etc. I want such a functionality that When server finishes serving client # 1 and it wants to inform all other connected clients that i have finished serving client # 1 and consequently updates them on their request numbers ,for example the client which was assigned req # 2 is allotted req # 1, the client which was assigned req # 3 is re-assigned req # 2 .How do i achieve this?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.