Skip to content

Instantly share code, notes, and snippets.

@tjhv
Created February 17, 2018 05:34
Show Gist options
  • Save tjhv/e8914d549e2c7ef125bfb82d8562f372 to your computer and use it in GitHub Desktop.
Save tjhv/e8914d549e2c7ef125bfb82d8562f372 to your computer and use it in GitHub Desktop.
Old snippet from the beginning.
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define DSTADDR "darklite.be.eu.SwiftIRC.net"
#define DSTPORT 6667
#define BUFF_SIZE 512
int main(int argc, char * argv[])
{
struct hostent * canonical;
canonical = gethostbyname(DSTADDR);
if (!canonical)
{
printf("Unknown host: %s\r\n", DSTADDR);
return 1;
}
int fd;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
{
printf("Error creating socket.\r\n");
return 1;
}
struct sockaddr_in client;
memset(&client, 0, sizeof client);
client.sin_family = AF_INET;
client.sin_port = htons(DSTPORT);
memcpy(&client.sin_addr, canonical->h_addr, canonical->h_length);
int eid;
eid = connect(fd, (struct sockaddr *) &client, sizeof client);
if (eid < 0)
{
printf("Error connecting socket.\r\n");
return 1;
}
char buff[BUFF_SIZE];
memset(buff, 0, sizeof buff);
sprintf(buff, "NICK %s\r\n", "H2O");
send(fd, buff, strlen(buff), 0);
memset(buff, 0, sizeof buff);
sprintf(buff, "USER %s \"\" \"\" :%s\r\n", "svs", "c");
send(fd, buff, strlen(buff), 0);
memset(buff, 0, sizeof buff);
char pong[BUFF_SIZE];
while (recv(fd, buff, sizeof buff, 0) > 0)
{
if (sscanf(buff, "PING :%s", pong) > 0)
{
sprintf(buff, "PONG :%s\r\n", pong);
send(fd, buff, strlen(buff), 0);
}
printf("-> %s\r\n", buff);
memset(buff, 0, sizeof buff);
}
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment