Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:40:57+00:00 2026-06-17T20:40:57+00:00

ANSWER: (because i can’t answer my questiong after 7hours :/) ok, i solved it

  • 0

ANSWER: (because i can’t answer my questiong after 7hours :/)
ok, i solved it with this modification:

struct RecvDataModel
{
    int sockAddr;
    string inData; //old: char *inData;
};

void Client::Recv(int sockAddr, char *inData)
{
    cout << inData << endl;
    RecvDataModel *outData = new RecvDataModel();
    outData->sockAddr = sockAddr;
    outData->inData = string(inData);//old outData->inData = inData;
    pthread_t rThr;
    pthread_create(&rThr, NULL, ProcessData, outData);
}

i think string automatically fixing char* format, otherwise i can’t understand nothing!

and now i wanna ask: string and char performance are the same? is there a problem using string instead of char*(excepting pointers-void*-)?

QUESTION:
-inData and sockAddr came from a socket, this is a multiclient chat server- in here, i can send sockAddr to ProcessData but inData can’t send! it changes with broken format (like “testDataTextÿñ€ÿñ:¥øv:Y” i sent “testDataText” and it changed). i have tried to create an other char* and copy all in with for loop but at this time if i send “te%s;t” like data, again changes with broken format. what can i do?

struct RecvDataModel
{
    int sockAddr;
    char *inData;
};

void *ProcessData(void *arg);

void Client::Recv(int sockAddr, char *inData)
{
    RecvDataModel * outData = new RecvDataModel();
    outData->sockAddr = sockAddr;
    outData->inData = inData;
    pthread_t rThr;
    pthread_create(&rThr, NULL, ProcessData, outData);
}

void *ProcessData(void *arg)
{
    RecvDataModel *inData = (RecvDataModel*)arg;
    cout << inData->inData << endl;
    return 0;
}

inData and sockAddr came from here(Client Class):

#include <winsock2.h>

class Client
{
private:
    int mySock;
    int sockAddr;
    bool logged;
    void *listen(void)
    {
        int numBytes;
        char buffer[5120];
        while(1)
        {
            numBytes = recv(mySock, buffer, 5120, 0);
            if(numBytes == 0 || numBytes == -1)
            {
                Drop(mySock);
                mySock = 0;
                sockAddr = 0;
                return 0;
            }
            Recv(sockAddr, buffer);
            memset(buffer, 0, sizeof buffer);
        }
        return 0;
    }
public:
    void SetSock(int sock)
    {
        mySock = sock;
    }
    void SetSockAddr(int addr)
    {
        sockAddr = addr;
    }
    void Logged(bool status)
    {
        logged = status;
    }
    int GetSock()
    {
        return mySock;
    }
    int GetSockAddr()
    {
        return sockAddr;
    }
    bool IsLogged()
    {
        return logged;
    }
    static void *Listen(void *arg)
    {
        return ((Client*)arg)->listen();
    }
    static void Recv(int sockAddr, char *inData);
    static void Drop(int sockAddr);
};

and all of main.cpp

#define PORT 9696
#define MAXCONN 9999
#define BACKLOG 128

#include <winsock2.h>
#include <pthread.h>
#include <iostream>
#include "Client.cpp"

using namespace std;

struct RecvDataModel
{
    int sockAddr;
    char *inData;
};

Client m_Clients[MAXCONN];

void *AcceptClients(void *arg);
void *HandleClient(void *arg);
void *DropClient(void *arg);
void *ProcessData(void *arg);

void Client::Recv(int sockAddr, char *inData)
{
    cout << inData << endl;
    RecvDataModel *outData = new RecvDataModel();
    outData->sockAddr = sockAddr;
    outData->inData = inData;
    pthread_t rThr;
    pthread_create(&rThr, NULL, ProcessData, outData);
}

void Client::Drop(int sockAddr)
{
    pthread_t dThr;
    pthread_create(&dThr, NULL, DropClient, (void*)sockAddr);
}

int main()
{
    /* WinSock initialization::START */
    WSADATA wsaData;
    if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
    {
        cout << "WSA initialization failed!";
        WSACleanup();
        return 1;
    }
    /* WinSock initialization::END */
    SOCKET m_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    //--> TCP Socket: socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
    //--> UDP Socket: socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
    if(m_Socket == INVALID_SOCKET)
    {
        cout << "Cannot create server socket!";
        WSACleanup();
        return 1;
    }
    sockaddr_in m_Inf;
    m_Inf.sin_family = AF_INET;
    m_Inf.sin_port = htons(PORT);
    m_Inf.sin_addr.s_addr = INADDR_ANY;
    if(bind(m_Socket, (sockaddr*)(&m_Inf), sizeof(m_Inf)) == SOCKET_ERROR)
    {
        cout << "Cannot bind server socket!";
        WSACleanup();
        return 1;
    }
    if(listen(m_Socket, BACKLOG) == SOCKET_ERROR)
    {
        cout << "Server socket cannot start listening!";
        WSACleanup();
        return 1;
    }

    for(int i = 0; i < MAXCONN; i++)
    {
        m_Clients[i].SetSock(0);
    }

    cout << "Listening for connections..." << endl;
    pthread_t mThr;
    pthread_create(&mThr, NULL, AcceptClients, (void*)m_Socket);
    cin.ignore();
    cin.get();
    cin.clear();
    WSACleanup();
    return 0;
}

void *AcceptClients(void *arg)
{
    int m_Socket = (int)arg;
    while(1)
    {
        sockaddr_in Sin;
        int SinLen = sizeof(Sin);
        SOCKET c_Socket = accept(m_Socket, (sockaddr*)(&Sin), &SinLen);
        if(c_Socket == INVALID_SOCKET)
        {
            cout << "A connection initialized but dropped! (invalid socket)" << endl;
        }
        else
        {
            pthread_t hThr;
            pthread_create(&hThr, NULL, HandleClient, (void*)c_Socket);
        }
    }
    return 0;
}

void *HandleClient(void *arg)
{
    int inSock = (int)arg;
    bool avaibleFound = false;
    int lastLooked = 0;
    int avaibleAddr = -1;
    while(!avaibleFound)
    {
        avaibleFound = true;
        if(lastLooked != MAXCONN)
        {
            if(m_Clients[lastLooked].GetSock() == 0)
            {
                avaibleAddr = lastLooked;
                avaibleFound = true;
            }
            else
            {
                lastLooked += 1;
                avaibleFound = false;
            }
        }
        else
        {
            avaibleAddr = -1;
            avaibleFound = true;
        }
    }
    if(avaibleAddr != -1)
    {
        m_Clients[avaibleAddr].SetSockAddr(avaibleAddr);
        m_Clients[avaibleAddr].SetSock(inSock);
        cout << "Socket(#" << inSock << ") connected." << endl;
        pthread_t cThr;
        pthread_create(&cThr, NULL, &Client::Listen, (void*)&m_Clients[avaibleAddr]);
    }
    else
    {
        send(inSock, "11", 2, 0);//11: Server is full!
        closesocket(inSock);
    }
    return 0;
}

void *DropClient(void *arg)
{
    int inSock = (int)arg;
    cout << "Socket(#" << inSock << ") disconnected." << endl;
    return 0;
}

void *ProcessData(void *arg)
{
    RecvDataModel *inData = (RecvDataModel*)arg;
    cout << inData->sockAddr << inData->inData << endl;
    delete inData;
    return 0;
}

i have already post a question like this but nobody answered, this is very important for me sorry :L

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:40:59+00:00Added an answer on June 17, 2026 at 8:40 pm

    Add the following into your listen function just before the call to Recv:

    buffer[numBytes]=0;
    

    This way, you’ll be sure that the string you’re trying to print is null-terminated, and you’ll see no garbage coming from the uninitialized buffer for the first time (yes, you memset it later, but it does not help for the first packet).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I searched before asking this question. I didn't find an answer probably because I
I read this answer on SO: Because the recursive mutex has a sense of
I'm rather sure I haven't found an answer to this because I'm not sure
I feel silly asking this because I'm betting the answer is staring right at
I didn't find an answer to this question because I don't know if I'm
Am not sure how to put this, and I couldn't find the answer because
Previously I had asked this question and accepted the answer because it worked in
(Posting this with answer because I couldn't find a full explanation of how to
The official documentation does not seem to answer this, or I can't figure it
Ok so after reading danben's answer on this post , I guess I'm convinced

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.