TCP通信机制,采用全双工(就是既可以发送数据,也可以接收数据)的形式进行客户端和服务器之间的链接,并且这是一种可靠数据传输(数据在进行收发的过程中并不会进行损失),并非UDP那样,详细细节,为什么可靠传输,可以参考《计算机网络——自顶向下》,以下代码均采用c++的模式进行实现
1.TCP------Server
#pragma warning(disable:4996); #include <iostream> #include<winsock2.h> #include<windows.h> using namespace std; #pragma comment(lib,"ws2_32.lib") class TCP { public: void check_version(); void intintail(); void blind_listen(); void sent_recv(); private: WSADATA data; SOCKET sock_server; SOCKET sock_client; SOCKADDR_IN add_server; SOCKADDR_IN add_client; }; int main() { TCP a; a.check_version(); a.intintail(); a.blind_listen(); a.sent_recv(); } void TCP::check_version() { // int ret= WSAStartup(MAKEWORD(2,2),&data); if (WSAStartup(MAKEWORD(2,2),&data)!=0) { cout << "套接字的版本错误" << endl; WSACleanup(); } else { cout << "套接字版本正确" << endl; } } void TCP::intintail() { //套接字接口的配置 sock_server = socket(AF_INET, SOCK_STREAM, 0); //配置地址的设置 add_server.sin_family = AF_INET; add_server.sin_port = htons(); add_server.sin_addr.S_un.S_addr =inet_addr("");//填写一个Ip地址
} void TCP::blind_listen() { int n = sizeof(SOCKADDR_IN); int ret = bind(sock_server, (LPSOCKADDR)&add_server, n); if (ret==SOCKET_ERROR) { cout << "套接字绑定失败" << endl; WSACleanup(); } else { cout << "套接字绑定成功" << endl; } //开始监听套接字 int ret2 = listen(sock_server, 10);//第二个参数决定未完成队列和已完成队列中连接数目之和的最大值 if (ret2 == SOCKET_ERROR) { cout << "监听失败" << endl; WSACleanup(); } else { cout << "监听成功" << endl; } } void TCP::sent_recv() { cout << "请稍等。。。" << "\t" << "正在准备链接客户端" << endl; Sleep(3000); while (true) { int len = sizeof(SOCKADDR); //首先开始链接客户端 sock_client = accept(sock_server, (LPSOCKADDR)&add_client, &len); if (sock_client == SOCKET_ERROR) { cout << "链接失败" << endl; WSACleanup(); break; } else { cout << "链接成功" << endl; } //进行数据的收发 char ser[100] = "hello client"; if (send(sock_client, ser,100, 0) == SOCKET_ERROR) { cout << "发送失败" << endl; WSACleanup(); break; } else { cout << "发送成功" << endl; } } }
2TCP-----Client
#pragma warning(disable:4996); #include <iostream> using namespace std; #include<winsock2.h> #pragma comment(lib,"ws2_32.lib") #include<windows.h> class TCP { public: void intalize(); void my_connect(); void my_recv(); private: SOCKET sock_client; SOCKADDR_IN addr_server; SOCKADDR_IN client_ADDR; WSADATA data; public:char rec[100];//接受并且进行打印的数组 int len; }; int main() { TCP a; a.intalize(); a.my_connect(); a.my_recv(); } void TCP::intalize() { if (WSAStartup(MAKEWORD(2, 2), &data) != 0) { cout << "套接字的版本错误" << endl; WSACleanup(); } else { cout << "套接字版本正确" << endl; } memset(&addr_server, 0, sizeof(addr_server)); sock_client=socket(AF_INET, SOCK_STREAM, 0); if (sock_client == INVALID_SOCKET) { cout << "设置失败" << endl; WSACleanup(); } else { cout << "设置成功" << endl; } addr_server.sin_family = AF_INET; addr_server.sin_port = htons();//选择一个电脑的任意端口 addr_server.sin_addr.S_un.S_addr =inet_addr("");//填写一个Ip地址 len = sizeof(SOCKADDR_IN); } void TCP::my_connect() { if (connect(sock_client,(LPSOCKADDR)&addr_server,len)==SOCKET_ERROR) { cout << "连接失败" << endl; WSACleanup(); } else { cout << "连接成功" << endl; } } void TCP::my_recv() { if (recv(sock_client,rec,1,0)==SOCKET_ERROR) { cout << "接收失败" << endl; WSACleanup(); } else { cout << "接收成功" << endl; cout << "服务器的信息是:" << rec << endl; } }
【remark】
对于inet_addr()的调用,在vs2017 和vs2019中若不加上#pragma warning(disable:4996);都会因为版本的问题进行报错
来源:https://www.cnblogs.com/dfxdd/p/12221939.html