Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bsd sockets

Status
Not open for further replies.

newbieatassembly

Technical User
Oct 4, 2000
39
IN
Hi, I am trying to write a simple socket - send and reciive function to port 80.

Here is the code :
// Socket to test http port

// let us have some global variables
#include <stdio.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>

#define MAX_BUFFER_SIZE 16*1024

int len, bytes_sent;
char *msg = &quot;GET\n&quot;;
char port[8] = &quot;80&quot;;
struct sockaddr_in server;
struct hostent *h;
struct timeval to;
int socketopen = 1;

int main()
{
int sock_fd, tmp_connect;
fd_set rread, wwrite;
char recv_message[MAX_BUFFER_SIZE];
len = strlen(msg);

sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if(sock_fd < 0) {perror(&quot;socket&quot;); return -1;}

// put in required resources
server.sin_family = AF_INET;
h = gethostbyname(&quot;localhost&quot;);
if(h == 0) return -1;
memcpy(&(server.sin_addr), h->h_addr, h->h_length);
server.sin_port = htons(atoi(port));

// now connect
tmp_connect = connect(sock_fd, (struct sockaddr *)
&server, sizeof(server));
if(tmp_connect < 0) { perror(&quot;connect&quot;);
return -1; }

do {
FD_ZERO(&rread); FD_ZERO(&wwrite);
FD_ISSET(sock_fd, &rread);
FD_ISSET(sock_fd, &wwrite);
memset((char *)&to, 0, sizeof(to));
to.tv_sec = 5;
int sw = select(sock_fd + 1, (fd_set *)0,
&wwrite, (fd_set *)0, &to);
if(sw < 0) { perror(&quot;select&quot;); return -1;}
if(sw) {
// data can be written now
send(sock_fd, msg, len, 0);
}

int sr = select(sock_fd + 1, &rread,
(fd_set *)0,(fd_set *)0, &to);
if(sr < 0) { perror(&quot;select read&quot;);
return -1; }
if(sr) {
// data can be read now
int rval = read(sock_fd,
recv_message, 16000);
if(rval < 0) { perror(&quot;read&quot;);
return -1; }
printf(&quot;\n%s\n&quot;, recv_message);
socketopen = 0;
}
}while(socketopen == 1);

close(sock_fd) ;

return 0;
}

It seems to sending nothing, strace seems to have a sendto(something,something MSB_OOB);

I think something is wrong with the code. I know this code looks like C, so if anyone is offended by this post I am sorry.




 
Well hmm I got it - it should have been FD_SET and not FD_ISSET.

Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top