繁体   English   中英

使用TCP套接字的C ++多客户端聊天

[英]C++ multi client chat using TCP socket

我试图使用TCP套接字在C ++中进行多客户端聊天。 我已经从该站点下载了套接字实现的源代码。

问题是当我尝试从客户端向服务器发送消息时,我从服务器接收到的“ ecko”是无尽的空格字符串。

我尝试调试客户端代码,客户端正确读取了输入。 在前几条消息中,服务器将其消息发送回客户端,但在几条消息后,客户端获得无尽的空间。 我试图使用memset来nullify(在所有数组中都置零),但更糟糕的是,服务器根本不接收消息。

希望能提供帮助(:

这是服务器端:

 #include "PracticalSocket.h"
#include <stdio.h>
#include <process.h>
#include <Windows.h>
using namespace std;
#pragma comment(lib,"ws2_32.lib")


TCPSocket* MyClients[20];
int ClientCount = 0;

 void connectCLient(void* pValue){  
    int nI,Flag;
    char st[1024];
    //memset(st,0,1024); // doing problems
    TCPSocket* pServerClient = (TCPSocket*)pValue;  
    MyClients[ClientCount] = pServerClient;
    ClientCount++;
    try{
        while (true)
        {
             Flag = pServerClient->recv(st,strlen(st));
             if(Flag>1){
                printf("%s\n",st);
                for(nI = 0; nI< ClientCount ; nI++){
                    MyClients[nI]->send(st,strlen(st)+1);
             }
        }


        }
    }
    catch(...){
        puts("one client lefttt");
    }
 }

int main(int argc, char* argv[])
{
    TCPServerSocket* pServer = new TCPServerSocket(8546);
    int nClientCounter = 0;
    printf("Start TCP Server ... on Port %d\n", 8546);

    try{
        while(true)
        {

            printf("Wait for new TCP Clients ... \n");
            TCPSocket* pClient = pServer->accept();     
            _beginthread(connectCLient,0,(void*)pClient);
            printf("Client %d Connected ... \n", ++nClientCounter);
        }
    }
    catch(...){
        puts("one client left");
    }

    return 0;
}

这是客户端:

#include "PracticalSocket.h"
#include <stdio.h>
#include <process.h>
#include <Windows.h>
using namespace std;
#pragma comment (lib, "ws2_32.lib")


void ReciveMessages(void * pValue ){
    char recvM[1024];
    TCPSocket* pClient = (TCPSocket*)pValue;
    while(true){
        pClient->recv(recvM,strlen(recvM));
        printf("%s\n",recvM);
    }
}


int main(int argc, char* argv[])
{
    try
    {
        TCPSocket * cClient = new TCPSocket();
        cClient->connect("127.0.0.1",8546);
        _beginthread(ReciveMessages,0,(void*)cClient);
        char st[1024];
        memset(st,0,1024);
        while(true)
        {

            printf("Press Text -->");
            fgets(st, sizeof st, stdin);
            cClient->send(st,strlen(st)+2); 
        }
    }
    catch(...)
    {
        printf("Socket Error..!");
        system("pause");//run cmd comment - stop the system
    }
    return 0;
}

代码中有一些错误:

MyClients[ClientCount] = pServerClient;
ClientCount++;

由于上述情况发生在不同的线程中,因此ClientCount++是非原子性的,并会导致争用条件。 使ClientCount原子化或在一个服务器线程中执行。


在:

Flag = pServerClient->recv(st,strlen(st));
if(Flag>1) {
    printf("%s\n",st);
    for(nI = 0; nI< ClientCount ; nI++)
        MyClients[nI]->send(st,strlen(st)+1);

st不能以\\0结尾,因为它可以是部分读取,因此strlen(st)返回错误的结果。 固定:

ssize_t received = pServerClient->recv(st, sizeof st - 1);
if(received > 0) {
    st[received] = 0; // Zero-terminate.        
    printf("%s\n", st);
    for(nI = 0; nI< ClientCount ; nI++)
        MyClients[nI]->send(st, received);

类似问题:

pClient->recv(recvM,strlen(recvM));
printf("%s\n",recvM);

固定:

ssize_t received = pClient->recv(recvM, sizeof recvM - 1);
if(received > 0) {
    recvM[received] = 0;
    printf("%s\n",recvM);
}

在:

 cClient->send(st,strlen(st)+2); 

发送零终止符毫无意义:

 cClient->send(st, strlen(st)); 

TCP是一种流协议,这意味着sendrecv可以发送/接收部分数据,并且没有消息边界。 您可能想分隔您的消息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM