![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#12 |
|
Newbie
Join Date: Apr 2006
Posts: 10
Rep Power: 0
![]() |
yes, a return character('/r') it isn`t working
|
|
|
|
|
|
#13 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Why do you want to send a carriage return character?
By the way, it's '\r', not '/r'. Maybe you want: Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#14 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
I've put something together for you, I hadn't really worked with sockets before so wanted to see if it was easy. Conclusion is: yes it is.
The *nix version is almost the same, but I could post it if you're on Linux, Unix, Solaris or *BSD. If you don't want to do it all yourself you could also take a IRC wrapper from internet. #include <winsock.h>
#include <iostream>
using namespace std;
string ircnick;
SOCKET ircsocket;
bool connected = false;
FILE * socketstream;
int start(char* server, int port, char* nick, char* user, char* name, char* pass)
{
HOSTENT* irchost;
sockaddr_in remserv;
if(connected)
return 1;
ircsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(ircsocket == INVALID_SOCKET)
{
perror("Socket()");
return 1;
}
irchost = gethostbyname(server);
if(!irchost)
{
closesocket(ircsocket);
return 1;
}
remserv.sin_family = AF_INET;
remserv.sin_port=htons(port);
memcpy(&remserv.sin_addr, irchost->h_addr, 4);
if(connect(ircsocket, (struct sockaddr*)&remserv, sizeof(remserv)) == SOCKET_ERROR)
{
cerr << "Connect():" << WSAGetLastError() << endl;
closesocket(ircsocket);
return 1;
}
socketstream = fdopen(ircsocket, "w");
if(!socketstream)
{
printf("Failed to open streams!\n");
closesocket(ircsocket);
return 1;
}
connected = true;
ircnick = nick;
fprintf(socketstream, "PASS %s\r\n", pass);
fprintf(socketstream, "NICK %s\r\n", nick);
fprintf(socketstream, "USER %s * 0 :%s\r\n", user, name);
fflush(socketstream);
return 0;
}
int msgloop()
{
char buff[2048];
if (!connected)
{
cout << "Not connected!" << endl;
return 1;
}
while(1)
{
int len = recv(ircsocket, buff, 2047, 0);
if (len == SOCKET_ERROR || !len)
{
return 1;
}
buff[len] = '\0';
char * msg = buff, * p;
while (p = strstr(msg, "\r\n"))
{
*p = '\0';
cout << msg << endl;
msg = p + 2;
}
}
return 0;
}
int quit(string message = "Leaving")
{
if(connected)
{
fprintf(socketstream, "QUIT %s\r\n", message.c_str());
if(fflush(socketstream))
return 1;
}
return 0;
}
void disconnect()
{
if(connected)
{
fclose(socketstream);
cout << "Disconnected" << endl;
connected = false;
quit();
closesocket(ircsocket);
}
}
int main(int argc, char ** argv)
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
{
cout << "WSAStartup failed." << endl;
exit(1);
}
start("194.68.45.50", 6666, "superduperbotty", "superduperbotty", "Super Bot", "");
msgloop();
WSACleanup();
return 0;
}P.S. I _REALLY_ mixed up C with C++, you'll have to fix that yourself, this was just a small test.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|