Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My program works with the exception of it missing data when data is sent to the client. The server seems to be able to absorb every bit of data without fail but clients do not seem to do the same.Why is something I have not been able to determine so far. So I ask why this problem exists and how do I fix it.

What I have tried:

main.cpp
C++
#include <iostream>
#include <string>
#include "code.h"
using namespace std;

void main(int argc , char *argv[])
{
	if(argc >= 2)
	{
		if(*argv[1] == '0')
		{
			cout << "Client active" << endl;
			classClient::makeSocketLoad();
			classClient::sockConnect("192.168.0.3",8888);
			classClient::getData();
			cout << "Enter your name" << endl;
			string name;
			getline(cin,name);
			name.append(": ");
			system("CLS");
			while(true)
			{
				string input;
				string data;
				getline(cin,input);
				data = name;
				data.append(input);
				classClient::sendData(data);
			}
			cout << "System Active" << endl;
			
			classClient::killWinsock();
		}
		if(*argv[1] == '1')
		{
			cout << "Server" << endl;
			serverClass::startWinsock();
			serverClass::makeListenSocket(8888);
			serverClass::takeConnections();		
		}	
	}
	else
	{
		cout << argc << endl;
	}
}


code.h
C++
#ifndef _code_h_
#define _code_h_
#include <iostream>
#include <string>
#include <io.h>
#include <stdlib.h>
#include <fstream>
//#include <winsock.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <sstream>
#include <thread>
#include <chrono>
#include <future>
#pragma comment(lib,"ws2_32.lib") //Winsock Library

namespace classClient
{

	struct sockaddr_in theClient;
	char *buffer = new char[1002];
	WSADATA theWSA;
	SOCKET theSocket,theOtherSocket;	

	DWORD WINAPI ThreadProc(__in  LPVOID lpParameter)
	{
		char *in = new char[1002];
		SOCKET AcceptSocket=(SOCKET) lpParameter;
		while(true)
		{
			recv(AcceptSocket,in,1000,0);
			std::cout << in << std::endl;
		}
		//closesocket(AcceptSocket);
		return 0;
	}
	void makeSocketLoad()
	{
		WSAStartup(MAKEWORD(2,2),&theWSA);
		theSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	}
	// port of 8888
	void sockConnect(std::string ip,int port)
	{	
		theClient.sin_addr.s_addr = inet_addr(ip.c_str());
		theClient.sin_family = AF_INET;
		theClient.sin_port = htons(port);
		//bind(theSocket,(sockaddr*) &theClient,sizeof(theClient));
		if(connect(theSocket,(SOCKADDR*)(&theClient),sizeof(theClient))!=0)
		{
			std::cout<<"Failed to establish connection with server\r\n";
			WSACleanup();
			system("PAUSE");
		}
	}
	void sendData(std::string data)
	{
		data[data.length()+1] = '\0';
		strcpy(buffer,data.c_str());
		send(theSocket,buffer,1000,0);	
	}
	void getData2()
	{
		DWORD dwThread;
		HANDLE hThread = CreateThread(NULL,0,ThreadProc,(LPVOID)theSocket,0,&dwThread);
		CloseHandle(hThread);
	}
	void getData()
	{
		auto go = std::async (getData2);
		go.get();
	}
	void killWinsock()
	{
		closesocket(theSocket);
		WSACleanup();
	}
}

namespace serverClass
{
	WSADATA wsaData;
	SOCKET ListenSocket;
	sockaddr_in addrServer;
	bool sendData = false;
	char *dataToSend = new char[1002];
	DWORD WINAPI ThreadProc(__in  LPVOID lpParameter)
	{
		SOCKET AcceptSocket=(SOCKET) lpParameter;
		char recvBuf[1002];
		memset(recvBuf,'\0',1000);
		std::string data("");
		while(true)
		{
			int count = recv(AcceptSocket,recvBuf,1000,0);
			if(count==0)break;
			if(count==SOCKET_ERROR)break;
			int sendCount,currentPosition=0;
			data = recvBuf;
			memset(recvBuf,'\0',1000);
			strcpy(dataToSend,data.c_str());
			sendData = true;
		}
		closesocket(AcceptSocket);
		return 0;
	}
	DWORD WINAPI ThreadProc2(__in  LPVOID lpParameter)
	{
		SOCKET AcceptSocket=(SOCKET) lpParameter;
		while(true)
		{
			if(sendData)
			{
				send(AcceptSocket,dataToSend,1000,0);
				std::cout << dataToSend << std::endl;
				sendData = false;
			}
			Sleep(5);
		}
		return 0;
	}	
	void startWinsock()
	{
		int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
		if (iResult != NO_ERROR) {
			wprintf(L"WSAStartup failed with error: %ld\n", iResult);
		}
	}
	void makeListenSocket(int port)
	{
		ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		if (ListenSocket == INVALID_SOCKET) 
		{
			wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
			WSACleanup();
		}
		sockaddr_in addrServer;
		addrServer.sin_family = AF_INET;
		addrServer.sin_addr.s_addr = INADDR_ANY;
		addrServer.sin_port = htons(port);
		if (bind(ListenSocket,(SOCKADDR *) & addrServer, sizeof (addrServer)) == SOCKET_ERROR)
		{
			wprintf(L"bind failed with error: %ld\n", WSAGetLastError());
			closesocket(ListenSocket);
			WSACleanup();
		}	
	}
	void takeConnections()
	{
		if (listen(ListenSocket, 5) == SOCKET_ERROR) {
			wprintf(L"listen failed with error: %ld\n", WSAGetLastError());
			closesocket(ListenSocket);
			WSACleanup();
		}
		SOCKADDR_IN addrClient;
		int len=sizeof(SOCKADDR);
		while(true)
		{
			SOCKET AcceptSocket=accept(ListenSocket,(SOCKADDR*)&addrClient,&len);
			if(AcceptSocket  == INVALID_SOCKET)break; 
			DWORD dwThread;
			HANDLE hThread = CreateThread(NULL,0,ThreadProc,(LPVOID)AcceptSocket,0,&dwThread);
			DWORD dwThread2;
			HANDLE hThread2 = CreateThread(NULL,0,ThreadProc2,(LPVOID)AcceptSocket,0,&dwThread2);
						
			if(hThread==NULL)
			{
				closesocket(AcceptSocket);
				wprintf(L"Thread Creat Failed!\n");
				break;
			}
			CloseHandle(hThread);
			CloseHandle(hThread2);	
		}
		closesocket(ListenSocket);
		WSACleanup();
	}
}

#endif
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900