LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2008, 06:51 PM   #1
thefountainhead100
LQ Newbie
 
Registered: Mar 2008
Posts: 27

Rep: Reputation: 15
Unhappy Socket programming simple question:


hi

I am a newbie to socket programming. I am trying to transfer a file from one computer to other using TCP sockets.... I have explained my problem below.

there are two programs, talker and listener. The talker side establishes the connection (has connect() function) and listener had bind(), listen() and accept() functions.

I am transmitting a file from talker to listener. I run the listener first and then the talker.

My problem is, how to make the listener detect that all the contents of the file have been received. The listener is running first. It receives the file when sent and then continues to listen rather than ending the program.

Breifly, the problem is "how to break from this loop? "..right now, I use
while (totalbytes < BYTESTORECEIVE)
{
numbytes = recv ( sockfd, buf, MAXBUFLEN, 0); // buf is char*
fputs(buf,outfile);
totalbytes += numbytes;
}

but I WOULD NOT know the BYTESTORECEIVE always. I tried while(buf == EOF) and (numbytes != 0) but they still remain in loop. One can use EOF with character but with a string ?

What should I do.

Thanks for your time.

Ash
 
Old 03-13-2008, 07:54 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Does the connection have to stay open after the transfer is complete? If not, just close the connection with shutdown. If it needs to stay open, you will probably have to send some sort of header info before sending the file to denote its size.
ta0kira
 
Old 03-14-2008, 04:00 AM   #3
dimitriw1972
LQ Newbie
 
Registered: Dec 2007
Posts: 5

Rep: Reputation: 0
You'll need to define some sort of protocol language suited for the data you send across the link. You could either define a header containing among others the size of the data that will be sent (ta0kira's suggestion), or send some kind of 'end magic' when you're done.

Hope this helps.
 
  


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
Simple Java Socket Question Hady Programming 1 01-08-2007 02:37 AM
Socket programming question brianvdc Programming 2 12-30-2004 01:36 PM
Socket Programming Question? sharma_deepak83 Programming 1 06-29-2004 10:36 AM
UDP socket programming question. niac Programming 6 03-02-2003 02:23 PM
socket programming question.. concoran Programming 4 01-14-2002 02:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:20 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