# Winsock chat server and client application

I have made a C++ command line chat with server and clients as a hobby project.

#include <iostream>

#include <winsock2.h>

#include <windows.h>

#include "Class_Logger.hpp"

using namespace std;

SOCKET ServerCom = INVALID_SOCKET;

char lftxt[40] = "../Logs/ClientNetworkInitLogs";

logfile lf(lftxt);

class ClientNetworkInit

{

private:

char message[512];

public:

ClientNetworkInit(char sIP[],u_short sPORT)

{

lf.writeLog(sIP);

cout<<"Intializing Winsock API";

strcpy(lftxt,"Initializing Winsock API");

lf.writeLog(lftxt);

for(int i=0;i<15;i++)

{

cout<<".";

Sleep(100);

}

{

cout<<"done"<<endl;

strcpy(lftxt,"done");

lf.writeLog(lftxt);

}

else

{

cout<<"Error starting Winsock API"<<endl;

strcpy(lftxt,"Error starting Winsock API");

lf.writeLog(lftxt);

cout<<"Shutting down the server"<<endl;

strcpy(lftxt,"Error starting Winsock API");

lf.writeLog(lftxt);

lf.~logfile();

closesocket(ServerCom);

WSACleanup();

exit(0);

}

ServerCom = socket(AF_INET,SOCK_STREAM,0);

if(ServerCom == INVALID_SOCKET)

{

cout<<"Error initialising socket"<<endl;

strcpy(lftxt,"Error starting socket");

lf.writeLog(lftxt);

lf.~logfile();

closesocket(ServerCom);

WSACleanup();

exit(0);

}

else

{

cout<<"Socket Intialised"<<endl;

strcpy(lftxt,"Socket Intialised");

lf.writeLog(lftxt);

}

}

void ConnectServer()

{

int val;

if(!val)

{

cout<<"Connected to server"<<endl;

strcpy(lftxt,"Connected to server");

lf.writeLog(lftxt);

}

else

{

cout<<"Unable to connect to the server"<<endl;

strcpy(lftxt,"Unable to connect to the server");

lf.writeLog(lftxt);

cout<<"Closing chat client"<<endl;

strcpy(lftxt,"Closing chat client");

lf.writeLog(lftxt);

lf.~logfile();

WSACleanup();

exit(0);

}

}

};


I am a grade 12 student. The C++ syllabus doesn't even include vectors, but I still pulled this project off on my own. Please take this fact into consideration.

• Welcome to Code Review! This is off-topic because "Questions must include the code to be reviewed. Links to code hosted on third-party sites are permissible, but the most relevant excerpts must be embedded in the question itself." see the section Make sure you include your code in your question on What topics can I ask about here? – Sᴀᴍ Onᴇᴌᴀ Dec 7 '17 at 18:36
• Which site is appropriate for such a question? As i was kicked from SO and told to ask the question here – Vedant Dec 7 '17 at 18:40
• @SamOnela Can you still take a look at it? Please!!!!! – Vedant Dec 7 '17 at 18:41
• You can post it here - just paste the most relevant parts of your code - ensure you indent with four spaces to create a pre-formatted code block (i.e. <pre><code>) - see more in markdown help. – Sᴀᴍ Onᴇᴌᴀ Dec 7 '17 at 18:41
• @SamOnela done, is this appropriate? – Vedant Dec 7 '17 at 18:53