Skip to content

Instantly share code, notes, and snippets.

@agesome
Created August 19, 2009 17:32
Show Gist options
  • Save agesome/170498 to your computer and use it in GitHub Desktop.
Save agesome/170498 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define BUFSZ 4096
void
die(char *why)
{
printf(why);
exit(EXIT_FAILURE);
}
void
logstr(char *what)
{
printf(what);
}
int
s_connect(char *host, int *sock)
{
struct addrinfo *server, hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
logstr("Creating socket...\n");
if( !(*sock = socket(AF_INET, SOCK_STREAM, 0)) )
die("Socket creation failed.\n");
logstr("Created.\nResolving hostname\n");
if( getaddrinfo(host, "ircd", &hints, &server) )
die("Failed to resolve hostname.\n");
logstr("Resolved.\nConecting...\n");
if( connect(*sock, server->ai_addr, server->ai_addrlen) == -1)
die("Connection to server failed.\n");
logstr("Connected.\n");
}
int
main(void)
{
int sock;
char inbuf[BUFSZ], *obuf;
s_connect("irc.cluenet.org", &sock);
read(sock, inbuf, BUFSZ);
printf(inbuf);
obuf = "NICK weew\n";
write(sock, obuf, strlen(obuf));
memset(inbuf, 0, BUFSZ);
read(sock, inbuf, BUFSZ);
printf(inbuf);
memcpy(inbuf, "PONG", 4);
write(sock, inbuf, strlen(inbuf));
printf(inbuf);
obuf = "USER weew localhost irc.cluenet.org :test\n";
write(sock, obuf, strlen(obuf));
while(1){
memset(inbuf, 0, BUFSZ);
read(sock, inbuf, BUFSZ);
printf(inbuf);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment