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

  • Home
  • SEARCH
  • 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 295527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:27:58+00:00 2026-05-12T06:27:58+00:00

This my chat mixed with threads to run server and chat at same time.

  • 0

This my chat mixed with threads to run server and chat at same time. The problem is when I send a message to the chat lets say “Hello” as the first message,
and “bye” as the second
Output will go:
Hello
Byelo And some extra characters that I do not want there
how to remove my chat’s extra characters and letters? C++
I tried cout<<“Message: “<

But it did not seem to work

#include <windows.h>
#include <winsock2.h>
#include <stdio.h>
#include <iostream>
#include <cpl.h>
#include <string>
using namespace std;

DWORD WINAPI Thread(LPVOID);
HANDLE Thread_Handle = NULL;
DWORD Thread_ID      = 0;

DWORD WINAPI Thread(LPVOID data)
{

    while(1){        
    WSADATA t_wsa; // WSADATA structure
    WORD wVers; // version number
    int iError; // error number

    wVers = MAKEWORD(2, 2); // Set the version number to 2.2
    iError = WSAStartup(wVers, &t_wsa); // Start the WSADATA


if(iError != NO_ERROR || iError == 1){
    //  MessageBox(NULL, (LPCTSTR)"Error at WSAStartup()", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
        WSACleanup();

    }

    if(LOBYTE(t_wsa.wVersion) != 2 || HIBYTE(t_wsa.wVersion) != 2){
   // MessageBox(NULL, (LPCTSTR)"Error at WSAStartup()", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
        WSACleanup();

    }

    SOCKET sServer;
    sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    if(sServer == INVALID_SOCKET || iError == 1){
    //  MessageBox(NULL, (LPCTSTR)"Invalid Socket!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
        WSACleanup();

    }


    SOCKADDR_IN sinServer;
    memset(&sinServer, 0, sizeof(sinServer));

    sinServer.sin_family = AF_INET;
    sinServer.sin_addr.s_addr = INADDR_ANY; // Where to start server?
    sinServer.sin_port = htons( 1002); // Port



    if(bind(sServer, (LPSOCKADDR)&sinServer, sizeof(sinServer)) == SOCKET_ERROR){
        /* failed at starting server */
    //  MessageBox(NULL, (LPCTSTR)"Could not bind the server!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
        WSACleanup();

    }
    while(listen(sServer, 20) == SOCKET_ERROR){
    Sleep(10);

}



  SOCKET sClient;
    int szlength;

    szlength = sizeof(sinServer);
    sClient = accept(sServer, (LPSOCKADDR)&sinServer, &szlength);
    if (sClient == INVALID_SOCKET){
    //  MessageBox(NULL, (LPCTSTR)"Could not accept this client!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
    closesocket(sServer);
        WSACleanup();
    } else {
   // MessageBox(NULL, (LPCTSTR)"Accepted a Client!", (LPCTSTR)"Server::Success", MB_OK);
  }
  // Now we can send/recv data!

    int iRet;
    char buffer[200];
//  strcpy(buffer, "Welcome to our Server!\0");
    iRet = send(sClient, buffer, strlen(buffer), 0); 
    if(iRet == SOCKET_ERROR){
        //MessageBox(NULL, (LPCTSTR)"Could not send data!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
        closesocket(sClient);
    closesocket(sServer);
        WSACleanup();

    }
    char autoresponse[80];
    int bytes;
//  strcpy(autoresponse, "Message Received!");
    autoresponse[strlen(autoresponse)-1] = 0;
    //MessageBox(NULL, (LPCTSTR)"Server is ready for messages and is hiding!", (LPCTSTR)"Server::Success", MB_OK);
    char *cClientMessage;
    cClientMessage = new char[600];
    cClientMessage[599] = 0;
  while(bytes = recv(sClient, cClientMessage, 599, 0)){

    if(bytes < 1){

      continue;
    }
char cRead[99];///////////////////////////////////////////////////////////////////



     cout<<"Message: "<<cClientMessage<<"       \n";     




      /////////////////////////////////////////////////////////////////////////////

        iRet = send(sClient, autoresponse, strlen(autoresponse), 0); 
        if(iRet == SOCKET_ERROR){
        //  MessageBox(NULL, (LPCTSTR)"Could not send response!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
      }}

  delete [] cClientMessage;
    // Cleanup
    closesocket(sClient);
    closesocket(sServer);
    // Shutdown Winsock
    WSACleanup();


Sleep(10);
}
    return (0);
}

int main()
{
    Thread_Handle = CreateThread(NULL,0,Thread,(LPVOID)0,0,&Thread_ID);


    while(1){

char IP[100];
char message[255];
cout<<"IP: \n";
cin>>IP;                    

char buffer[150];
    WSADATA t_wsa; // WSADATA structure
    WORD wVers; // version number
    int iError; // error number

    wVers = MAKEWORD(2, 2); // Set the version number to 2.2
    iError = WSAStartup(wVers, &t_wsa); // Start the WSADATA

    if(iError != NO_ERROR || iError == 1){
        MessageBox(NULL, (LPCTSTR)"Error at WSAStartup()", (LPCTSTR)"Client::Error", MB_OK|MB_ICONERROR);
        WSACleanup();
        return 0;
    }
    /* Correct version? */
    if(LOBYTE(t_wsa.wVersion) != 2 || HIBYTE(t_wsa.wVersion) != 2){
    MessageBox(NULL, (LPCTSTR)"Error at WSAStartup()", (LPCTSTR)"Client::Error", MB_OK|MB_ICONERROR);
        WSACleanup();
        return 0;
    }
    SOCKET sClient;
    sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    if(sClient == INVALID_SOCKET || iError == 1){
        MessageBox(NULL, (LPCTSTR)"Invalid Socket!", (LPCTSTR)"Client::Error", MB_OK|MB_ICONERROR);
        WSACleanup();
        return 0;
    }
    SOCKADDR_IN sinClient;
    memset(&sinClient, 0, sizeof(sinClient));

 char cIP[50];

    sinClient.sin_family = AF_INET;
    sinClient.sin_addr.s_addr = inet_addr(IP); // Where to start server?
    sinClient.sin_port = htons(1003); // Port

    if(connect(sClient, (LPSOCKADDR)&sinClient, sizeof(sinClient)) == SOCKET_ERROR){
        /* failed at starting server */
        MessageBox(NULL, (LPCTSTR)"Could not connect to the server!", (LPCTSTR)"Client::Error", MB_OK|MB_ICONERROR);
        WSACleanup();
        return 0;
    }

    while(1){            
cout<<"Message: \n";
cin.getline(message,255);
string input2 = message;  

    // Now we can send/recv data!
    int iRet;
    char cBuffer[600];
    char buffer2[260];


//  MessageBox(NULL, (LPCTSTR)"Connected!", (LPCTSTR)"Client::Server", MB_OK|MB_ICONEXCLAMATION);

    strcpy(buffer, message );



iRet = send(sClient, buffer, strlen(buffer), 0); //send data 




    if(iRet == SOCKET_ERROR){
        MessageBox(NULL, (LPCTSTR)"Could not send data!", (LPCTSTR)"Client::Error", MB_OK|MB_ICONERROR);
        WSACleanup();
        return 0;
    }
}

    int bytes;
    bytes = SOCKET_ERROR;
    char *cServerMessage;
    cServerMessage = new char[600];
  while(bytes = recv(sClient, cServerMessage, 599, 0)){
    if(bytes == SOCKET_ERROR){
        char cError[500];
            sprintf(cError, "Connection failed, WINSOCK error code: %d", WSAGetLastError());
            MessageBox(NULL, (LPCTSTR)cError, (LPCTSTR)"Client::Error", MB_OK|MB_ICONERROR);
            closesocket(sClient);
        // Shutdown Winsock
        WSACleanup();
      return 0;
    }
    if (bytes == 0 || bytes == WSAECONNRESET) {
      MessageBox(NULL, (LPCTSTR)"Connection Disconnected!", (LPCTSTR)"Client::Error", MB_OK|MB_ICONERROR);
      closesocket(sClient);
      // Shutdown Winsock
      WSACleanup();
      return 0;
    }
    if(bytes < 1){
      Sleep(300);
      continue;
    }   
    delete [] cServerMessage;
    // Cleanup
    closesocket(sClient);
    // Shutdown Winsock
    WSACleanup();
    return 0;


}
Sleep(10);
// This is how to terminate a thread // TerminateThread(Thread_Handle,0);
}

    return 0;
}
  • 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-05-12T06:27:58+00:00Added an answer on May 12, 2026 at 6:27 am

    recv() doesn’t append a '\0' to the end of the bytes written. You need to use the return value of recv() and add the '\0' yourself.

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

Sidebar

Ask A Question

Stats

  • Questions 156k
  • Answers 156k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not sure i understand you, but i will try to… May 12, 2026 at 10:50 am
  • Editorial Team
    Editorial Team added an answer From the code above, im seeing the following UIButton *button… May 12, 2026 at 10:50 am
  • Editorial Team
    Editorial Team added an answer Why not use NSFileHandle's readInBackgroundAndNotify instead? This way, your code… May 12, 2026 at 10:50 am

Related Questions

I have a WCF chat service that accepts duplex tcp connections. A single duplex
I'm starting a personal project, so I have at the moment complete architectural/design control.
I am trying to create a simple ajax chat using JQuery and ASP.NET. My
I want to implement a chat site with following features: Direct one-on-one chat with
I've looked for this a few times in the past, to no avail. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.