All pastes #241960 Raw Edit

Anonymous

public text v1 · immutable
·published 2006-11-06 20:22 UTC
rendered paste body
#include <stdio.h>
#include <bncsutil/bncsutil.h>

#include <pthread.h>
#include <unistd.h>

#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>

using namespace std;

void* connectbot(void*)
{
#define INET_ADDR "irc.tehnet.org"
#define INET_PORT 6667

int sockfd, sockerr, bytesRecv;
sockaddr_in addr_remote;
struct hostent *he;

sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

he = gethostbyname(INET_ADDR);
if (he == NULL) {
herror("gethostbyname error");
exit(1);
}
addr_remote.sin_family = AF_INET;
addr_remote.sin_addr = *((struct in_addr *)he->h_addr);
addr_remote.sin_port = htons(INET_PORT);

printf("%s resolved to %s\n", INET_ADDR, inet_ntoa(addr_remote.sin_addr));
printf("Connecting to %s on port %d\n", inet_ntoa(addr_remote.sin_addr), INET_PORT);
sockerr = connect(sockfd, (struct sockaddr *)&addr_remote, sizeof(addr_remote));
if (sockerr == -1) {
perror("connect() error");
close(sockfd);
exit(1);
} else {
printf("Connected, recieving data.\n");
}

char buffer[1024];
while (bytesRecv != -1) {
bytesRecv = recv(sockfd, buffer, sizeof(buffer), 0);
if(bytesRecv == 0) {
printf("Socket disconnected\n");
close(sockfd);
exit(1);
}
buffer[bytesRecv] = 0;
printf("Recv(%d): %s", bytesRecv, buffer);
}
}

int main ()
{
int thread1int;
pthread_t thread1;
thread1int = pthread_create(&thread1, NULL, connectbot, 0);
pthread_join(thread1, NULL);
return 0;
//char *bversion;
//bncsutil_getVersionString(bversion);
}