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 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

Related Questions

This is beyond both making sense and my control. That being said here is
I have found this example on StackOverflow: var people = new List<Person> { new
I want to use a temp directory that will be unique to this build.
Let say I have the following desire, to simplify the IConvertible's to allow me
I have a new web app that is packaged as a WAR as part
(please excuse that I didn't use aliases). I would like my query output to
I'm trying to build a C++ extension for python using swig. I've followed the
I have a login.jsp page which contains a login form. Once logged in the
I need to develop a file indexing application in python and wanted to know
I was reading JavaScript: The Good Parts and the author mentions that JavaScript is

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.