The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Looking for an X11 Utility - Send Keystrokes to Multiple Clients Alon.Albert UNIX for Dummies Questions & Answers 5 07-08-2008 01:10 PM
GNU Screen: Send to Multiple Screens at Once? deckard UNIX for Advanced & Expert Users 3 02-05-2008 10:26 AM
to send email to multiple users vishwas.shenoy UNIX for Dummies Questions & Answers 0 01-25-2008 06:00 AM
Need to send multiple files during ftp cdunavent Shell Programming and Scripting 2 06-17-2005 09:23 AM
How to send mail to multiple users ??? arunava_maity UNIX for Dummies Questions & Answers 3 10-16-2002 11:07 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-30-2007
seanword seanword is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 1
Multiple send()'s

Hi guys I'm quite new to socket programming. I'm using Beej's guide.

I have a program that requests a html page and downloads it one character at a time stopping when it reaches an EOF. I do this with the send() and recv() functions. When I'm finished I use send() again to get another page but I instantly get a EOF back. Both pages are working.

I'm not sure what i'm missing and would appreciate if any one could help. Thanks in advance.

Edit Figured it now was using HTTP 1.0!

Last edited by seanword; 12-30-2007 at 11:21 AM..
  #2 (permalink)  
Old 12-30-2007
ramen_noodle ramen_noodle is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 251
If you are using send and recv I don't see the point in testing for eof or reading data one character at a time. send and recv have perefectly reasonable ways to deal with data streams without the stdio constructs. The probable cause for your second page eof failure is a logic problem in testing for eof and/or that the recv hasn't read any data and the old data is still stored in memory. Don't know because you haven't posted sample code.

Here's a hacked up echo client with eof inclusions for an example of send and recv..it may help.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>

#define BSZ 25
#define EPORT 7

int makeConnect(char *);
void chomp(char *);
int iseofhere(char *);
void addeof(char *);


int main(int argc , char **argv) {
int s, err, p = 0;
char sbuf[BSZ];
char rbuf[BSZ];

                     if (argc != 2) {printf("Please provide a host ip address for connect().\n"); return 1;}
                     srand(time(NULL));
                     if ( (s = makeConnect(argv[1])) <= 0) {
                          perror("makeconnect()");
                          return 1;
                     }

                     bzero(sbuf,BSZ);
                     while (1) {
                               printf("\nSend string through descriptor %d: ",s);
                               fgets(sbuf,BSZ,stdin);
                               if (strcmp(sbuf,"quit\n") == 0) {close(s); break;}
                               if ((p++) % 3 == 0) {addeof(sbuf);}
                               if ( (err = send(s,sbuf,BSZ,0)) <= 0) {perror("send()"); continue;}
                               if ( (err = recv(s,rbuf,BSZ,0)) <= 0) {perror("recv()"); continue;}
                               if (iseofhere(sbuf) || iseofhere(rbuf)) {printf("Detected eof.\n");}
                               chomp(sbuf); chomp(rbuf);
                               printf("Sent buffer %s and received buffer %s\n",sbuf,rbuf);
                     }
return 0;
}               

void addeof(char *dta) {
int p = strlen(dta);
                        dta[(int)(1 + rand() %  p)] = EOF;
}

int iseofhere(char *dta) {
                         for ( ; *dta != '\0' ; dta++) {
                             if (*dta == EOF) {return 1;}
                          }
                          return 0;
}  

void chomp(char *dta) {

                      for ( ; *dta != '\n' && *dta != '\0'; dta++) {;}
                      *dta = '\0';
}

                           
int makeConnect(char *addr) {
int sck = -1;
in_addr_t valid_add;
struct sockaddr_in info;

                             bzero(&info,sizeof(info));
                             if ( (valid_add = inet_addr(addr)) < 0) {return sck;}
                             if ( (sck = socket(PF_INET,SOCK_STREAM,0)) < 0) {return sck;}

                             info.sin_port = htons(EPORT);
                             info.sin_addr.s_addr = valid_add;
                             info.sin_family = PF_INET;
                            
                             if (connect(sck,(struct sockaddr *)&info,sizeof(info)) < 0) {
                                 close(sck);
                                 return -1;
                             }
                             return sck;
}

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0