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.
send a file through raw socket using C programming
Hi all,
I am currently doing a research on video transmission over wireless LAN. I tend to transmit my offline file (xx.svc) from server to client.It may sound stupid (since I have a very little knowledge about c programming and raw socket), but my biggest challenges is that when I want to write the file to the buffer, how actually to define/include the file at the programming coding? where I need to locate the file? Is it at the same folder with my c programming, or somewhere in the linux\include folder?
Can anyone just give a simple example on how to include a file and write it into a buffer before send it through raw socket.
This link http://www.linuxhowtos.org/C_C++/socket.htm provides a good intro to sockets in Linux. provides source code to send/receive a message. So you can make changes to fit to your use. Hope this helps.
Can anyone just give a simple example on how to include a file and write it into a buffer before send it through raw socket.
Another good resource for socket programming is the beej's tutorial
For the file "problem", do you really think that you need to know the file path when coding your app? If so, how a text editor (for example) could work ?!?
You need to open your file given its path with "fopen"; read it chunk by chunk with "fread", and send each chunk of data through you socket. Close the file once done with "fclose". See manuals for more details (in a console, "man fopen", "man fread"...
But, especially if you're a beginner, don't reinvent the wheel, take a look to project like "ortp", "videolan server" etc...
thanks Yaami and Argentino for your help. I have gone through the link provided and also beej`s guideline which have become my primary reference since before. However, my question is that, when we want to read/open the specific file,let say xx.264,I could not see any link to specific folder or define to a specific file in order to read/open the file. For an example;
int SendRawPacket(int rawsock, unsigned char *pkt, int pkt_len)
{
int sent= 0;
/* A simple write on the socket ..thats all it takes ! */
if((sent = write(rawsock, pkt, pkt_len)) != pkt_len)
{
/* Error */
printf("Could only send %d bytes of packet of length %d\n", sent, pkt_len);
return 0;
}
return 1;
}
As the pkt is the pointer, where does this pointer refers to?
... I could not see any link to specific folder or define to a specific file in order to read/open the file.
Your server and your client have to define a communications protocol. For example, the client could send a message requesting a list of all available files, and then the server would respond with this list. Right there you have two types of messages; these typically are referred to as being part of the IPC, or Interprocess Communication set of messages. Read more here: http://en.wikipedia.org/wiki/Inter-p..._communication
Anyhow, basically what I am attempting to state is that your server and client need to establish some form of protocol in which they exchange information. No matter how you look at it, the information (data) needs to be treated indifferently. In other words, whether it is a text request or data from a file, your applications will send and receive the data in the same manner. It is what you do with the data that matters.
So, here is a scenario:
1. Server is running and awaiting client connections
2. Client is running and establishes connection with server
3. Client sends a request for a listing of all available files (of interest) on the server
4. Server responds by sending a list of file names (including the path)
5. Client sends a request to download a particular file
6. Server receives request
7. Server proceeds to open the file, read it in chunks, and send the data to the client
8. Client receives data and writes it to a file (it already knows the filename from Step 5).
For all of this to transpire, you will need to implement a series of functions on both the server and the client that will handle each appropriate step listed above.
C library functions that come to mind that you will require for the Server:
socket()
listen()
bind()
accept()
recv()
send()
opendir()
readdir()
closedir()
fopen()
fread()
fclose()
For the client:
socket()
connect()
recv()
send()
fopen()
fwrite()
fclose()
As for getting assistance with the implementation of the Server and Client, this is not a forum for requesting such. If you cannot accomplish the task yourself, then consult with an experienced software developer, who may or may not charge a fee for their services. If you have software written, that is not working for whatever reason, then feel free to post your code, so that I or someone else can critique it, and help you get over the hurdle that is preventing you from resolving your task.
P.S. Often times people refer to the Berkeley Sockets as "Raw" sockets. These terms are not the same. A raw socket is one in which the Server and/or the Client will take on the onerous task of piecing together each individual component of the TCP or UDP packets, which includes the header, checksums, data, etc. It is very tedious to do, and more importantly it requires root-privileges to open such a socket. For your task, you do not require these raw sockets; a regular socket will do. For example:
Code:
int sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
A raw socket would be opened as:
Code:
int sd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
Last edited by dwhitney67; 07-25-2010 at 09:31 AM.
i'm having one annoying trouble doing this client-server file transfer...
i'm using fopen on binary mode...send() and recv() functions...but there's one problem:
i can send images, .pdf's...etc. But i can't send any text files...i mean, i can send them, but the receiver can't open them because the OS doesn't recognize the file as text (character codifying)
i'm having one annoying trouble doing this client-server file transfer...
i'm using fopen on binary mode...send() and recv() functions...but there's one problem:
i can send images, .pdf's...etc. But i can't send any text files...i mean, i can send them, but the receiver can't open them because the OS doesn't recognize the file as text (character codifying)
any solutions?
You might be doing something odd in your code that is preventing to from transferring the data correctly. Can you please check the md5sums of both the source file and the destination file to ensure they are the same? If the sums are not the same, then you definitely have a source code problem.
So just convert the file into a readable characters encoding with "iconv" command, available in LINUX.
hi,
but i can't do that...i have to guarantee that the receiver gets all the files simple and clean...
what's the right size of chunks to send over "send" function? because i guess if i send a chunk much bigger than the file, there will be garbage on buffer...and the file gets unreadable?!!
... because i guess if i send a chunk much bigger than the file, there will be garbage on buffer...and the file gets unreadable?!!
Duh! If you are sending more data than you read from a file, then you need to sort that issue out. Send only the number of bytes read from the file. What language are you programming in? In C, the read() function returns the number of bytes successfully read (or an error, should one occur).
Duh! If you are sending more data than you read from a file, then you need to sort that issue out. Send only the number of bytes read from the file. What language are you programming in? In C, the read() function returns the number of bytes successfully read (or an error, should one occur).
the "md5sum" command returned different sums in fact:
client:
a3f74fbefb7ed7f149a025874270feae server.txt
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.