You know, just a few lines of code that can open a file, then write that file to the open socket (newsockfd in my case)...plz? Help?
You know, just a few lines of code that can open a file, then write that file to the open socket (newsockfd in my case)...plz? Help?
If using WIN32 API file and memory functions
Code:hFile=CreateFile(sFileName,GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL); //open the file //error check iSize=GetFileSize(hFile);//find its size pFile=GlobalAlloc(GHND,iSize+1); //alloc buffer for whole file and terminator //error check sBuffer=(char *)GlobalLock(pFile);//lock the pointer down iReturn=ReadFile(hFile,sBuffer,iSize,&dwRead,NULL);//read whole file //error check send(newsockfd,sBuffer,iSize,dwFlags); //error check GlobalUnlock(pFile); pFile=GlobalFree(pFile); //error check
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill
I plan on running this on unix. Is there any way you could rewrite it so it used stdio.h in unix/linux instead of a win32 api?
I'll show you mine if you show me yours.
In otherwords, not without seeing that YOU tried and failed.
I'm not here to write your code, unless you want to pay me (as the rest of my customers do).
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill
well, here is my crappy code.
int ch, i = 0;
while((ch = getchar()) != EOF)
i ++;
write(newsockfd,ch,1)
I am not sure if it works (don't have access to my unix comp right now), but the problem is that it writes every character one at a time to the socket.
ack!
the right one:
<code>
int ch, i = 0;
while((ch = getch(stdin)) != EOF)
i ++;
write(newsockfd,ch,1)
</code>
I feel like a retard. Glad I am unregistered.
int ch, i = 0;
while((ch = getc(stdin)) != EOF)
i ++;
write(newsockfd,ch,1)
Here I go again...Originally posted by Unregistered
I feel like a retard. Glad I am unregistered.
int ch, i = 0;
while((ch = getc(stdin)) != EOF)
i ++;
write(newsockfd,ch,1)
What exactly is this supposed to do? All your code does is read from stdin until you enter EOF (ctrl Z or D, depending, typicaly), and then it writes one character to your socket.
You can't mean for this to happen. Why do you even have 'i' in your code example at all?
Might I recommend Beej?
Quzah.
Hope is the first step on the road to disappointment.
In UNIX, everything is a file, from your directory to your mouse. If you can write to a file on your file system, you could as well write to an open socket, the very same way, the only criteria is that the socket be opened and initialized properly.
Also, as quzah said, check you code. One more thing, your parameter 'ch' to write() is not right, you should be passing the address of 'ch', not 'ch' itself:
ORCode:while((ch = getc(stdin)) != EOF) /* i ++; */ write(newsockfd, &ch, 1);
Code:while((ch = getc(stdin)) != EOF) { i ++; write(newsockfd, &ch, 1); }
Do not write the file byte by byte to the socket.
Each write is a message to the OS on the recv end, each will have to have a packet header ect, further increasing the overhead. You will probably create more WOULDBLOCK's this way. (the socket is busy/full and the info will need to be buffered and sent later, by your app not by the OS)
Use setsockopt() with the SO_SNDBUF flag (there must be a 'NIX version) to make sure you have a decent buffer size (I use 32K) and then read the file in buffer sized increments and write to the socket.
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill