赞
踩
创作不易 觉得有帮助请点赞关注收藏
TCP/IP是当下网络协议栈中的主流协议 TCP属于传输层的协议 可靠传输 包括经典的三次握手等等
IP协议是网络层协议 尽全力传输但不可靠 学过计算机网络的同学们对这个应该比较熟悉
以下是使用C++进行网络通信的实例
服务端
主要使用多线程技术建立连接 也就是一个服务端可以连接多个客户端 最大连接数可以限定
向客户端发送字符串this is server 并启动一个线程等待客户端发送信息过来
如果客户端发送A过来 服务器返回B 发送C过来 服务器返回D 发送exit之后服务器关闭线程
效果图如下
代码如下
- #include<iostream>
- #include<stdlib.h>
- #include<WinSock2.h>
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
-
- using namespace std;
- #pragma comment(lib, "ws2_32.lib")
- DWORD WINAPI threadpro(LPVOID pParam) {
- SOCKET hsock = (SOCKET)pParam;
- char buffer[1024];
- char sendBuffer[1024];
- if (hsock != INVALID_SOCKET)
-
- cout << "start receive" << endl;
- while (1)
- {
- int num = recv(hsock, buffer, 1024, 0);
- if (num >= 0)
- cout << "receive from clinet" << buffer << endl;
- if (!strcmp(buffer, "A"))
- {
- memset(sendBuffer, 0, 1024);
- strcpy_s(sendBuffer, "B");
- int ires = send(hsock, sendBuffer, sizeof(sendBuffer), 0);//回送信息
- cout << "send to client" << sendBuffer << endl;
-
-
- }
- else if (!strcmp(buffer, "C"))
- {
- memset(sendBuffer, 0, 1024);
- strcpy_s(sendBuffer, "D");
- int ires = send(hsock, sendBuffer, sizeof(sendBuffer), 0);//回送信息
- cout << "send to client" << sendBuffer << endl;
-
- }
- else if (!strcmp(buffer, "exit"))
- {
-
- cout << "client close" << endl;
- cout << "server process close" << endl;
- return 0;
- }
- else
- {
- memset(sendBuffer, 0, 1024);
- strcpy_s(sendBuffer, "ERR");
- int ires = send(hsock, sendBuffer, sizeof(sendBuffer), 0);//回送信息
- cout << "send to client" << sendBuffer << endl;
-
-
- }
-
- }
- return 0;
-
-
- }
- void main() {
- WSADATA wsd;
- WSAStartup(MAKEWORD(2, 2), &wsd);
- SOCKET m_SockServer;
- sockaddr_in serveraddr;
- sockaddr_in serveraddrfrom;
- SOCKET m_Server[20];
- serveraddr.sin_family = AF_INET;
- serveraddr.sin_port = htons(4600);//端口号
- //serveraddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
- m_SockServer = socket(AF_INET, SOCK_STREAM, 0);
- int i = bind(m_SockServer, (sockaddr*)&serveraddr, sizeof(serveraddr));//绑定数
- cout << "bind:" << i << endl;
- int imaxconnect = 20;
- int iconnect = 0;
- int ilisret;
- char buf[] = "this is server\0";
- char WarnBuf[] = "it is over max connect\0";
- int len = sizeof(sockaddr);
- while (1) {
- ilisret = listen(m_SockServer, 0);
- m_Server[iconnect] = accept(m_SockServer, (sockaddr*)&serveraddrfrom, &len);
- if (m_Server[iconnect] != INVALID_SOCKET)
- {
- int ires = send(m_Server[iconnect], buf, sizeof(buf), 0);
- cout << "accept:" << ires << endl;
- iconnect++;
- if (iconnect > imaxconnect)
- {
- int ires = send(m_Server[iconnect], WarnBuf, sizeof(WarnBuf), 0);
-
- }
- else
- {
- HANDLE m_handle;
- DWORD nThreadld = 0;
- m_handle = (HANDLE)::CreateThread(NULL, 0, threadpro, (LPVOID)m_Server[--iconnect], 0, &nThreadld);//启动 线程
-
-
-
- }
-
-
-
-
- }
-
-
- WSACleanup();
- }
-
- }
客户端
- #include<iostream>
- #include<stdlib.h>
- #include"winsock2.h"
- #include<time.h>
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- using namespace std;
- #pragma comment(lib,"ws2_32.lib")
- void main() {
- WSADATA wsd;
- WSAStartup(MAKEWORD(2, 2), &wsd);
- SOCKET m_SockClient;
- sockaddr_in clientaddr;
- clientaddr.sin_family = AF_INET;
- clientaddr.sin_port = htons(4600);
- //clientaddr.sin_addr.S_un.S_addr =inet_addr("127.0.0.1");
- m_SockClient = socket(AF_INET, SOCK_STREAM, 0);
- int i = connect(m_SockClient, (sockaddr*)&clientaddr, sizeof(clientaddr));
- cout << "connect" << i << endl;
- char buffer[1024];
- char inBuf[1024];
- int num;
- num = recv(m_SockClient, buffer, 1024, 0);
- if (num > 0) {
- cout << "receive from server" << buffer << endl;
- while (1) {
- num = 0;
- cin >> inBuf;
- if (!strcmp(inBuf, "exit")) {
-
- send(m_SockClient, inBuf, sizeof(inBuf), 0);
- return;
-
- }
- send(m_SockClient, inBuf, sizeof(inBuf), 0);
- num = recv(m_SockClient, buffer, 1024, 0);
- if (num >= 0)
- cout << "reveive from server" << buffer << endl;
-
-
-
-
-
- }
-
-
-
-
-
-
-
-
- }
-
-
-
-
-
-
-
-
- }
创作不易 觉得有帮助请点赞关注收藏
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。