Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,628 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,019 people online right now. Registration is fast and FREE... Join Now!





Winsock example

Main() function for all winsock routines, not written by me.

Submitted By: cipherence
Actions:
Rating:
Views: 9,880

Language: C++

Last Modified: November 25, 2006
Instructions: Compile. not written by me, i came across it while learning how to program with Winsock.

Snippet


  1. /***********************************************************************
  2. main.cpp - The main() routine for all the "Basic Winsock" suite of
  3.     programs from the Winsock Programmer's FAQ.  This function parses
  4.     the command line, starts up Winsock, and calls an external function
  5.     called DoWinsock to do the actual work.
  6. This program is hereby released into the public domain.  There is
  7. ABSOLUTELY NO WARRANTY WHATSOEVER for this product.  Caveat hacker.
  8. ***********************************************************************/
  9. /*
  10. Copied from Tangentsoft.net
  11. To </D.I.C.> by: Cipherence
  12. */
  13.  
  14. #include <winsock.h>
  15.  
  16. #include <stdlib.h>
  17. #include <iostream>
  18.  
  19. using namespace std;
  20.  
  21.  
  22. //// Prototypes ////////////////////////////////////////////////////////
  23.  
  24. extern int DoWinsock(const char* pcHost, int nPort);
  25.  
  26.  
  27. //// Constants /////////////////////////////////////////////////////////
  28.  
  29. // Default port to connect to on the server
  30. const int kDefaultServerPort = 4242;
  31.  
  32.  
  33. //// main //////////////////////////////////////////////////////////////
  34.  
  35. int main(int argc, char* argv[])
  36. {
  37.     // Do we have enough command line arguments?
  38.     if (argc < 2) {
  39.         cerr << "usage: " << argv[0] << " <server-address> " <<
  40.                 "[server-port]" << endl << endl;
  41.         cerr << "\tIf you don't pass server-port, it defaults to " <<
  42.                 kDefaultServerPort << "." << endl;
  43.         return 1;
  44.     }
  45.  
  46.     // Get host and (optionally) port from the command line
  47.     const char* pcHost = argv[1];
  48.     int nPort = kDefaultServerPort;
  49.     if (argc >= 3) {
  50.         nPort = atoi(argv[2]);
  51.     }
  52.  
  53.     // Do a little sanity checking because we're anal.
  54.     int nNumArgsIgnored = (argc - 3);
  55.     if (nNumArgsIgnored > 0) {
  56.         cerr << nNumArgsIgnored << " extra argument" <<
  57.                 (nNumArgsIgnored == 1 ? "" : "s") <<
  58.                 " ignored.  FYI." << endl;
  59.     }
  60.  
  61.     // Start Winsock up
  62.     WSAData wsaData;
  63.      int nCode;
  64.     if ((nCode = WSAStartup(MAKEWORD(1, 1), &wsaData)) != 0) {
  65.           cerr << "WSAStartup() returned error code " << nCode << "." <<
  66.                     endl;
  67.         return 255;
  68.     }
  69.  
  70.     // Call the main example routine.
  71.     int retval = DoWinsock(pcHost, nPort);
  72.  
  73.     // Shut Winsock back down and take off.
  74.     WSACleanup();
  75.     return retval;
  76. }
  77.  
  78.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month