客户端
1 #define _CRT_SECURE_NO_WARNINGS 2 #include3 #include 4 #include 5 //网络通信 6 #include 7 #pragma comment(lib,"ws2_32.lib") 8 9 void main()10 {11 //发送的消息12 char sendbuf[1024] = { 0 };13 //收到的消息14 char receivebuf[1024] = { 0 };15 //发送的长度16 int sendlength = 0;17 //接收的长度18 int receivelength = 0;19 20 //通信套接字,用于创建TCP连接21 SOCKET socket_send;22 //创建服务器端结构体保存ip地址以及端口号23 SOCKADDR_IN sever_addr;24 25 //版本,2,2版本26 WORD wversion = MAKEWORD(2, 2);27 //版本28 WSADATA wsadata;29 //获取版本对比,开始通信30 int error = WSAStartup(wversion, &wsadata);31 if (error != 0)32 {33 printf("error");34 return;35 }36 //取出低八位,高八位,对比版本37 if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2)38 {39 printf(" version error");40 return;41 }42 //设置通信模式43 sever_addr.sin_family = AF_INET;44 sever_addr.sin_addr.S_un.S_addr = inet_addr("192.168.0.104");45 //端口46 sever_addr.sin_port = htons(9982);47 48 //创建tcp通信49 socket_send = socket(AF_INET, SOCK_STREAM, 0);50 51 //通过套接字连接到服务器52 connect(socket_send, (SOCKADDR*)&sever_addr, sizeof(SOCKADDR));53 54 FILE *pfr = fopen("1.txt", "rb");55 56 57 while (!feof(pfr))58 {59 fgets(sendbuf, 1024, pfr);60 //通过套接字发送到服务器61 sendlength = send(socket_send, sendbuf, 1024, 0);62 memset(receivebuf, 0, 1024);//清零63 }64 65 66 //关闭网络67 closesocket(socket_send);68 WSACleanup();//关闭69 system("pause");70 }
服务器端
1 #define _CRT_SECURE_NO_WARNINGS 2 #include3 #include 4 #include 5 //网络通信 6 #include 7 #pragma comment(lib,"ws2_32.lib") 8 9 void main()10 {11 //发送的消息12 char sendbuf[1024] = { 0 };13 //收到的消息14 char receivebuf[1024] = { 0 };15 //发送的长度16 int sendlength = 0;17 //收到的长度18 int receivelength = 0;19 // SOCKADDR结构体长度20 int length;21 22 //服务器套接字23 SOCKET socket_server;24 //连接的套接字25 SOCKET sorket_receive;26 27 //服务器结构体信息28 SOCKADDR_IN sever_addr;29 //客户端结构体信息30 SOCKADDR_IN client_addr;31 32 //版本,2,2版本33 WORD wversion = MAKEWORD(2, 2);34 //当前版本35 WSADATA wsadata;36 //获取版本对比,开始通信37 int error = WSAStartup(wversion, &wsadata);38 if (error != 0)39 {40 printf("error");41 return;42 }43 //取出低八位,高八位,对比版本44 if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2)45 {46 printf(" version error");47 return;48 }49 50 //设置服务器结构体信息51 sever_addr.sin_family = AF_INET;//通信52 sever_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//获取本地址53 sever_addr.sin_port = htons(9982);//端口54 55 //创建服务器套接字,接受消息 SOCK_STREAM流56 socket_server = socket(AF_INET, SOCK_STREAM, 0);//创建57 //把套接字与服务器结构体绑定 (绑定的是地址,端口)58 bind(socket_server, (SOCKADDR*)&sever_addr, sizeof(SOCKADDR));59 //服务器套接字监听60 listen(socket_server, 5);//最大等待数目为561 length = sizeof(SOCKADDR);62 //通过服务器套接字接受连接,初始化套接字sorket_receive,把连接者的信息存放在结构体client_addr中63 sorket_receive = accept(socket_server, (SOCKADDR*)&client_addr, &length);64 65 FILE *pfw = fopen("2.txt", "wb");66 int flag = 0;//判断是否文件传输完67 while (1)68 {69 //接收数据70 receivelength = recv(sorket_receive, receivebuf, 1024, 0);71 if (strlen(receivebuf) > 0)72 {73 flag = 1;74 printf("客户端说:%s\n", receivebuf);75 fputs(receivebuf, pfw);76 memset(receivebuf, 0, 1024);//清零77 }78 else79 {80 if (flag == 1)81 {82 printf("传输完成\n");83 break;84 }85 }86 }87 88 //释放网络89 closesocket(sorket_receive);//关闭网络90 closesocket(socket_server);91 WSACleanup();//关闭92 system("pause");93 }