Sockets

This is a discussion on Sockets within the Windows Programming forums, part of the Platform Specific Boards category; I've tried using Dsock. It would work if I could make Turbo C++ work. However what i'm using is DJGPP. ...

  1. #1
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43

    Unhappy Sockets

    I've tried using Dsock. It would work if I could make Turbo C++ work. However what i'm using is DJGPP. Are there any socket libraries for DJGPP out there? I tried to search for some via google, but couldn't find any. Also I have tried this. However it doesn't compile.

    Code:
    #include <windows.h>
    #include <windowsx.h>
    #include <winsock.h>
    #include "tcp.h"
    
    /*
     *   init_winsock()             Will try to initialize winsock 2.0 and returns true if it succeeds
     *   connect_to_host(host,port) Creates a socket, sets the options for non-blocking, and connects
    								it returns true if it succeeds and error codes defined in the header
    								if it fails (a check for < 0 works too)
    	read_from_socket(buf, size) Reads from the socket if data is ready and stores it in the buffer
    								provided.  Size should be the size of the char array being used is
    								dimensioned to.  It returns false if no data is ready or an error
    								occurs.  It returns -1 if a lost connection is detected
    	write_to_socket(buf)        Wrapper to send() to the socket, the entire buffer is written
    	shutdown_winsock()          Closes the socket and uninitializes winsock
    	close_connection()          Wrapper for closesocket()
     */
    SOCKET sock;
    
    void close_connection(void)
    {
    	closesocket(sock);
    }
    
    int init_winsock(void)
    {
        WORD wVersionRequested = MAKEWORD(2,0);
        WSADATA wsaData;
    
        WSAStartup(wVersionRequested, &wsaData);
    
        if (wsaData.wVersion != wVersionRequested)
        {
    		wVersionRequested = MAKEWORD(1,1);
    		WSACleanup();
            WSAStartup(wVersionRequested, &wsaData);
            if (wsaData.wVersion != wVersionRequested)
    	  	  return false;
        }
        return true;
    }
    
    int connect_to_host(char *hostname, int port)
    {
        LPHOSTENT lpHostEntry;
        SOCKADDR_IN saServer;
        int arg;
        unsigned long arg2;
    
    	// Perform DNS lookup
        lpHostEntry = gethostbyname(hostname);
        if (lpHostEntry == NULL)
        {
            return DNS_FAILURE;
        }
    
    	// Create the socket
        sock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    
    	// set the recv buf to 4k
    	arg = 4096;
        setsockopt(sock,SOL_SOCKET,SO_RCVBUF,(char *) &arg,sizeof(arg));
    
    	// set the send buf to 1k
        arg = 1024;
        setsockopt(sock, SOL_SOCKET,SO_SNDBUF, (char *) &arg, sizeof(arg));
    
    	if (sock == INVALID_SOCKET)
        {
    		return SOCKET_FAILURE;
        }
    
        // Connect to host
        saServer.sin_family = PF_INET;
        saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
        saServer.sin_port = htons(port);
    
        if ( connect(sock,
    				   (LPSOCKADDR)&saServer,
    				   sizeof(struct sockaddr)) == SOCKET_ERROR)
    	{
    		closesocket(sock);
    		return CONNECT_FAILURE;
    	}
    
    	// Turn off blocking mode
        ioctlsocket(sock,FIONBIO,&arg2);
    
        return true;
    }
    
    int read_from_socket(char * buf, int bufsize)
    {
        int cnt;
    //	unsigned long arg;
    
    //	if (ioctlsocket(sock, FIONREAD, &arg) == SOCKET_ERROR)
    //		return -1;
    //	if (arg <= 0) return false;
    
    	cnt = recv(sock,buf, bufsize,0);
    	if (cnt == SOCKET_ERROR || cnt == 0)
    	{
    		int error;
    		error = WSAGetLastError();
    		// Might should check for other error codes too, but these seem most likely
    		// to indicate lost connection
    		if (error == WSAENETRESET     ||
    				error == WSAECONNABORTED  ||
    				error == WSAECONNRESET    ||
    				error == WSAEINVAL ||
    				cnt == 0)
    			return -1;
    		return false;
    	}
    	buf[cnt] = '\0';
    	return true;
    }
    
    void write_to_socket(char * buf)
    {
       send(sock, buf, strlen(buf),0);
       return;
    }
    
    void shutdown_winsock(void)
    {
    	// cleanup winsock
    	closesocket(sock);
    	WSACleanup();
    	return;
    }
    Something to do with parse errors in winsock.h

    Does that mean I need to find a new winsock.h?
    To error is human, to really foul things up requires a computer

  2. #2
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    Could be. Make sure that you have ws2_32.lib. Also make sure that winsock.h is included before windows.h and usually it's best to initialize winsock 2,0 and use winsock2.h, because some portions of Windows Sockets 1 are obsolete. If you still have problems, then post all of your code, including tcp.h and I'll try compiling it and trying to find the source of the errors.

  3. #3
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    Are you also sure that you want to be using PF_INET rather than AF_INET?

    Maybe you should also try using something like:

    saServer.sin_addr.S_un.S_addr = inet_addr(lpHostEntry->h_addr_list);

    rather than:

    saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);

    Otherwise, your code looks like it should work. (I havent worked out all of your constants etc.. from your setsockopt or anything).

  4. #4
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    Actually I figured it out. I'm trying to use DJGPP to compile, but someone pointed out to me that DJGPP just makes dos programs. So I'm getting Visual C++ 6, and hoping that will work. I'm assuming I can still make regular C dos programs with visual c++ anyways.
    To error is human, to really foul things up requires a computer

  5. #5
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    Yes, Visual C++ allows you to create console applications.

  6. #6
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    What I posted earlier was my tcp.cpp which compiles fine in VC. However this doesn't.

    The error i'm getting;
    --------------------Configuration: autoroller - Win32 Debug--------------------
    Linking...
    autoroller.obj : error LNK2001: unresolved external symbol _write_to_socket
    autoroller.obj : error LNK2001: unresolved external symbol _read_from_socket
    autoroller.obj : error LNK2001: unresolved external symbol _connect_to_host
    autoroller.obj : error LNK2001: unresolved external symbol _init_winsock
    Debug/autoroller.exe : fatal error LNK1120: 4 unresolved externals
    Error executing link.exe.

    autoroller.exe - 5 error(s), 0 warning(s)

    the code
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include "tcp.h"
    
    
    
    char name[100];
    char race[100];
    char clas[100];
    char sub_class[100];
    char alignment[100];
    char moral[100];
    char hometown[100];
    char sex[100];
    
    
    
    
    // To write to the file
    
    // Prints the races for good alignment
    void do_good()
    {
    	printf("------------------------------------------\n\r");
    	printf("human         costs   0 experience points.\n\r");
    	printf("dwarf         costs 500 experience points.\n\r");
    	printf("elf           costs 500 experience points.\n\r");
    	printf("centaur       costs 400 experience points.\n\r");
    	printf("gnome         costs 400 experience points.\n\r");
    	printf("draconian     costs 300 experience points.\n\r");
    	printf("changeling    costs 300 experience points.\n\r");
    	printf("halfling      costs 300 experience points.\n\r");
    	printf("minotaur      costs 300 experience points.\n\r");
    	printf("arborian      costs 500 experience points.\n\r");
    	printf("------------------------------------------\n\r");
    }
    
    void do_specs()
    {
    	
    	
    	//Name
    	printf("Name for character?\n\r");
    	gets(name);
    	
    		
    		if (name == '\0')
    		{
    			printf("Name was blank.\n\r");
    		}
    
    	//Password
    	printf("The password is set by default to abcde.\n\r");
    	
    
    	//Align
    	printf("Alignment?\n\r");
    	gets(alignment);
    	strupr(alignment);
    
    	
    
    	//Morality (DM, Morality? Bah!)
    	printf("What are their morals to be?\n\rM/I/N\n\r");
    	gets(moral);
    	strupr(moral);
    
    
    RACE:
    	printf("What is your race to be?\n\r");
    	gets(race);
    
    
    		if (race == '\0')
    		{
    			printf("Pick a race.\n\r");
    			goto RACE;
    		}
    	
    
    SEX:
    	printf("Whats the sex to be?\n\r");
    	printf("If Illithid, remember to choose n for none.\n\r");
    	gets(sex);
    
    		if (sex == '\0')
    		{
    			printf("Pick a sex. Male or Female. (M/F)\n\r");
    			goto SEX;
    		}
    		
    
    CLASS:
    	printf("What class is it to be?\n\r");
    	gets(clas);
    
    		if (clas == '\0')
    		{
    			printf("Pick a class.\n\r");
    			goto CLASS;
    		}
    
    
    
    	printf("Whats the sub-class?\n\r");
    	printf("If your choosing a class with no sub-class, just press enter.\n\r");
    	gets(sub_class);
    	
    
    	// Stats
    
    
    
    HOMETOWN:
    	printf("What hometown do you wish?\n\r");
    	gets(hometown);
    
    	if (hometown == '\0')
    	{
    		printf("Enter a hometown.\n\r");
    		goto HOMETOWN;
    	}
    
    }
    
    void send_spec()
    {
    	write_to_socket(name);
    	write_to_socket( "abcde");
    	write_to_socket( alignment);
    	write_to_socket( sex);
    	write_to_socket( race);
    	write_to_socket( clas);
    	write_to_socket( sub_class);
    }
    
    /* Werv's Autoroller, modified. */
    void check_autoroller(void)
    {
    	/* this autoroller is for DM only! */
       char buf[400];
       char *from;
       int stat;
    
       int str, in, wis, dex, con;
    
       	printf("Max str?\n\r");
       	scanf("%d",&str);
       	printf("Max int?\n\r");
       	scanf("%d",&in);
       	printf("Max wis?\n\r");
       	scanf("%d",&wis);
       	printf("Max dex?\n\r");
       	scanf("%d",&dex);
       	printf("Max con?\n\r");
    	scanf("%d",&con);
    
       	from = read_from_socket(buf, 400);
    
    	   if (from[0] != 'S')
    	      return;
    	   // str
    	   stat = (from[10]-48) * 10 + from[11]-48;
    	   if (stat < str){
    		write_to_socket("no\n");
    	    return;
    	   }
    	   // int
    	   stat = (from[27]-48) * 10 + from[28]-48;
    	   if (stat < in){
    	    write_to_socket("no\n");
    	    return;
    	   }
    	   // wis
    	   stat = (from[38]-48) * 10 + from[39]-48;
    	   if (stat < wis){
    	    write_to_socket("no\n");
    	    return;
    	   }
    	   // dex
    	   stat = (from[52]-48) * 10 + from[53]-48;
    	   if (stat < dex){
    	    write_to_socket("no\n");
    	    return;
    	   }
    	   // con
    	   stat = (from[69]-48) * 10 + from[70]-48;
    	   if (stat < con){
    	    write_to_socket("no\n");
    	    return;
    	   }
    	   write_to_socket("yes\n");
    
    	   return;
    }
    
    int main(void)
    {
    	char host;
    	int port;
    
    	host = ("darkmists.org");
    	port = 2222;
    	init_winsock();
    	connect_to_host(host,port);
      	do_specs();
    	send_spec();
    	check_autoroller();
    
    	return 0;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    	/*
    
    	sample roll:
    
    	Strength: 15 Intelligence: 12 Wisdom: 16 Dexterity: 16 Constitution: 15
    	Do these reflect your training (Y/N)?
    
    	*/
    I'm baffled. I'm new to this visual c++, but I had the wits about me to set wsock32.lib in the linker. Then tcp.cpp the first code I posted here compiled fine. This second part is autoroller.c and thats where the warning is comming from. Any suggestions?
    To error is human, to really foul things up requires a computer

  7. #7
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    Oh, and my tcp.h just for giggles.


    Code:
    #define DNS_FAILURE     -1
    #define SOCKET_FAILURE  -2
    #define CONNECT_FAILURE -3
    
    int  init_winsock(void);
    int  connect_to_host(char* hostname, int port);
    int  read_from_socket(char * buf, int bufsize);
    void write_to_socket(char * buf);
    void shutdown_winsock(void);
    void close_connection(void);
    To error is human, to really foul things up requires a computer

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I'm new to this visual c++
    Then why are you posting on the C forum

    Moving to the Windows forum...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    You have the header for these routines but the linker can't find the executables. Where are they, a library? If so, have you added that library to the linker's list.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 01:43 PM
  2. Cross platform sockets
    By zacs7 in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-27-2007, 05:16 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 06:36 PM
  4. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 04:52 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 10:49 AM

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