As you may remember from an older thread, I am diving into network connections, using the BSD sockets. My ultimate goal is to make a very basic IRC client.
However, I am stuck.
I have created a commend line tool in C and I have the following code:
The server I put was a server running on IRC's EFNet network. I was supposed to receive a message, from the server, right? Not only this doesn't happen, my program also crashes without showing anything! (the debugger comes up with no explanation).
Any ideas why? Actually, I must have made a newbie mistake due to my ignorance. Please enlighten me.
However, I am stuck.
I have created a commend line tool in C and I have the following code:
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 1000 // 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");
char p[] = "join #macfilez";
send(sockfd, p, sizeof(p), 0);
recv(sockfd, buf, sizeof(buf), 0);
buf[numbytes] = '\0';
printf("Received: %s",buf);
close(sockfd);
return 0;
}
Any ideas why? Actually, I must have made a newbie mistake due to my ignorance. Please enlighten me.