笔记 字符集类型的错误 C2440 “static_cast”- 无法从“const char [13]”转换为“LPCTSTR”

这是一个使用C++和Winsock库编写的多线程TCP服务器示例,能够接受多个客户端连接并广播消息。代码中定义了一个结构体`client_type`来存储客户端的ID和套接字,并通过`process_client`函数处理每个客户端的通信。服务器在接收到新的客户端连接时,会创建一个新的线程来处理该连接,同时限制了最大客户端数量为5。当客户端断开连接时,会通知其他客户端。

1. 源码

server.cpp

#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string>
#include <thread>
#include <vector>

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

#define IP_ADDRESS "192.168.56.1"
#define DEFAULT_PORT "3504"
#define DEFAULT_BUFLEN 512

struct client_type {
   
   
    int id;
    SOCKET socket;
};

const char OPTION_VALUE = 1;
const int MAX_CLIENTS = 5;

//Function Prototypes
int process_client(client_type& new_client, std::vector<client_type>& client_array, std::thread& thread);
int main();

int process_client(client_type& new_client, std::vector<client_type>& client_array, std::thread& thread) {
   
   
    std::string msg = "";
    char tempmsg[DEFAULT_BUFLEN] = "";

    //Session
    while (1) {
   
   
        memset(tempmsg, 0, DEFAULT_BUFLEN);

        if (new_client.socket != 0) {
   
   
            int iResult = recv(new_client.socket, tempmsg, DEFAULT_BUFLEN, 0);

            if (iResult != SOCKET_ERROR) {
   
   
                if (strcmp("", tempmsg))
                    msg = "Client #" + std::to_string(new_client.id) + ": " + tempmsg;

                std::cout << msg.c_str(
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值