Thread: Socket or send() problem?

  1. #1
    Registered User
    Join Date
    Oct 2005

    Socket or send() problem?

    I posted another topic about needing to Multithread, and I got that workin' all fine and dandy, but now it seems that my socket is buggin' out.

    I'm connecting to an IRC server and trying to send/recieve whatever data that I need, but my send is acting kind of funny. Upon connecting to the server, I recieve the first message fine: NOTICE AUTH :*** Found your hostname

    Therefore my recv is working just fine. But I then try to send the USER and NICK command to the IRC server (which should produce a crapload of messages from the server), but it doesn't seem to be going through. I know its not sending because my friend is hosting the IRC server, and can detect when someone sends the USER and NICK commands, and he is getting nothing when I supposively send. I am getting no response from the server when I try to send it data... not even an error message when I send some spam. Any help would be greatly appreciated.

    NOTE: I'm coding in Windows, using winsock2 library.


    Socket connection, setup, etc.
    	wVersionRequested = MAKEWORD( 2, 2 );
    	err = WSAStartup(wVersionRequested, &wsaData );
    	if(err != 0) {
    		printf("WSA startup fail.\n");
    	m_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if(m_socket == INVALID_SOCKET){
    		printf( "Error at socket(): %ld\n", WSAGetLastError() );   
    	clientService.sin_family = AF_INET;
    	clientService.sin_addr.s_addr = inet_addr( hostToIP("") );
    	clientService.sin_port = htons( 6667 );
    	if ( connect( m_socket, (SOCKADDR*) &clientService,
    			   sizeof(clientService) ) == SOCKET_ERROR) {
    		printf( "Failed to connect.\n" );
    	} else {
    		printf("Connection Success!\n\n");
    send() loop:
    	do {
    		if((s_ready = select(0, NULL, &fd, NULL, NULL)) != SOCKET_ERROR) {
    			if(strstr("/q", input)) break;
    			bSent = send(m_socket, input, sizeof(char)*strlen(input), 0);
    			printf("Sent(%d): %s\n", bSent, input);
    	} while (1);
    recv() loop:
    	while(go == 1) {
    		if((err = ioctlsocket(m_socket, FIONREAD, &l)) == SOCKET_ERROR) {
    			printf("Error: ioctlsocket(). error: %d\n", WSAGetLastError());
    		} else if(l > 0) {
    				printf("Trying to recieve...\n");
    				bytesIn = recv(m_socket, rec, BUFFMAX, 0);
    				if(bytesIn > 0) {
    					printf("%s", rec);
    					rec[0] = '\0';
    		ready = 1;
    The two latter snippets of code are running simotaneously via multithreading. This program shoould essentially be a telnet program untill I get some command parsing going on .

    Note 2: flusher() and hostToIP() are functions that I have created.
    Last edited by Achy; 06-09-2006 at 12:09 AM.

  2. #2
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Still in the egg
    Well, I did not have a clear look at the code. But you said, you cannot send USER and NICK to IRC server. Did you try to listen your network traffic. For example with tcpdump or ethereal? I suggest you to run one of these tools and learn exactly what you send to the network.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Thats a good idea. I can telnet fine to the irc server, but it seems like it just isn't leaving my machine from my program. I'll give it a try.


  4. #4
    Registered User
    Join Date
    Oct 2005
    When I get the successfull respons (upon connecting to the server) Ethereal shows an IRC response as expected. When I send out a message it sends a packet with my string I sent and looks fine, but I get back a bunch of jarble. Do I need to attatche a certain header or something... it seems like my data is actually being sent, which is not what I expected.

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Banks of the River Styx
    What does the printf("Sent(%d)... line give? Is it sending the entire buffer?
    Do you need to send a newline after sending your USER command? (gets() will replace a terminating \n with a \0, see gets().) gets() also does no buffer overflow checking, its use is hazardous. Ensure that there is enough room in input. Most will recommend replacing your call of gets() with one to fgets(). (Which won't erase a newline, btw, again, read the help pages)

    What does flusher() do? What will you do if send() doesn't send the complete buffer?
    And last, select()'s first parameter is usually set to the highest file descriptor in any of the three sets, +1. See it's man page. This parameter is, I believe, ignored in Windows, but if you ever wanted to port to another platform, you might want to include it.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User
    Join Date
    Oct 2005
    fgets() saves the day! I didn't know that gets() took off the \n... woops! Thanks a lot! Works like a charm now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. sending n no of char data uning send() function,
    By thebrighter in forum Windows Programming
    Replies: 1
    Last Post: 08-22-2007, 12:26 PM
  3. problem closing socket
    By Wisefool in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-29-2003, 12:19 PM
  4. C socket problem
    By TeMpEsT-9 in forum C Programming
    Replies: 6
    Last Post: 07-27-2002, 12:53 PM
  5. stream socket problem
    By WL in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 11:07 PM