C++ Socket TCP "Hello World!"
这是C++ SOCKET网络程序中的C/S结构之TCP "Hello World !",共两个控制台工程:
- //////////////////////////////////////////////////////////////////////////
- // TCPServer.cpp
- #include <stdio.h>
- #include <WINSOCK2.H>
- #pragma comment(lib,"WS2_32.lib")
- #define BUF_SIZE 64 // 缓冲区大小
- int main(void)
- {
- WSADATA wsd; // WSADATA变量
- SOCKET sServer; // 服务器套接字
- SOCKET sClient; // 客户端套接字
- int retVal; // 返回值
- char buf[BUF_SIZE]; // 接受数据
- // 初始化套接字动态库
- ZeroMemory(buf,BUF_SIZE);
- if(WSAStartup(MAKEWORD(2,2),&wsd) != 0)
- {
- printf("WSAStartup failed !/n");
- return 1;
- }
- // 创建套接字
- sServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
- if(INVALID_SOCKET == sServer)
- {
- printf("socket failed !/n");
- WSACleanup();
- return -1;
- }
- // 服务器套接字地址
- SOCKADDR_IN addrServ;
- addrServ.sin_family = AF_INET;
- addrServ.sin_port = htons(4999);
- addrServ.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
- // 绑定套接字
- retVal = bind(sServer,(const struct sockaddr*)&addrServ,sizeof(SOCKADDR_IN));
- if(SOCKET_ERROR == retVal)
- {
- printf("bind failed !/n");
- closesocket(sServer);
- WSACleanup();
- return -1;
- }
- // 监听套接字
- retVal = listen(sServer,1);
- if(SOCKET_ERROR == retVal)
- {
- printf("listen failed !/n");
- closesocket(sServer);
- WSACleanup();
- return -1;
- }
- // 接受客户请求
- sockaddr_in addrClient;
- int addrClientlen = sizeof(addrClient);
- sClient = accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen);
- if(INVALID_SOCKET == sClient)
- {
- printf("accept failed !/n");
- closesocket(sServer);
- WSACleanup();
- return -1;
- }
- // 接受客户数据
- retVal = recv(sClient,buf,BUFSIZ,0);
- if(SOCKET_ERROR == retVal)
- {
- printf("recv failed !/n");
- closesocket(sServer);
- closesocket(sClient);
- WSACleanup();
- return -1;
- }
- // 退出程序
- printf("Recv From Client:%s/n",buf);
- retVal = send(sClient,"Hello World !",strlen("Hello World !"),0);
- if(SOCKET_ERROR == retVal)
- {
- printf("send failed !/n");
- closesocket(sServer);
- closesocket(sClient);
- WSACleanup();
- return -1;
- }
- // 退出程序
- closesocket(sServer);
- closesocket(sClient);
- WSACleanup();
- return 0;
- }
- //////////////////////////////////////////////////////////////////////////
- // TCPClient.cpp
- #include <stdio.h>
- #include <Winsock2.H>
- #pragma comment(lib,"WS2_32.lib")
- #define BUF_SIZE 64 // 缓冲区大小
- int main(void)
- {
- WSADATA wsd; // 初始化<a href="http://lib.csdn.net/base/14" class='replace_word' title="MySQL知识库" target='_blank' style='color:#df3434; font-weight:bold;'>数据库</a>
- SOCKET sHost; // 服务器套接字
- SOCKADDR_IN servAddr; // 服务器地址
- char buf[BUF_SIZE]; // 接受数据缓冲区
- int retVal; // 返回值
- if(WSAStartup(MAKEWORD(2,2),&wsd) != 0)
- {
- printf("WSAStartup failed !/n");
- return 1;
- }
- // 创建套接字
- sHost = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
- if(INVALID_SOCKET == sHost)
- {
- printf("socket failed !/n");
- WSACleanup();
- return -1;
- }
- // 设置服务器地址
- servAddr.sin_family = AF_INET;
- servAddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.254");
- servAddr.sin_port = htons(4999);
- int sServerAddlen = sizeof(servAddr);
- // 连接服务器
- retVal = connect(sHost,(LPSOCKADDR)&servAddr,sizeof(servAddr));
- if(SOCKET_ERROR == retVal)
- {
- printf("connect failed !/n");
- closesocket(sHost);
- WSACleanup();
- }
- // 向服务器发送数据
- ZeroMemory(buf,BUF_SIZE);
- strcpy(buf,"Hello World !");
- retVal = send(sHost,buf,strlen(buf),0);
- if(SOCKET_ERROR == retVal)
- {
- printf("send failed !/n");
- closesocket(sHost);
- WSACleanup();
- return -1;
- }
- // 向服务器接受数据
- retVal = recv(sHost,buf,sizeof(buf)+1,0);
- printf("Recv From Server: %s/n",buf);
- // 退出程序
- closesocket(sHost);
- WSACleanup();
- return 0;
- }
程序运行效果:
源代码下载地址:
http://download.csdn.net/source/1667113
http://www.rayfile.com/files/79dbe03a-a1fe-11de-83dd-0014221b798a/
http://blog.csdn.net/wangningyu/article/details/4556327