LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-11-2004, 07:23 AM   #1
biiiep
Member
 
Registered: Aug 2003
Posts: 39

Rep: Reputation: 15
filetransfer with socket and tcp


hallo,

i have a little problem with a small server/client program. I have to do it for studies.

the programm should be able to transfer files from server to client. i've tried it in the way below, but it has a bug. When i send the file my client recieves only the first line of the file. But i don't know where the problem is.

here is the important part of the server code

while(1)
{
/* for (i = 0; i < MAXLENGTH; i++)
buf[i]=0;*/

fd2 = accept(fd,(struct sockaddr *)&client, &sin_size);

if(fd2 == -1)
{
printf("accept() errorn");
exit(-1);
}

printf("You got a connection from %s \n",inet_ntoa(client.sin_addr) );
send(fd2,"Welcome to my server\n",22,0); /* send to the client welcome message */

numbytes = recv(fd2, buf, MAXLENGTH, 0);

if (numbytes > 0)
printf("recieved Filename: %s\n", buf);
else
perror("data error");

filetocp = fopen((const char*) buf, "r");
if (filetocp == NULL)
{
perror("opening file failed");
close(fd2);
continue;
}
i=0;
while (fgets(buf, MAXLENGTH, filetocp) != NULL)
{
i++;
printf("%s\n", buf);
printf("sending...\n");
send(fd2,buf,strlen(buf)+1,0); /* sending file */
}
printf("%d\n", i);

printf("sending finished\n");
fclose(filetocp);
close(fd2); /* close fd2 */
}

and now the client code

if ((numbytes=recv(fd,buf,MAXLENGTH,0)) == -1)
{
perror("recv() error");
exit(-1);
}
printf("%s\n", buf);


numbytes = send(fd,filename,strlen(filename)+1,0); /* send to the client welcome message */
if (numbytes == 0)
printf("Error");
else
printf("%d\n", numbytes);

if (numbytes == -1)
{
perror("file recieving failed, perhaps it doesn't exist ");
exit (-1);
}


filetorv = fopen((const char*) filename, "w");
perror("oeffne file");
i=0;
while(recv(fd, buf, MAXLENGTH, 0) > 0)
{
i++;
printf("%d\n", i);
printf("empfange...\n");
fputs(buf, filetorv);
}

if (filetorv != NULL)
{
fclose(filetorv);
perror("schliesse Datei: ");
}

exit(1);


perhaps anybody can help me...

thanks
 
Old 06-11-2004, 09:24 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Since this is homework and you need to learn this stuff here is a primaer - if you look in section 3, it discusses what you want to know - socket streams
 
Old 06-11-2004, 12:33 PM   #3
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
USE CODE TAGS
 
  


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
socket pogramming(TCP) hubabuba Programming 4 10-24-2005 11:14 PM
TCP/IP book for socket programming hubabuba Programming 3 10-17-2005 03:46 AM
Linux TCP Socket swa_dur Linux - Networking 3 09-23-2005 11:03 AM
socket buffer TCP lucs Slackware 0 05-05-2005 07:26 AM
tcp socket monitoring xuttux Linux - Networking 1 05-19-2004 08:49 AM

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

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