Home Forums Register FAQ Members List Calendar Search Today's Posts Mark Forums Read Arcade Chat
Go Back   ClubPenguinHQ Forums > General Discussion > Programming > Other
Reload this Page C++ Socket Library
Other Discussion of programming languages that don't fall into the other categories belongs here.

C++ Socket Library

Reply
 
LinkBack Thread Tools Display Modes
C++ Socket Library
Old
  (#1)
Charlie__ is Offline
New Member
Charlie__ is an unknown quantity at this point
 
Posts: 11
Join Date: Jan 2009
Rep Power: 0
   
Default C++ Socket Library - Sunday, January 25th, 2009

This socket library is the library that was used with PCL++.
It is very simple compared to most ocket libraries that are available. This is for windows. Before you start using sockets in your program you must put this code. You must link your program to ws2_32.dll, when ocmpiling with bloodshed dev link to lib/libws2_32.a instead.
Before your first connecttosocket put.
Code:
int ws2result;
ws2result = startws2();
if(ws2result != 0) {
//printf("WSAStartup failed: %d\n", ws2result);
return ws2result;
}
Put this in a file called socket.h and include it in your program.
Code:
#include <winsock2.h>
    SOCKET ConnectSocket = INVALID_SOCKET;
    char recvbuf[DEFAULT_BUFLEN];
    using namespace std;

    // Initialize Winsock
    int startws2(){
       WSADATA wsaData;
       iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
       if(iResult != 0) {
          /*printf("WSAStartup failed: %d\n", iResult);*/
          return iResult;
       }
       else{
          return 0;
       }
    }
    int connecttosocket(string host, string portno){
    struct addrinfo *result = NULL,
                    *ptr = NULL,
                    hints;

    ZeroMemory( &hints, sizeof(hints) );
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    #define DEFAULT_PORT "6112"


    iResult = getaddrinfo(host.c_str(), portno.c_str(), &hints, &result);
    if ( iResult != 0 ) {
        printf("getaddrinfo failed: %d\n", iResult);
        WSACleanup();
        return 1;
    }



    ptr=result;

    // Create a SOCKET for connecting to server
    ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);

    // Connect to server.
    iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        closesocket(ConnectSocket);
        ConnectSocket = INVALID_SOCKET;
    }
    freeaddrinfo(result);

    if (ConnectSocket == INVALID_SOCKET) {
        printf("Unable to connect to server!\n");
        WSACleanup();
        return 1;
    }
}

    char *readRawPacket(){
    int recvbuflen = DEFAULT_BUFLEN

    do {

        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
       if( iResult > 0 ){
          return recvbuf;
       }
       else if ( iResult == 0 ){
            //printf("Connection closed\n");
          return "0";
       }
        else
            printf("recv failed: %d\n", WSAGetLastError());
          return "-1";

    } while( iResult > 0 );
    return "1";
    }

    int sendRawPacket(string sendbuf) {
       // Send an initial buffer
       iResult = send( ConnectSocket, sendbuf.c_str(), (int)strlen(sendbuf.c_str()) + 1, 0 );
       if (iResult == SOCKET_ERROR) {
          printf("send failed: %d\n", WSAGetLastError());
          closesocket(ConnectSocket);
          WSACleanup();
          return 1;
       }
    }
  
Reply With Quote
Re: C++ Socket Library
Old
  (#2)
yo2boy is Offline
Senior Member
yo2boy is an unknown quantity at this point
 
yo2boy's Avatar
 
Posts: 516
Join Date: Apr 2008
Location: #iamthewalrus and/or Canada.
Rep Power: 0
  Send a message via MSN to yo2boy Send a message via Skype™ to yo2boy 
Default Re: C++ Socket Library - Sunday, January 25th, 2009

Pretty damn good.


http://clubpenguinhq.com/forums/image.php?type=sigpic&userid=11373&dateline=124020  0589
  
Reply With Quote
Re: C++ Socket Library
Old
  (#3)
Stanley is Offline
Tech God
Stanley is an unknown quantity at this point
 
Stanley's Avatar
 
Posts: 758
Join Date: Aug 2008
Rep Power: 0
  Send a message via AIM to Stanley Send a message via MSN to Stanley  
Default Re: C++ Socket Library - Monday, January 26th, 2009

Cool,Charles = Master at C++


Just an old CPHQ User.
  
Reply With Quote
Reply

Lower Navigation
Go Back   ClubPenguinHQ Forums > General Discussion > Programming > Other

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
vBulletin Skin developed by: vBStyles.com
Copyright ©2006 - 2009, RancidKraut Industries