⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 使用winsock库中socket套间实现TCP/IP固定连接的程序,工程是完整的,只要在VC环境下重新编译一下就可以了.
💻 CPP
字号:
/*********************************************************************** main.cpp - The main() routine for all the "Basic Winsock" suite of    programs from the Winsock Programmer's FAQ.  This function parses    the command line, starts up Winsock, and calls an external function    called DoWinsock to do the actual work. This program is hereby released into the public domain.  There is ABSOLUTELY NO WARRANTY WHATSOEVER for this product.  Caveat hacker.***********************************************************************/#include <winsock.h>#include <stdlib.h>#include <iostream>using namespace std;//// Prototypes ////////////////////////////////////////////////////////extern int DoWinsock(const char* pcHost, int nPort);//// Constants /////////////////////////////////////////////////////////// Default port to connect to on the serverconst int kDefaultServerPort = 4242;//// main //////////////////////////////////////////////////////////////int main(int argc, char* argv[]){    // Do we have enough command line arguments?    if (argc < 2) {        cerr << "usage: " << argv[0] << " <server-address> " <<                "[server-port]" << endl << endl;        cerr << "\tIf you don't pass server-port, it defaults to " <<                kDefaultServerPort << "." << endl;        return 1;    }    // Get host and (optionally) port from the command line    const char* pcHost = argv[1];    int nPort = kDefaultServerPort;    if (argc >= 3) {        nPort = atoi(argv[2]);    }    // Do a little sanity checking because we're anal.    int nNumArgsIgnored = (argc - 3);    if (nNumArgsIgnored > 0) {        cerr << nNumArgsIgnored << " extra argument" <<                (nNumArgsIgnored == 1 ? "" : "s") <<                 " ignored.  FYI." << endl;    }    // Start Winsock up    WSAData wsaData;	int nCode;    if ((nCode = WSAStartup(MAKEWORD(1, 1), &wsaData)) != 0) {		cerr << "WSAStartup() returned error code " << nCode << "." <<				endl;        return 255;    }    // Call the main example routine.    int retval = DoWinsock(pcHost, nPort);    // Shut Winsock back down and take off.    WSACleanup();    return retval;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -