Thread: Communication ~Please no flaming

  1. #16
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Ok, thanks alot jmd15, you've been a great help.

  2. #17
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    This is what I have so far
    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <fstream.h>
    
    #define NETWORK_ERROR -1
    #define NETWORK_OK 0
    
    void ReportError(int, const char*);
    
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
    {
        WORD sockVersion; /*Initializing the word variable to hold which version of 
        Winsock we want to use(which is 1.1)*/
        WSADATA wsaData; /*The structure that holds our data needed for startup,
        etc*/
        int nret;/*Just an integer that we use to check if the functions failed or 
        not*/
        
        socksVersion(MAKEWORD 1,1); /*Giving sockVer the right value so when passed 
        to WSAStartup it knows to use version 1.1*/
        
        WSAStartup(sockVersion, &wsadata);/*Starting up WSA which stands for
        Windows Socket Architecture(NEEDS TO BE INITIALIZED TO USE WINSOCK!!!)*/
        
        
        SOCKET listningsocket;/*Creating a variable of type SOCKET to use*/
        listningsocket=socket(AF_INET,/*Go over TCP/IP*/ SOCK_STREAM,/*this is a 
        stream-oriented socket*/IPPROTO_TCP/*we would like TCP rather than UDP*/);
        
        if (listningsocket==INVALID_SOCKET)
        {
            nret=WSAGetLastError();        /*Get a more detailed error message*/
            ReportError(nret,"socket()"); /*Report our error with our custom 
            function*/
            
            WSACleanup(); /*Shutdown WinSOCK*/
            return NETWORK_ERROR; /*Return error value*/
        }
        
            struct sockaddr_in
            {
                short sin_family=AF_INET; /*Specifies protocol*/
                u_short sin.port=1337;    /*Specifies port, just having fun
                with the port number*/
                struct in_addr sin_addr=24.186.150.196 /*Specifies IP address of 
                server*/
                char sin_zero[8]; /*unused*/
            }
    I'm just making my own code but referring to your code and the tutorial you gave me.
    Last edited by bikr692002; 11-18-2005 at 07:40 PM.

  3. #18
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yeah, right here:
    Code:
    struct sockaddr_in
            {
                short sin_family=AF_INET; /*Specifies protocol*/
                u_short sin.port=1337;    /*Specifies port, just having fun
                with the port number*/
                struct in_addr sin_addr=24.186.150.196 /*Specifies IP address of 
                server*/
                char sin_zero[8]; /*unused*/
            }
    SOCKADDR_IN is already defined, I know in the tutorial it makes it LOOK like you have to define it, but they were just showing you the members of that structure. You use it like so:
    Code:
    SOCKADDR_IN serverInfo;  //declaring a sockaddr_in struct
    serverInfo.sin_family=AF_INET;  //Setting all the member values
    serverInfo.sin_port=htons(1337);  //Need to use htons
    serverInfo.sin_addr=*((LPIN_ADDR)*hostEntry->h_addr_list);
    EDIT:
    Oh yeah, and this part:
    Code:
    listningsocket=socket(AF_INET,/*Go over TCP/IP*/ SOCK_STREAM,/*this is a 
        stream-oriented socket*/IPPROTO_TCP/*we would like TCP rather than UDP*/);
    It may be legal code but I just really don't like the comments within the function. If you want to do it then fine, but it bugs me is all.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  4. #19
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    So this is the code, completed, but not compiling :'(
    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>
    
    #define NETWORK_ERROR -1
    #define NETWORK_OK 0
    
    void ReportError(int, const char*); /*The variable to control the error 
    message*/
    
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
    {
        WORD sockVersion; /*Initializing the word variable to hold which version of 
        Winsock we want to use(which is 1.1)*/
        WSADATA wsaData; /*The structure that holds our data needed for startup,
        etc*/
        int nret;/*Just an integer that we use to check if the functions failed or 
        not*/
        
        socksVersion(MAKEWORD 1,1); /*Giving sockVer the right value so when passed 
        to WSAStartup it knows to use version 1.1*/
        
        WSAStartup(sockVersion, &wsadata);/*Starting up WSA which stands for
        Windows Socket Architecture(NEEDS TO BE INITIALIZED TO USE WINSOCK!!!)*/
        
        
        SOCKET listningsocket;/*Creating a variable of type SOCKET to use*/
        listningsocket=socket(AF_INET,/*Go over TCP/IP*/ 
                              SOCK_STREAM,/*this is a stream-oriented socket*/
                              IPPROTO_TCP);/*we would like TCP rather than UDP*/
        
        if (listningsocket==INVALID_SOCKET)
        {
            nret=WSAGetLastError();        /*Get a more detailed error message*/
            ReportError(nret,"socket()"); /*Report our error with our custom 
            function*/
            
            WSACleanup(); /*Shutdown WinSOCK*/
            return NETWORK_ERROR; /*Return error value*/
        }
        
        /*Use a SOCKADDR_IN to fill in address information*/
        SOCKADDR_IN serverInfo;
        
        serverInfo.sin_family=AF_INET;           /*Tells what protocol to use*/
        serverInfo.sin_addr.s_addr=INADDR_ANY;   /*Since this socket is listning for
        connections, any local address will do*/
        serverInfo.sin_port=htons(1337); /*Converts integer (1337) to network-byte
        order and inserts into the port field*/
        
        /*Bind the socket to our local server address*/
        nret=bind(listningsocket,(LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
        
        if (nret==SOCKET_ERROR)
        {
            nret=WSAGetLastError();
            ReportError(nret,"bind()");
            
            WSACleanup();
            return NETWORK_ERROR;
        }
        
        /*Make the socket listen*/
        nret=listen(listningsocket,10); /*Up to 10 connections may wait at any one
        time to be accept()'ed*/
        if (nret==SOCKET_ERROR)
        {
            nret=WSAGetLastERROR();
            ReportError(nret,"listen()");
            
            WSACleanup();
            return NETWORK_ERROR;
        }
        
        /*Wait for client*/
        SOCKET theClient;
            
        theClient=accept(listeningsocket,
                                    NULL,     /*Address of sockaddr structure*/
                                    NULL);    /*Address of a variable containing size of
        sockaddrstruct*/
        if (theClient==INVALID_SOCKET)
        {
            nret=WSAGetLastError();
            ReportError(nret, "accept()");
                
            WSACleanup();
            return NETWORK_ERROR;
        }
            
            
        /*Send and recieve from the client, and filally*/
        closesocket(theClient);
        closesocket(listningsocket);
            
                
        /*Shutdown WinSOCK*/
        WSACleanup();
        return NETWORK_OK;
    }
        
    void ReportError(int ErrorCode, const char *whichFunc)
    {
        char errorMsg[92];
            
        ZeroMemory(errorMsg, 92);
        /* The following line copies the phrase, whichFunc string, and integer 
        errorCode into the buffer*/
        sprintf(errorMsg, "Call to %s returned error %d!", (char*)whichFunc,
        errorCode);
            
        MessageBox(NULL, errorMsg, "socketIndication",MB_OK);
    }
    Just to connect/disconnect

  5. #20
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Right here:
    Code:
    socksVersion(MAKEWORD 1,1);
    socksVersion is a variable not a function. MAKEWORD is the macro, used like so:
    Code:
    socksVersion=MAKEWORD(1,1);
    Other than that it looks pretty good, but PLEASE you need to post your compile errors so I don't have to read through the whole code again.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  6. #21
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    I fixed that
    But theres more

  7. #22
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    And the compile errors are.......
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unkown Hang
    By Bladactania in forum C Programming
    Replies: 31
    Last Post: 04-22-2009, 09:33 AM
  2. Communication between programs?
    By johny145 in forum Windows Programming
    Replies: 3
    Last Post: 06-01-2005, 10:14 PM
  3. Looking for communication lib
    By BrownB in forum C Programming
    Replies: 3
    Last Post: 04-27-2005, 10:01 AM
  4. Serial communication packets
    By Roaring_Tiger in forum C Programming
    Replies: 3
    Last Post: 04-26-2003, 08:33 AM
  5. communication
    By in need of help in forum C Programming
    Replies: 3
    Last Post: 12-27-2002, 03:56 PM