#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0

    Winsock event 10038


    I keep getting 10038 when I try to connect to my server. I've never had this problem before and I can't spot whats wrong.

    I know it's going to be some retarded thing I did...

    "Error 10038:

    Question/Problem: Socket operation on non-socket.

    Answer/Solution: An operation was attempted on something that is not a socket. The specified socket parameter refers to a file, not a socket."


    void connect( void )
    {
    printf( "\n\n\tEnter the remote IP Address: " );
    scanf( "%s", &address );

    remote.sin_family = AF_INET;
    remote.sin_port = htons( PORT );
    remote.sin_addr.s_addr = inet_addr( address );

    printf( "\n\tConnecting to %s:%d...", address, PORT );

    ret = connect( tcpsock, ( struct sockaddr * ) &remote, sizeof( sockaddr_in ) );

    if( ret == SOCKET_ERROR )
    {
    printf( "\n\n\tError: Call to connect(); failed with: %d\n", WSAGetLastError() );
    exit( 1 );
    }

    printf( "OK" );
    }
  2. #2
  3. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    address is char*, port is int
    Code:
    	sock=socket(AF_INET, SOCK_STREAM, 0); 
    	if(sock==SOCKET_ERROR)
    	{
    		printf("failed.\n"); 
    		return 0; 
    	}
    
    	//gethostbyname, returns pointer to host_ent 
    	hEntry=gethostbyname(address); 
    	if(hEntry==NULL)
    	{
    		printf("Error @ gethostbyname\n"); 
    		return 0; 
    	}
    
    	serverinfo.sin_family = AF_INET; 
    	serverinfo.sin_port   = htons(port); 
    	serverinfo.sin_addr.s_addr   = *(unsigned long*)hEntry->h_addr; 
    
    	//Got info, let's connect 
    	err=connect(sock, (struct sockaddr*)&serverinfo, sizeof(struct sockaddr)); 
    	if(err==SOCKET_ERROR)
    	{
    		handle_error(); 
    		return 0; 
    	}
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    I don't really understand what your getting at.... :\

    what part of my code should be changed ?
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    you need to do error checking on the call to socket(). your socket is not being created correctly, or the variable that you claim to contain the socket doesnt actually contain it. where is the rest of the code.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    It seems to work now, though I don't know what I did to make it work. I wrote this real fast so you'll probably find errors...

    Any suggestions on how to improve it are helpful.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <winsock.h>
    
    #define PORT 666
    #define ADDRESS "127.0.0.1"
    
    WSADATA wsda;
    sockaddr_in remote;
    
    SOCKET tcpsock;
    int ret;
    
    char address[40];
    char filename[20];
    char buffer[1024];
    
    void connect( void );
    void send( void );
    
    int main( void )
    {
    	WSAStartup( MAKEWORD( 1,1 ), &wsda );
    
    	printf( "\n\n\tCreating Socket..." );
    
    	tcpsock = socket( AF_INET, SOCK_STREAM, IPPROTO_IP );
    
    	if( tcpsock == SOCKET_ERROR )
    	{
    		printf( "\n\n\tError: Socket Creation Failed." );
    		exit(1);
    	}
    
    	printf( "OK" );
    
    	while( 1 )
    	{
    		connect();
    		send();
    	}
    
    	return 0;
    }
    
    void connect( void )
    {
    	printf( "\n\n\tEnter the remote IP Address: " );
    	scanf( "%s", &address );
    
    	remote.sin_family = AF_INET;
    	remote.sin_port = htons( PORT );
    	remote.sin_addr.s_addr = inet_addr( address );
    
    	printf( "\n\tConnecting to %s:%d...", address, PORT );
    
    	ret = connect( tcpsock, ( struct sockaddr * ) &remote, sizeof( sockaddr ) );
    
    	if( ret == SOCKET_ERROR )
    	{
    		printf( "\n\n\tError: Call to connect(); failed with: %d\n", WSAGetLastError() );
    		exit( 1 );
    	}
    
    	printf( "OK" );
    }
    
    void send( void )
    {
    	FILE *fp;
    	int bytes_read;
    	int total = 0;
    
    	printf( "\n\n\tEnter the Path Filename of the file you want to send: " );
    	scanf( "%s", &filename );
    
    	if( send( tcpsock, filename, strlen( filename ), 0 ) == -1 )
    	{
    		printf( "\n\n\tError: Filename Send Failed" );
    		exit(1);
    	}
    
    	if( ( fp = fopen( filename, "rb" ) ) == NULL )
    	{
    		printf( "\n\n\tError: File not found." );
    		exit(1);
    	}
    
    	while( ( bytes_read = fread( buffer, sizeof( char ), 1024, fp ) ) > 0 )
    	{
    		if( send( tcpsock, buffer, bytes_read, 0) == -1)
    		{
    			printf( "\n\n\tError: File Send Failed" );
    			exit(1); 
    		}
    
    		total += bytes_read; 
    	}
    
    	fclose( fp );
    
    	closesocket( tcpsock );
    	printf("\n\n\tDone, sent %d bytes\n", total); 
    }

IMN logo majestic logo threadwatch logo seochat tools logo