send a file through raw socket using C programming
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.
P.S. The code above is sorely missing a lot of error checking.
thank you dwhitney...you were right. in fact the problem was the last piece of chunk.it was always sending 150 bytes, including garbage...
thank you all for the help
Have you considered closing the client (receiver) socket on the "uploader" side? Once the entire file has been sent to the receiver, it would seem like the logical thing to do.
Btw, you should avoid using recv() from a non-blocking socket, unless you either 1) set a maximum timeout period to some sane setting, such as 2-3 seconds, or 2) use select() before attempting to call recv().
When the "uploader" closes the client socket, recv() should fail, but at this late hour, I'm not 100% sure of that. Thus I would recommend that you at least try using select(). I know that select() returns a 0 (zero) when the socket has been closed.
Code:
...
struct timeval timeout = {1, 0}; // one second
while (activityDetected(sock, timeout) != 0)
{
char buffer[1024];
ssize_t bytes = recv(sock, buffer, sizeof(buffer), 0);
if (bytes > 0)
{
fwrite(buffer, sizeof(char), bytes, fp);
}
}
Code:
int activityDetected(int sd, struct timeval timeout)
{
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(sd, &readfds);
int sel = -1;
while (sel < 0)
{
sel = select(sd + 1, &readfds, 0, 0, &timeout);
if (sel > 0)
{
return FD_ISSET(sd, &readfds);
}
if (sel < 0 && errno != EINTR)
{
return 0;
}
}
/* timeout */
return 0;
}
Last edited by dwhitney67; 11-28-2010 at 07:20 PM.
but the thing is that i wan't to receive one last data from the server, after uploading the file...i can't close the socket.
i'm trying to send 0 bytes to the server, so he can break the while cycle, but i can't make that work...
but the thing is that i wan't to receive one last data from the server, after uploading the file...i can't close the socket.
i'm trying to send 0 bytes to the server, so he can break the while cycle, but i can't make that work...
What is it that you want the uploader (server) to send to the client? How will the client know that this is a status message from the server versus some data part of the file? Unless you have set up a particular protocol that each understands, you are going to have trouble with your requirements.
Consider defining a protocol; for example
Code:
enum MsgType
{
REQUEST,
DATA,
DONE
};
struct Message
{
MsgType type;
int status; // 1 = ok, 0 = bad
size_t dataSize; // number of bytes of data
unsigned char* data;
};
Here, you define the type of data that is being flung around. For instance, the client would initially send the uploader (server) a message containing {REQUEST, 1, namesize, <filename>}. Then the server would respond; if the file is found, then one or more {DATA, 1, datasize, data} messages are sent. If the file is not found, or the data transmission has completed, then the server can send a {DONE, status, 0, 0} message, depending on the outcome of the request.
Anyhow, this is merely something to consider. You are free to define your own protocol. Just remember that with structs, you will need to serialize them before sending the data, and then deserialize when receiving it. You can Google for information on these subjects.
Last edited by dwhitney67; 11-29-2010 at 07:36 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.