SliderMan
May 25th, 2008, 05:30 AM
I started to mess with sockets today.. I tried to make a console IRC Client.
I used wpe pro to sniff some packets and use them in my source.. But I dont know why I couldnt connect anymore.
What is so wierd is that in the first time of running the EXE it worked. and i got connected. Since then it wont work.. here is my source thats would be great, if someone could look at my source and tell me what im doing wrong. since im just trying to learn.
Btw I used telnet to check my "packets syntax", it worked great.
#include <stdio.h>
#include <tchar.h>
#include "winsock2.h"
void main(int argc, char* argv[]) {
//----------------------
// Initialize Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
printf("Error at WSAStartup()\n");
//----------------------
// Create a SOCKET for connecting to server
SOCKET ConnectSocket;
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return;
}
//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "86.58.165.2" );
clientService.sin_port = htons( 6667 );
//----------------------
// Connect to server.
if ( connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return;
}
printf("Connected");
char buf[50];
char buf2[255];
scanf(buf,"%s");
int send1, send2, send3, send4;
recv(ConnectSocket, buf2, strlen(buf2), 0);
printf("%s", buf2);
char sendbuf1[] = "NICK something", sendbuf2[] = "USER moshe moshe.moshe irc.rizon.net :SliderMan....",
sendbuf3[] = "USERHOST SliderMan.......!", sendbuf4[] = "PRIVMSG SliderMan moshe";
send1 = send(ConnectSocket, sendbuf1, strlen(sendbuf1), 0);
send2 = send(ConnectSocket, sendbuf2, strlen(sendbuf2), 0);
send3 = send(ConnectSocket, sendbuf3, strlen(sendbuf3), 0);
send4 = send(ConnectSocket, sendbuf4, strlen(sendbuf4), 0);
printf("\n%i %i %i %i\n", send1, send2, send3, send4);
printf("%i\n", WSAGetLastError());
while(ConnectSocket != SOCKET_ERROR) {
for(int i=0; i<=50; i++) {
buf2[i] = NULL;
}
recv(ConnectSocket, buf2, strlen(buf2), 0);
printf("%s", buf2);
}
WSACleanup();
return;
}
Most of the code here is taken from MSDN.
Another question is when I used free open source irc client, the output was visable in the console (like send and recive). So my question is how can i hook it so the output will come to my console without using printf function each time?
Thanks in advance!
I used wpe pro to sniff some packets and use them in my source.. But I dont know why I couldnt connect anymore.
What is so wierd is that in the first time of running the EXE it worked. and i got connected. Since then it wont work.. here is my source thats would be great, if someone could look at my source and tell me what im doing wrong. since im just trying to learn.
Btw I used telnet to check my "packets syntax", it worked great.
#include <stdio.h>
#include <tchar.h>
#include "winsock2.h"
void main(int argc, char* argv[]) {
//----------------------
// Initialize Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
printf("Error at WSAStartup()\n");
//----------------------
// Create a SOCKET for connecting to server
SOCKET ConnectSocket;
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return;
}
//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "86.58.165.2" );
clientService.sin_port = htons( 6667 );
//----------------------
// Connect to server.
if ( connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return;
}
printf("Connected");
char buf[50];
char buf2[255];
scanf(buf,"%s");
int send1, send2, send3, send4;
recv(ConnectSocket, buf2, strlen(buf2), 0);
printf("%s", buf2);
char sendbuf1[] = "NICK something", sendbuf2[] = "USER moshe moshe.moshe irc.rizon.net :SliderMan....",
sendbuf3[] = "USERHOST SliderMan.......!", sendbuf4[] = "PRIVMSG SliderMan moshe";
send1 = send(ConnectSocket, sendbuf1, strlen(sendbuf1), 0);
send2 = send(ConnectSocket, sendbuf2, strlen(sendbuf2), 0);
send3 = send(ConnectSocket, sendbuf3, strlen(sendbuf3), 0);
send4 = send(ConnectSocket, sendbuf4, strlen(sendbuf4), 0);
printf("\n%i %i %i %i\n", send1, send2, send3, send4);
printf("%i\n", WSAGetLastError());
while(ConnectSocket != SOCKET_ERROR) {
for(int i=0; i<=50; i++) {
buf2[i] = NULL;
}
recv(ConnectSocket, buf2, strlen(buf2), 0);
printf("%s", buf2);
}
WSACleanup();
return;
}
Most of the code here is taken from MSDN.
Another question is when I used free open source irc client, the output was visable in the console (like send and recive). So my question is how can i hook it so the output will come to my console without using printf function each time?
Thanks in advance!