LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2009, 12:44 AM   #1
sdas7
LQ Newbie
 
Registered: May 2009
Posts: 2

Rep: Reputation: 0
read and write binary data from socket


Hi ppl,

I am new to network programming. I am trying to read and write binary data from and to a socket. After the corresponding transfers to and from the socket, I try to write the binary data to the file. But my source file and the file written once received from the socket are not the same. The size of both the files are same though. I am doing the file operations using fread and fwrite and the send and recv for the socket calls.......Can anyone please help me out. Thanks in advance
 
Old 05-23-2009, 05:55 AM   #2
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 70
Lets have a look at the code then
 
Old 05-24-2009, 01:09 PM   #3
sdas7
LQ Newbie
 
Registered: May 2009
Posts: 2

Original Poster
Rep: Reputation: 0
the server is uploading the file....

while(sent_bytes < file_size)
{
write_buff=(char*)malloc(file_size);
uint32_t nread = fread(write_buff,1,1024,fp);
strcpy(write_buf,write_buff);
printf("\n%s\n",write_buf);
// writing fromthe file to the socket descriptor temp
uint32_t nwritten = write(temp, write_buf, nread);
sent_bytes += nwritten;
usleep(20000);
memset(write_buf,0,1024);
free(write_buff);
}
if((file_size-sent_bytes==0)){
fclose(fp);
}

the following snippet is for the client
//read from socket desc temp to buffer temp_write
bytes_read = read(temp, temp_write, 1024);
received_bytes+=bytes_read;
if(received_bytes<=file_descriptor[i].file_len){
if(file_descriptor[i].fd==0){
file_descriptor[i].fd++;
//opening the file for writing for first time file_descriptor[i].fp=fopen(extracted_payload_buf,"w"); fwrite(temp_write,1,bytes_read,file_descriptor[i].fp);
fflush(file_descriptor[i].fp);
}
else fwrite(temp_write,1,bytes_read,file_descriptor[i].fp); if(file_descriptor[i].file_len-received_bytes==0){
received_bytes=0; fclose(file_descriptor[i].fp);
file_descriptor[i].fd=0;
close(temp);
remove_conn(i);
}
}
}

I want to make sure if i can use read,write for socket ops and fwrite,fread for file ops. Also is the usage right.
Thanks

Last edited by sdas7; 05-24-2009 at 01:11 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl:How to read data use IO::Socket ? nsfocus Programming 12 07-27-2008 07:57 PM
How to read data with IO::Socket in Perl? nsfocus Programming 1 07-17-2008 07:28 PM
write and read socket lrios Programming 1 05-01-2008 01:19 PM
Read data from a socket allomeen Linux - Networking 1 05-08-2006 03:19 AM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 04:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration