UDP通信
先启动server再启动client
//server #include <stdio.h> #include <winsock2.h> #pragma comment (lib, "ws2_32.lib") //加载 ws2_32.dll #define BUF_SIZE 100 int main(){ WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); //创建套接字 SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); //绑定套接字 struct sockaddr_in servAddr; memset(&servAddr, 0, sizeof(servAddr)); //每个字节都用0填充 servAddr.sin_family = PF_INET; //使用IPv4地址 servAddr.sin_addr.s_addr = htonl(INADDR_ANY); //自动获取IP地址 servAddr.sin_port = htons(1234); //端口 bind(sock, (SOCKADDR*)&servAddr, sizeof(SOCKADDR)); //接收客户端请求 SOCKADDR clntAddr; //客户端地址信息 int nSize = sizeof(SOCKADDR); char buffer[BUF_SIZE]; //缓冲区 while (1){ int strLen = recvfrom(sock, buffer, BUF_SIZE, 0, &clntAddr, &nSize); sendto(sock, buffer, strLen, 0, &clntAddr, nSize); } closesocket(sock); WSACleanup(); return 0; }
//client #include <stdio.h> #include <WinSock2.h> #pragma comment(lib, "ws2_32.lib") //加载 ws2_32.dll #define BUF_SIZE 100 int main(){ //初始化DLL WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); //创建套接字 SOCKET sock = socket(PF_INET, SOCK_DGRAM, 0); //服务器地址信息 struct sockaddr_in servAddr; memset(&servAddr, 0, sizeof(servAddr)); //每个字节都用0填充 servAddr.sin_family = PF_INET; servAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); servAddr.sin_port = htons(1234); //不断获取用户输入并发送给服务器,然后接受服务器数据 struct sockaddr fromAddr; int addrLen = sizeof(fromAddr); while (1){ char buffer[BUF_SIZE] = { 0 }; printf("Input a string: "); gets(buffer); sendto(sock, buffer, strlen(buffer), 0, (struct sockaddr*)&servAddr, sizeof(servAddr)); int strLen = recvfrom(sock, buffer, BUF_SIZE, 0, &fromAddr, &addrLen); buffer[strLen] = 0; printf("Message form server: %s\n", buffer); } closesocket(sock); WSACleanup(); return 0; }
先启动server再启动client
//server #include <stdio.h> #include <stdlib.h> #include <winsock2.h> #pragma comment (lib, "ws2_32.lib") //加载 ws2_32.dll #define BUF_SIZE 1024 int main(){ //先检查文件是否存在 char *filename = "D:\\OpenFlow.docx"; //文件名 FILE *fp = fopen(filename, "rb"); //以二进制方式打开文件 if (fp == NULL){ printf("Cannot open file, press any key to exit!\n"); system("pause"); exit(0); } WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); SOCKET servSock = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); sockAddr.sin_family = PF_INET; sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); sockAddr.sin_port = htons(1234); bind(servSock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR)); listen(servSock, 20); SOCKADDR clntAddr; int nSize = sizeof(SOCKADDR); SOCKET clntSock = accept(servSock, (SOCKADDR*)&clntAddr, &nSize); //循环发送数据,直到文件结尾 char buffer[BUF_SIZE] = { 0 }; //缓冲区 int nCount; while ((nCount = fread(buffer, 1, BUF_SIZE, fp)) > 0){ send(clntSock, buffer, nCount, 0); } shutdown(clntSock, SD_SEND); //文件读取完毕,断开输出流,向客户端发送FIN包 recv(clntSock, buffer, BUF_SIZE, 0); //阻塞,等待客户端接收完毕 fclose(fp); closesocket(clntSock); closesocket(servSock); WSACleanup(); system("pause"); return 0; }
文件传输
//client #include <stdio.h> #include <stdlib.h> #include <WinSock2.h> #pragma comment(lib, "ws2_32.lib") #define BUF_SIZE 1024 int main(){ //先输入文件名,看文件是否能创建成功 char filename[100] = { 0 }; //文件名 printf("Input filename to save: "); gets(filename); FILE *fp = fopen(filename, "wb"); //以二进制方式打开(创建)文件 if (fp == NULL){ printf("Cannot open file, press any key to exit!\n"); system("pause"); exit(0); } WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); struct sockaddr_in sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); sockAddr.sin_family = PF_INET; sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); sockAddr.sin_port = htons(1234); connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR)); //循环接收数据,直到文件传输完毕 char buffer[BUF_SIZE] = { 0 }; //文件缓冲区 int nCount; while ((nCount = recv(sock, buffer, BUF_SIZE, 0)) > 0){ fwrite(buffer, nCount, 1, fp); } puts("File transfer success!"); //文件接收完毕后直接关闭套接字,无需调用shutdown() fclose(fp); closesocket(sock); WSACleanup(); system("pause"); return 0; }
Windows操作系统之Socket通信
先启动server再启动client
//server.cpp #include <stdio.h> #include <winsock2.h> #pragma comment (lib, "ws2_32.lib") //加载 ws2_32.dll #define BUF_SIZE 100 int main(){ WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); //创建套接字 SOCKET servSock = socket(AF_INET, SOCK_STREAM, 0); //绑定套接字 struct sockaddr_in sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); //每个字节都用0填充 sockAddr.sin_family = PF_INET; //使用IPv4地址 sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址 sockAddr.sin_port = htons(1234); //端口 bind(servSock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR)); //进入监听状态 listen(servSock, 20); //接收客户端请求 SOCKADDR clntAddr; int nSize = sizeof(SOCKADDR); char buffer[BUF_SIZE] = { 0 }; //缓冲区 while (1){ SOCKET clntSock = accept(servSock, (SOCKADDR*)&clntAddr, &nSize); int strLen = recv(clntSock, buffer, BUF_SIZE, 0); //接收客户端发来的数据 send(clntSock, buffer, strLen, 0); //将数据原样返回 closesocket(clntSock); //关闭套接字 memset(buffer, 0, BUF_SIZE); //重置缓冲区 } //关闭套接字 closesocket(servSock); //终止 DLL 的使用 WSACleanup(); return 0; }
//client.cpp #include <stdio.h> #include <WinSock2.h> #include <windows.h> #pragma comment(lib, "ws2_32.lib") //加载 ws2_32.dll #define BUF_SIZE 100 int main(){ //初始化DLL WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); //向服务器发起请求 struct sockaddr_in sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); //每个字节都用0填充 sockAddr.sin_family = PF_INET; sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); sockAddr.sin_port = htons(1234); char bufSend[BUF_SIZE] = { 0 }; char bufRecv[BUF_SIZE] = { 0 }; while (1){ //创建套接字 SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR)); //获取用户输入的字符串并发送给服务器 printf("Input a string: "); gets_s(bufSend); send(sock, bufSend, strlen(bufSend), 0); //接收服务器传回的数据 recv(sock, bufRecv, BUF_SIZE, 0); //输出接收到的数据 printf("Message form server: %s\n", bufRecv); memset(bufSend, 0, BUF_SIZE); //重置缓冲区 memset(bufRecv, 0, BUF_SIZE); //重置缓冲区 closesocket(sock); //关闭套接字 } WSACleanup(); //终止使用 DLL return 0; }