Thread: Opening Connections With IRC Servers/Networks

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    9

    Opening Connections With IRC Servers/Networks

    Hi. I'm working on an application to open a connection to an IRC server/network that will help administrators manage the daily tasks and hassles. First, I need help with opening the connection and replying to the ping request sent by the server/network. Please help, thanks

    (By the way.. I'm using Dev-C++ compiler)
    Last edited by KuTsuM; 06-05-2005 at 06:32 PM.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    What operating system are you using? You're going to need to learn to use sockets, and that's a very system-dependent issue.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    Thanks for the reply, and i'm using Windows XP.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You'll want to have a look at Winsock 2, and if you haven't already, learn any protocols associated with IRC - personally I have no experience in that particular area.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    read the IRC rfc

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    I've already read the rfc1459 and everything, i just need to learn how to use sockets and I haven't found any good tutorials yet.. If you know one please tell me

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Search the networking board - there've been tonnes of suggestions made. Beej's Guide is a very famous and commonly used one - it just focuses on UNIX sockets a bit more than winsock - but if you read that and learn the basic principles, then go and look for a winsock tutorial you're all set. I used, "Johnnies Winsock Tutorial" which is usually the first google result for, "winsock tutorial".

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    Thanks for your help. I've learned quite a bit about sockets but based off that winsock tutorial I have been trying to test a client out, but I'm getting an error and I was wondering if any of you can help tell me whats wrong. Here is the area of the code where the problem is occuring:

    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>
    
    #define NETWORK_ERROR -1
    #define NETWORK_OK     0
    
    void ReportError(int, const char *);
    
    
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) {
    	WORD sockVersion;
    	WSADATA wsaData;
    	int nret;
    
    	sockVersion = MAKEWORD(1, 1);
    
    
    	WSAStartup(sockVersion, &wsaData);
    
    	LPHOSTENT hostEntry 66.252.20.206;
    	
    
    
    	if (!hostEntry) {
    		nret = WSAGetLastError();
    		ReportError(nret, "gethostbyname()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    Also, here is the errors that my compiler (Dev-C++) is giving me:

    Code:
    29 C:\Files\dev\Projects\IRC.cpp:23 too many decimal points in number 
     C:\Files\dev\Projects\IRC.cpp In function `int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)': 
    23 C:\Files\dev\Projects\IRC.cpp syntax error before numeric constant 
     C:\Files\dev\Projects\Makefile.win [Build Error]  [IRC.o] Error 1
    Thanks!

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    LPHOSTENT hostEntry 66.252.20.206;
    Well I'm quite certain you can't assign an IP address like that. Looka t your tutorial again.
    I assume that's the error occurring on "line 29". Of course it's hard to tell since that's actually not line 29 of the code you posted - hence, I can't see where the other error is occurring.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    Well I'm quite certain you can't assign an IP address like that.
    The tutorial said to do this:

    Code:
    LPHOSTENT hostEntry;
    
    hostEntry = 66.252.20.206;
    However, that still returned the same error. It's not accepting the IP as a host and I don't know what to do to fix it, thanks anyways

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I don't believe I've used the LPHOSTENT type before, but you definiately can't use IP literals like that. Try putting the IP in quotes?

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The tutorial said to do this:
    Then find a new tutorial. That code you posted is wrong in so many ways...

    First of all, if you have an ip address there is no need to use a hostent structure at all (unless you want to do a reverse lookup). If you need to resolve a domain name, then you would do something like:
    Code:
    LPHOSTENT hostEntry = gethostbyname("somedomain");

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    Thanks for everyones help, I fixed those problems but of course I'm getting another. Last question.. how can I fix this error?

    Code:
      [Linker error] undefined reference to `WSAStartup@8'
    heres the entire code

    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>
    
    #define NETWORK_ERROR -1
    #define NETWORK_OK     0
    
    void ReportError(int, const char *);
    
    
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) {
    	WORD sockVersion;
    	WSADATA wsaData;
    	int nret;
    
    	sockVersion = MAKEWORD(1, 1);
    
    
    	WSAStartup(sockVersion, &wsaData);
    
    	LPHOSTENT hostEntry = gethostbyname("irc.sinisterchat.com");
    	
    
    
    	if (!hostEntry) {
    		nret = WSAGetLastError();
    		ReportError(nret, "gethostbyname()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    	// Create the socket
    	SOCKET theSocket;
    
    	theSocket = socket(AF_INET,			// Go over TCP/IP
    			   SOCK_STREAM,			// This is a stream-oriented socket
    			   IPPROTO_TCP);		// Use TCP rather than UDP
    
    	if (theSocket == INVALID_SOCKET) {
    		nret = WSAGetLastError();
    		ReportError(nret, "socket()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    	// Fill a SOCKADDR_IN struct with address information
    	SOCKADDR_IN serverInfo;
    
    	serverInfo.sin_family = AF_INET;
    
    	// At this point, we've successfully retrieved vital information about the server,
    	// including its hostname, aliases, and IP addresses.  Wait; how could a single
    	// computer have multiple addresses, and exactly what is the following line doing?
    	// See the explanation below.
    
    	serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
    
    	serverInfo.sin_port = htons(6667);		// Change to network-byte order and
    							// insert into port field
    
    
    	// Connect to the server
    	nret = connect(theSocket,
    		       (LPSOCKADDR)&serverInfo,
    		       sizeof(struct sockaddr));
    
    	if (nret == SOCKET_ERROR) {
    		nret = WSAGetLastError();
    		ReportError(nret, "connect()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    	// Successfully connected!
    
    
    	// Send/receive, then cleanup:
    	closesocket(theSocket);
    	WSACleanup();
    }
    
    
    void ReportError(int errorCode, const char *whichFunc) {
       char errorMsg[92];					// Declare a buffer to hold
    							// the generated error message
       
       ZeroMemory(errorMsg, 92);				// Automatically NULL-terminate the string
    
       // The following line copies the phrase, whichFunc string, and integer errorCode into the buffer
       sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);
    
       MessageBox(NULL, errorMsg, "socketIndication", MB_OK);
    }
    Thank you all for your help

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You need to link to the winsock library. I believe the library name is ws2_32.lib for winsock 2. I don't use the compiler you are using, so I'm not sure what you need to do to link to an external library. You can always read the compiler documentation to figure it out.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    Thanks for all of your help, I got it to work I just have one last question. When you connect to an IRC server you have to respond to a ping request to be accepted. In order to respond to the request you have to answer this:

    Code:
    -irc.infinitechat.com- *** If you are having problems connecting due to ping timeouts, please type /raw pong 3b22356b or /quote pong 3b22356b now.
    with either
    Code:
    /raw pong 3b22356b
    or
    Code:
    /quote pong 3b22356b
    the "3b22356b" is a random variable. The things I need to know how to do is read that request when its sent for one, second I need to know how to respond with that variable. Thanks
    Last edited by KuTsuM; 06-07-2005 at 02:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Count established connections
    By TomChesley in forum Networking/Device Communication
    Replies: 1
    Last Post: 05-27-2008, 08:32 PM
  2. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 05:34 PM
  3. Select handling more then 500 connections
    By Chronom1 in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-27-2005, 03:20 PM
  4. Some help needed with IRC
    By SaintK in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2004, 07:27 AM
  5. Multiple Client Connections
    By (TNT) in forum Windows Programming
    Replies: 1
    Last Post: 04-06-2002, 11:04 PM