windows_tcp简单代码

文章内容:
一个简单的显示windows平台下c++tcp的代码

  1. 客户端代码已上传
  2. 服务器代码未验证
  3. 方便自己日后使用
客户端:
#if _MSC_VER >=1600 //VS2010版本号是1600
    #pragma execution_character_set("utf-8")
#endif
#include "mynetdump.h"

using namespace std;

MyNetDump::MyNetDump(QObject *parent): QThread(parent)
{

}

MyNetDump::~MyNetDump()
{
    closesocket(clientSocket);
    WSACleanup();
    this->wait();
}

void MyNetDump::InitWincap(QString IP, int port, int ui_count)
{
    m_count = ui_count;
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        qDebug() << "Failed to initialize winsock";
        return;
    }

    clientSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (clientSocket == INVALID_SOCKET)
    {
        qDebug() << "Failed to create socket";
        return;
    }

    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(port);
    serverAddress.sin_addr.s_addr = inet_addr(IP.toStdString().c_str());

    // // 设置连接超时时间
    // int timeout = 5000; // 5秒
    // setsockopt(clientSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
    // setsockopt(clientSocket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));

    if (::connect(clientSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR)
    {
        qDebug() << "Failed to connect to server";
        return;
    }

    this->start();
}

void MyNetDump::run()
{
    while(isRunning())
    {
        char buffer[4096];
        memset(buffer, 0, sizeof(buffer));

        int len = recv(clientSocket, buffer, sizeof(buffer), 0);
        if (len == SOCKET_ERROR)
        {
            qDebug() << "Failed to receive data from server";
            return;
        }

        QString str = QString(QLatin1String(buffer));
        emit sigRecvMsg(str, m_count);
        msleep(5);
    }
}

服务器代码
#include 
#include 

#pragma comment(lib, "ws2_32.lib")

int main() {
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        std::cout << "Failed to initialize winsock" << std::endl;
        return -1;
    }

    SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (serverSocket == INVALID_SOCKET) {
        std::cout << "Failed to create socket" << std::endl;
        return -1;
    }

    sockaddr_in serverAddress;
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(8080);
    serverAddress.sin_addr.s_addr = INADDR_ANY;

    if (bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) {
        std::cout << "Failed to bind socket" << std::endl;
        return -1;
    }

    if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {
        std::cout << "Failed to listen on socket" << std::endl;
        return -1;
    }

    std::cout << "Waiting for incoming connections..." << std::endl;

    SOCKET clientSocket;
    sockaddr_in clientAddress;
    int clientAddressSize = sizeof(clientAddress);

    while ((clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddress, &clientAddressSize)) != INVALID_SOCKET) {
        std::cout << "Client connected" << std::endl;

        char buffer[4096];
        memset(buffer, 0, sizeof(buffer));

        if (recv(clientSocket, buffer, sizeof(buffer), 0) == SOCKET_ERROR) {
            std::cout << "Failed to receive data from client" << std::endl;
            break;
        }

        std::cout << "Received data from client: " << buffer << std::endl;

        const char* response = "Hello from server!";
        if (send(clientSocket, response, strlen(response), 0) == SOCKET_ERROR) {
            std::cout << "Failed to send data to client" << std::endl;
            break;
        }

        closesocket(clientSocket);
        std::cout << "Client disconnected" << std::endl;
    }

    closesocket(serverSocket);
    WSACleanup();

    return 0;
}

你可能感兴趣的:(c++,windows,tcp/ip,网络协议)