实现的编译器是 Visual Studio 2010, 之前用的2022太新,一些头文件用不了
写的过程中对数据在传输层的通讯 ACK TCP 协议都有了新的理解
服务端和客户端分开两个project:运行注意事项:
// Server.cpp : create a console application, and include the sources in the project
//
// 1. open the *.c in the Visual C++, then “rebuild all”.
// 2. click “yes” to create a project workspace.
// 3. You need to -add the library ‘ws2_32.lib’ to your project
// (Project -> Properties -> Linker -> Input -> Additional Dependencies)
// 4. recompile the source.
代码
Server:
// You need to -add the library ‘ws2_32.lib’ to your project
#include “stdafx.h”
#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define DEFAULT_PORT 5019//持续监听5019端口
int main(int argc, char **argv){
char szBuff[100];//The information of the message need to transimit
int msg_len;//the length of the message
int addr_len;//the length of the address
struct sockaddr_in local, client_addr;
SOCKET sock, msg_sock;
WSADATA wsaData;
//514
if (WSAStartup(0x202, &wsaData) == SOCKET_ERROR){
// stderr: standard error are printed to the screen.
fprintf(stderr, "WSAStartup failed with error %d\n", WSAGetLastError());
//WSACleanup function terminates use of the Windows Sockets DLL.
WSACleanup();
return -1;
}
// Fill in the address structur