win-socket
WIN32平台上的WINSOCK编程都要经过下列步骤: 定义变量->获得WINDOCK版本->加载WINSOCK库->初始化->创建套接字->设置套接字选项->关闭套接字->卸载WINSOCK库->释放资源 下面介绍WINSOCK C/S的建立过程: 服务器 客户端 1 初始化WSA 1 初始化WSA 2 建立一个SOCKET 2 建立一个SOCKET 3 绑定SOCKET 3 连接到服务器 4 在指定的端口监听 4 发送和接受数据 5 接受一个连接 5 断开连接 6 发送和接受数据 7 断开连接
VC中进行WINSOCK编程时,需要引入如下两个库文件:WINSOCK.H(这个是WINSOCK API的头文件, WIN2K以上支持WINSOCK2,所以可以用WINSOCK2.H);Ws2_32.lib(WINSOCK API连接库文件). 使用方式如下: #include <winsock.h> #pragma comment(lib,"ws2_32.lib")
//********************************************************* //函 数:WSAStartup(MAKEWORD(2,2), &wsaData) //参 数:MAKEWORD(2,2) --使用WINSOCK2版本 //参 数:WSADATA --WSADATA结构 //返回值:int --0:成功; 非0:失败 //********************************************************* 首先,建立一个WSADATA结构:WSADATA wsaData; 然后,调用WSAStartup函数,这个函数是连接应用程序与winsock.dll的第一个调用
void TCP() { WSADATA wsaData; int port = 5099; char buf[] = "Server: hello, I am a server....."; if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { printf("Failed to load Winsock"); return; } //创建用于监听的套接字 SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0); SOCKADDR_IN addrSrv; addrSrv.sin_family = AF_INET; addrSrv.sin_port = htons(port); //1024以上的端口号 addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); int retVal = bind(sockSrv, (LPSOCKADDR)&addrSrv, sizeof(SOCKADDR_IN)); if(retVal == SOCKET_ERROR){ printf("Failed bind:%d\n", WSAGetLastError()); return; } if(listen(sockSrv,10) ==SOCKET_ERROR){ printf("Listen failed:%d", WSAGetLastError()); return; } SOCKADDR_IN addrClient; int len = sizeof(SOCKADDR); while(1) { //等待客户请求到来 SOCKET sockConn = accept(sockSrv, (SOCKADDR *) &addrClient, &len); if(sockConn == SOCKET_ERROR){ printf("Accept failed:%d", WSAGetLastError()); break; } printf("Accept client IP:[%s]\n", inet_ntoa(addrClient.sin_addr)); //发送数据 int iSend = send(sockConn, buf, sizeof(buf) , 0); if(iSend == SOCKET_ERROR){ printf("send failed"); break; } char recvBuf[100]; memset(recvBuf, 0, sizeof(recvBuf)); // //接收数据 recv(sockConn, recvBuf, sizeof(recvBuf), 0); printf("%s\n", recvBuf); closesocket(sockConn); } closesocket(sockSrv); WSACleanup(); system("pause"); }
#include "stdafx.h" #include <WinSock2.h> #include <stdio.h> #pragma comment(lib, "ws2_32.lib") void Client() { //加载套接字 WSADATA wsaData; char buff[1024]; memset(buff, 0, sizeof(buff)); if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { printf("Failed to load Winsock"); return; } SOCKADDR_IN addrSrv; addrSrv.sin_family = AF_INET; addrSrv.sin_port = htons(5099); addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //创建套接字 SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0); if(SOCKET_ERROR == sockClient){ printf("Socket() error:%d", WSAGetLastError()); return; } //向服务器发出连接请求 if(connect(sockClient, (struct sockaddr*)&addrSrv, sizeof(addrSrv)) == INVALID_SOCKET){ printf("Connect failed:%d", WSAGetLastError()); return; }else { //接收数据 recv(sockClient, buff, sizeof(buff), 0); printf("%s\n", buff); } //发送数据 char buff2[1024] = "hello"; send(sockClient, buff2, sizeof(buff2), 0); //关闭套接字 closesocket(sockClient); WSACleanup(); system("pause"); }