├── .gitignore ├── Client ├── Header Files │ ├── Class_ClientNetworkInit.hpp │ ├── Class_Logger.hpp │ ├── Function_ClientNetworkCom.hpp │ ├── Function_GetAuthenticationData.hpp │ └── Function_RunCounter.hpp └── Program Codes │ ├── ChatClient.cpp │ └── RunMeOnlyOnce.cpp ├── LICENSE ├── README.md ├── Server ├── Header Files │ ├── Class_Logger.hpp │ ├── Class_NetworkInit.hpp │ ├── Function_Menu.hpp │ ├── Function_MessageParser.hpp │ ├── Function_NetworkCom.hpp │ ├── Function_RunCounter.hpp │ └── Library_IDManager.hpp └── Program Codes │ ├── ChatServer.cpp │ └── RunMeOnlyOnce.cpp └── User Guide.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /Client/Header Files/Class_ClientNetworkInit.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Class_Logger.hpp" 5 | using namespace std; 6 | 7 | 8 | SOCKET ServerCom = INVALID_SOCKET; 9 | int ClientAddressSize; 10 | SOCKADDR_IN ClientAddress; 11 | char lftxt[40] = "../Logs/ClientNetworkInitLogs"; 12 | logfile lf(lftxt); 13 | 14 | class ClientNetworkInit 15 | { 16 | private: 17 | WSAData ClientNetworkData; 18 | char message[512]; 19 | 20 | public: 21 | ClientNetworkInit(char sIP[],u_short sPORT) 22 | { 23 | lf.writeLog(sIP); 24 | cout<<"Intializing Winsock API"; 25 | strcpy(lftxt,"Initializing Winsock API"); 26 | lf.writeLog(lftxt); 27 | 28 | for(int i=0;i<15;i++) 29 | { 30 | cout<<"."; 31 | Sleep(100); 32 | } 33 | if(!WSAStartup(MAKEWORD(2,1),(WSAData*)&ClientNetworkData)) 34 | { 35 | cout<<"done"< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "Function_RunCounter.hpp" 8 | using namespace std; 9 | 10 | class logfile 11 | { 12 | private: 13 | time_t now; 14 | char* time_now; 15 | fstream WriteLog; 16 | public: 17 | logfile(char filename[]) 18 | { 19 | char count[2]; 20 | snprintf(count,sizeof(count),"%d",ReadRunCounter()); 21 | now = time(0); 22 | time_now = ctime(&now); 23 | WriteLog.open(strcat(strcat(filename,count),".txt"),ios::out); 24 | WriteLog<<"Run Time : "< 2 | #include 3 | #include "Function_GetAuthenticationData.hpp" 4 | using namespace std; 5 | 6 | struct chatData 7 | { 8 | int myid; 9 | int chatid; 10 | }; 11 | 12 | void DataExt(char msg[],chatData& data) 13 | { 14 | char tmp[3]; 15 | char tmp0[3]; 16 | 17 | for(int i=0;i<2;i++) 18 | { 19 | tmp[i] = msg[i]; 20 | } 21 | for(int i=0;i<2;i++) 22 | { 23 | tmp0[i] = msg[i+2]; 24 | } 25 | 26 | data.myid = atoi(tmp); 27 | data.chatid = atoi(tmp0); 28 | } 29 | 30 | void ClientNetworkComInit(char ip[],u_short port) 31 | { 32 | string cMessageRecv; 33 | char cMessage[512] = " "; 34 | int cstate = 0; 35 | chatData data; 36 | 37 | do 38 | { 39 | cstate = recv(ServerCom,cMessage,512,0); 40 | }while(cstate <=0); 41 | cMessageRecv = cMessage; 42 | cout<<"Command: "< 2 | #include 3 | using namespace std; 4 | 5 | void GetAuthenticationData(char msg[]); 6 | 7 | void GetAuthenticationData(char msg[]) 8 | { 9 | string myid; 10 | string chatid; 11 | 12 | cout<<"Enter your ID: "; 13 | cin>>myid; 14 | 15 | cout<<"Enter the ID with whom you want to chat: "; 16 | cin>>chatid; 17 | 18 | myid = myid + chatid; 19 | 20 | strcpy(msg,myid.c_str()); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Client/Header Files/Function_RunCounter.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int ReadRunCounter(); 6 | void WriteRunCounter(); 7 | 8 | int ReadRunCounter() 9 | { 10 | int counter; 11 | fstream file; 12 | file.open("../Logs/RunCounter.txt",ios::in); 13 | 14 | file>>counter; 15 | file.close(); 16 | return counter; 17 | } 18 | 19 | void WriteRunCounter() 20 | { 21 | int tempcount = ReadRunCounter(); 22 | tempcount++; 23 | 24 | fstream fileWrite; 25 | fileWrite.open("../Logs/RunCounter.txt",ios::out); 26 | 27 | fileWrite< 2 | #include 3 | #include "../Header files/Class_ClientNetworkInit.hpp" 4 | #include "../Header files/Function_ClientNetworkCom.hpp" 5 | using namespace std; 6 | 7 | main() 8 | { 9 | WriteRunCounter(); //updates the counter which counts the number of times this program is run 10 | char ip[15]; //variable to store the ip address 11 | u_short port; 12 | thread recv; 13 | 14 | cout<<"\t\tChat Client v1.0_Alpha <====> Client"<>ip; 18 | 19 | cout<<"Enter the port of the server: "; 20 | cin>>port; 21 | 22 | ClientNetworkInit cli(ip,port); //construct the ClientNetworkInit object 23 | cli.ConnectServer(); //connect to the specified server 24 | 25 | ClientNetworkComInit(ip,port); //setup to enter your id and the id of the user you want to chat with 26 | 27 | recv = thread(ClientNetworkComRecv); //start the reciever on a separate thread 28 | recv.detach(); 29 | 30 | ClientNetworkComSend(); //run the sender in the main thread 31 | } 32 | -------------------------------------------------------------------------------- /Client/Program Codes/RunMeOnlyOnce.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | main() 6 | { 7 | fstream file; 8 | file.open("../Logs/RunCounter.txt",ios::out); 9 | int val = 0 ; 10 | 11 | file< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "Function_RunCounter.hpp" 7 | using namespace std; 8 | 9 | class logfile 10 | { 11 | private: 12 | time_t now; //required variables to get the current time 13 | char* time_now; 14 | fstream WriteLog; //declaration of fstream object to write logs to a file 15 | public: 16 | logfile(char filename[]) //class constructors 17 | { 18 | char count[3]; 19 | snprintf(count,sizeof(count),"%d",ReadRunCounter()); //convert runcount to char 20 | now = time(0); 21 | time_now = ctime(&now); 22 | WriteLog.open(strcat(strcat(filename,count),".txt"),ios::out); //open file to write log 23 | WriteLog<<"Run Time : "< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "Function_NetworkCom.hpp" 10 | using namespace std; 11 | 12 | ////////////////////////////////// Variables ////// 13 | int ClientStore = 0; // 14 | char lftxtnio[40] = "../Logs/NetworkInitLogs"; // 15 | char lftxtni[50]; // 16 | logfile lfni(lftxtnio); // 17 | ////////////////////////////////////////////// 18 | 19 | ///////////////////////////////////////// Class NetworkInit //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 20 | class NetworkInit 21 | { 22 | private: 23 | //declarations of winsock library objects necessary for the sockets parts to run 24 | WSAData NetworkData; 25 | SOCKET ListenSocket; 26 | SOCKADDR_IN Address; 27 | int AddressSize; 28 | 29 | public: 30 | NetworkInit(); // default constructor 31 | 32 | NetworkInit(char IP[],u_short PORT) // argumented constructor 33 | { 34 | lfni.writeLog(IP); // write server ip to log file 35 | //---------------------------------------Initailize Winsock API---------------------------------------------------------------------------------------------------------------------------- 36 | //process to start the winsock api 37 | 38 | strcpy(lftxtni,"Intializing Winsock API"); 39 | lfni.writeLog(lftxtni); 40 | cout<<"Intializing Winsock API"; 41 | for(int i=0;i<17;i++) 42 | { 43 | cout<<"."; 44 | Sleep(100); 45 | } 46 | if(!WSAStartup(MAKEWORD(2,1),(WSAData*)&NetworkData)) 47 | { 48 | cout<<"done"< 2 | #include 3 | using namespace std; 4 | 5 | int Menu(); 6 | int Menu() 7 | { 8 | int option; //define option variable 9 | system("cls"); //clear the screen 10 | cout<<"\t\tTCP Chat Server_______v1.0______Vedant Paranjape"<"<>option; //take the input of option 20 | cin.ignore(); 21 | 22 | return option; 23 | } 24 | -------------------------------------------------------------------------------- /Server/Header Files/Function_MessageParser.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct AuthDataParsed 7 | { 8 | int ID; 9 | int sID; 10 | }; 11 | 12 | void AuthDataParser(char RawMessage[],AuthDataParsed& msg); 13 | 14 | void AuthDataParser(char RawMessage[],AuthDataParsed& msg) 15 | { 16 | char id[3] = ""; 17 | char sid[3] = ""; 18 | 19 | for(int i=0;i<2;i++) 20 | { 21 | id[i] = RawMessage[i]; 22 | } 23 | 24 | for(int i=0;i<2;i++) 25 | { 26 | sid[i] = RawMessage[i+2]; 27 | } 28 | 29 | msg.ID = atoi(id); 30 | msg.sID = atoi(sid); 31 | 32 | } 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Server/Header Files/Function_NetworkCom.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "Library_IDManager.hpp" 8 | #include "Function_MessageParser.hpp" 9 | #include "Class_Logger.hpp" 10 | using namespace std; 11 | 12 | ////////// variables //////////// 13 | thread HandleClient[10]; 14 | SOCKET ClientConn[10]; 15 | char lftxt[20] = "../Logs/ChatLogs"; 16 | logfile lf(lftxt); 17 | ///////////////////////////// 18 | 19 | ////////////////////////////////////////////////// Function NetworkCom ////////////////////////////////////////////////////////////////////////////////////////////////// 20 | void NetworkCom(int loc); 21 | 22 | void NetworkCom(int loc) 23 | { 24 | int flag = 0; 25 | char datatemp[512] = " "; 26 | char msgtemp[512] = " "; 27 | AuthDataParsed AuthData; 28 | 29 | do 30 | { 31 | flag = recv(ClientConn[loc],datatemp,512,0); 32 | }while(flag<=0); 33 | flag = 0; 34 | 35 | AuthDataParser(datatemp,AuthData); 36 | IDStore(AuthData.ID,loc); 37 | 38 | cout<<">>"<<"ID # "< 0) 44 | { 45 | lf.writeLog(datatemp); 46 | lf.writeLog(msgtemp); 47 | } 48 | flag = 0; 49 | 50 | flag = send(ClientConn[ReturnLocation(AuthData.sID)],msgtemp,512,0); 51 | if(flag > 0) 52 | { 53 | lf.writeLog(datatemp); 54 | lf.writeLog(msgtemp); 55 | } 56 | flag = 0; 57 | 58 | if(!strcmp(msgtemp,"EXIT")) 59 | { 60 | closesocket(ClientConn[loc]); 61 | cout<<"Client "< 2 | #include 3 | using namespace std; 4 | 5 | int ReadRunCounter(); 6 | void WriteRunCounter(); 7 | 8 | int ReadRunCounter() 9 | { 10 | int counter; 11 | fstream file; 12 | file.open("../Logs/RunCounter.txt",ios::in); 13 | 14 | file>>counter; 15 | file.close(); 16 | return counter; 17 | } 18 | 19 | void WriteRunCounter() 20 | { 21 | int tempcount = ReadRunCounter(); 22 | tempcount++; 23 | 24 | fstream fileWrite; 25 | fileWrite.open("../Logs/RunCounter.txt",ios::out); 26 | 27 | fileWrite< 2 | using namespace std; 3 | 4 | ////////////////////// 5 | int ID_Array[10]; // 6 | //////////////////// 7 | 8 | ///////////////////////////////////////// 9 | void IDStore(int ID,int ArrayLoc); // 10 | void IDStore(int ID,int ArrayLoc) // 11 | { // 12 | ID_Array[ArrayLoc] = ID; // 13 | } // 14 | /////////////////////////////////// 15 | 16 | /////////////////////////////////////// 17 | int IDReturn(int ArrayLoc); // 18 | int IDReturn(int ArrayLoc) // 19 | { // 20 | return ID_Array[ArrayLoc]; // 21 | } // 22 | ///////////////////////////////// 23 | 24 | ////////////////////////////////////////////// 25 | int ReturnLocation(int ID); // 26 | int ReturnLocation(int ID) // 27 | { // 28 | int lo = 0; // 29 | // 30 | for(int i=0;i<10;i++) // 31 | { // 32 | if(ID_Array[i] == ID) // 33 | { // 34 | lo = i; // 35 | } // 36 | // 37 | } // 38 | return lo; // 39 | } // 40 | ////////////////////////////// 41 | -------------------------------------------------------------------------------- /Server/Program Codes/ChatServer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../Header Files/Function_Menu.hpp" 5 | #include "../Header Files/Class_NetworkInit.hpp" 6 | using namespace std; 7 | 8 | void CommandHandle(NetworkInit& a); 9 | 10 | void CommandHandle(NetworkInit& a) 11 | { 12 | char command[5]; 13 | 14 | cin.ignore(); 15 | 16 | while(1) 17 | { 18 | if(kbhit()) 19 | { 20 | cin.getline(command,20); 21 | } 22 | 23 | if(!strcmp(command,"quit")) 24 | { 25 | a.~NetworkInit(); 26 | break; 27 | } 28 | } 29 | } 30 | 31 | void RunServer(); 32 | 33 | void RunServer() 34 | { 35 | WriteRunCounter(); 36 | char IP[16]; 37 | u_short PORT; 38 | thread CommandHandler; 39 | 40 | system("cls"); 41 | cout<<"\n\n\t\tEnter the server ip address: "; 42 | cin>>IP; 43 | cout<>PORT; 47 | cout< 2 | #include 3 | using namespace std; 4 | 5 | main() 6 | { 7 | fstream file; 8 | file.open("../Logs/RunCounter.txt",ios::out); 9 | int val = 0 ; 10 | 11 | file<