-
socket error
I am tring to create a program that will let me loggin to irc... but.. i keep getting
Connecting to server...
NOTICE AUTH :*** Looking up your hostname.....
NOTICE AUTH :*** Checking Ident
NOTICE AUTH :*** Found your hostname
NOTICE AUTH :*** Got Ident response
ERROR :Closing Link: [[email protected]]
well i made it that far to get it connected.. but it wont give me all the info that it should.. so that i can join a chan etc..
anyways.. here is the code ( please take it easy im new to socket and am really stressed about not remembering somethings so streesed i can cry)
Code:
#define MYPORT 6667
int main( )
{
struct sockaddr_in mysock;
struct sockaddr_in tosock;
struct hostent *addr;
int s_sock, s_conn, x;
int len, s_send, s_recv;
char *p_buff, buff[525];
char *msg;
if((s_sock = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
{
fprintf( stderr, "Error on SOCKET( )" );
return 1;
}
/* fgets( buff, 20, stdin );
printf("xlordt suxz 1\n");
*/
mysock.sin_family = AF_INET;
mysock.sin_port = htons( MYPORT );
mysock.sin_addr.s_addr = inet_addr( "66.252.6.72" );
if((s_conn = connect( s_sock, ( struct sockaddr *)&mysock, sizeof mysock)) == -1 )
{
fprintf( stderr, "Error on SOCKET( )" );
return 1;
}
printf( "Connecting to server...\n" );
p_buff = buff;
p_buff = (char *)malloc( 64 * sizeof( char ));
for( x=0; x < 25; x++ )
{
if((s_recv = recv( s_sock, p_buff, sizeof p_buff, 0 )) == -1 )
{
fprintf( stderr, "Error on RECV( )" );
return 1;
}
printf( "%s", p_buff );
msg = "PING";
len = strlen( msg );
send( s_sock, msg, len, 0 );
recv( s_sock, p_buff, sizeof p_buff, 0 );
printf( "%s", p_buff );
}
msg="/nick hmmmm";
len = strlen( msg );
if((s_send = send( s_sock, msg, len, 0 )) == -1 )
{
strerror( errno );
return 1;
}
msg="/join #chan";
len = strlen( msg );
if((s_send = send( s_sock, msg, len, 0 )) == -1 )
{
strerror( errno );
return 1;
}
return 0;
close( s_sock );
}
I am just going crazy tring new $$$$ here.. =/
-
>> p_buff = buff;
>> p_buff = (char *)malloc( 64 * sizeof( char ));
Why do you point it to buff, then in the next breath to dynamic memory?
Also, you didn't free the memory.
>> if((s_recv = recv( s_sock, p_buff, sizeof p_buff, 0 )) == -1 )
The sizeof(ANY POINTER) == 4.
TCP is a *sliding window* protocol. You may send 255 bytes, but the other end may recieve these in 127 bytes chunks, and vice-versa. The point is, you are supposed to do your own book keeping, and build your buffer as the reception proceeds.
My advice is to put this project on the backburner, re-read your C-programming books, and try to build some real skills so that when you *really* need to do something like this, you will be able to pull it off with a least a modicum of competence.
Cheers.
-
Ya in a way your right.. but.. when i post something that i get from a book.. to test it out.. and it doesnt work.. i get questions like... why you do that ... why you did this... :( kinda confusing.. hmm heh anyways.. i will keep tring.. thanx =) eventually i will get it.. :) i just wish these damn books were a bit more straight forward( or should i say... a bit more less error less )