Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 19th, 2006, 3:46 AM   #11
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by tamer1221
i mean send return to the server
A return character?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 19th, 2006, 4:07 AM   #12
tamer1221
Newbie
 
Join Date: Apr 2006
Posts: 10
Rep Power: 0 tamer1221 is on a distinguished road
yes, a return character('/r') it isn`t working
tamer1221 is offline   Reply With Quote
Old Apr 19th, 2006, 5:35 AM   #13
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Why do you want to send a carriage return character?

By the way, it's '\r', not '/r'.

Maybe you want:
Quote:
IRC messages are always lines of characters terminated with a CR-LF (Carriage Return - Line Feed) pair, and these messages shall not exceed 512 characters in length, counting all characters including the trailing CR-LF. Thus, there are 510 characters maximum allowed for the command and its parameters. There is no provision for continuation message lines. See section 7 for more details about current implementations.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 19th, 2006, 10:52 AM   #14
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC