![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 09:10 AM |
| GNU Screen: Send to Multiple Screens at Once? | deckard | UNIX for Advanced & Expert Users | 3 | 02-05-2008 07:26 AM |
| to send email to multiple users | vishwas.shenoy | UNIX for Dummies Questions & Answers | 0 | 01-25-2008 03:00 AM |
| Need to send multiple files during ftp | cdunavent | Shell Programming and Scripting | 2 | 06-17-2005 05:23 AM |
| How to send mail to multiple users ??? | arunava_maity | UNIX for Dummies Questions & Answers | 3 | 10-16-2002 07:07 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#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 08:21 AM. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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;
}
|
|||
| Google The UNIX and Linux Forums |