C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-09-2006, 12:03 AM   #1
Registered User
 
Join Date: Oct 2005
Posts: 17
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.

Problem:
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:

:irc.XXX.com 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.

Code:

Socket connection, setup, etc.
Code:
	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("irc.XXX.com") );
	clientService.sin_port = htons( 6667 );

	if ( connect( m_socket, (SOCKADDR*) &clientService,
			   sizeof(clientService) ) == SOCKET_ERROR) {
		printf( "Failed to connect.\n" );
		WSACleanup();
	} else {
		printf("Connection Success!\n\n");
	}
send() loop:
Code:
	do {
		if((s_ready = select(0, NULL, &fd, NULL, NULL)) != SOCKET_ERROR) {
			gets(input);
			if(strstr("/q", input)) break;
			bSent = send(m_socket, input, sizeof(char)*strlen(input), 0);
			printf("Sent(%d): %s\n", bSent, input);
			flusher(input);
		}	
	} while (1);
recv() loop:
Code:
	while(go == 1) {
		if((err = ioctlsocket(m_socket, FIONREAD, &l)) == SOCKET_ERROR) {
			printf("Error: ioctlsocket(). error: %d\n", WSAGetLastError());
			break;
		} 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.
Achy is offline   Reply With Quote
Old 06-09-2006, 01:23 AM   #2
Just kidding....
 
fnoyan's Avatar
 
Join Date: Jun 2003
Posts: 223
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.
fnoyan is offline   Reply With Quote
Old 06-09-2006, 12:02 PM   #3
Registered User
 
Join Date: Oct 2005
Posts: 17
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.

Thanks!
Achy is offline   Reply With Quote
Old 06-09-2006, 12:49 PM   #4
Registered User
 
Join Date: Oct 2005
Posts: 17
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.
Achy is offline   Reply With Quote
Old 06-09-2006, 12:56 PM   #5
int x = *((int *) NULL);
 
Cactus_Hugger's Avatar
 
Join Date: Jul 2003
Location: Banks of the River Styx
Posts: 891
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)
Cactus_Hugger is offline   Reply With Quote
Old 06-09-2006, 01:09 PM   #6
Registered User
 
Join Date: Oct 2005
Posts: 17
fgets() saves the day! I didn't know that gets() took off the \n... woops! Thanks a lot! Works like a charm now!
Achy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:36 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22