|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 
|
Re: Simple BSD Sockets Problem
Actually, this code #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 6667 // the port client will be connecting to
#define MAXDATASIZE 2000 // max number of bytes we can get at once
int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; // connector's address information
he = gethostbyname("efnet.teleglobe.net");
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("error creating the socket\n");
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
if (connect(sockfd, (struct sockaddr *)&their_addr,
sizeof their_addr) == -1) {
perror("connect\n");
exit(1);
} else {
printf("You got the connection to the server!\n");
fprintf(stderr,"After send, entering recv loop\n");
numbytes=0;
while(numbytes == 0) {
fprintf(stderr,"In recv loop\n");
numbytes=recv(sockfd, buf, sizeof(buf)-1, 0); //-1 b/c you will null terminate
}
fprintf(stderr,"After recv loop, %d bytes read.\n",numbytes);
buf[numbytes] = '\0';
fprintf(stderr,"After null term, %d bytes read.\n",numbytes);
printf("Received: %s",buf);
fflush(stdout);
close(sockfd);
}
return 0;
} makes my computer connect to the server, and then receives a message saying "NOTICE AUTH  ** Processing connection to efnet.teleglobe.net"
That's all. However, I believe I was supposed to see more than this. Perhaps it's the problem that Game_Ended noticed about line endings.
|