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;
}
recv()doesn’t append a'\0'to the end of the bytes written. You need to use the return value ofrecv()and add the'\0'yourself.