Mar 7, 2018 04:46 PM
(This post was last modified: May 14, 2018 02:58 PM by aeiro.)
I wrote this program to get re-accustomed to c++ since I really haven't coded much in a while. Therefore this may need some optimization. My next project will be converting the client from console to GUI and I will release that too.
I tried to make comments on certain lines to help you guys understand what is going on. feel free to learn from this and criticize this work.
EDIT: I forgot to mention there is a bug in the chatloop that doesn't allow you to switch who you are sending your message to. If you guys cannot manage to handle this properly, I will update the code that fixes this.
Preview of code:
Code int Connection_Handler::Handle_Client(Data_Handler * _Handle)
{
CurrentClientData* CCD = new CurrentClientData();
CCD->_Handle = _Handle;
CCD->s[1] = NULL;
CCD->s[0] = _Handle->TempSocket;
Data_Handler::_sData *rDat = new Data_Handler::_sData();
Hash_Handler* hh = new Hash_Handler();
HANDLE cCH = NULL;
char* Buffer = (char*)malloc(1024);
int pType = NULL;
ULONG bAv = 1;
int succ = NULL;
ioctlsocket(CCD->s[0], FIONBIO, &bAv);
while (TRUE) {
fd_set recvSet;
timeval conTime;
conTime.tv_sec = 0;
conTime.tv_usec = 10000; //waits /10000 microseconds /10 milliseconds /.01 seconds
FD_ZERO(&recvSet); //sets struct to NULL
FD_SET(CCD->s[0], &recvSet); // adds unochat to struct, also adds +1 to struct count.
int sel = select(NULL, &recvSet, NULL, NULL, &conTime); //checks the status of the sockets.
if (sel > 0) {
ioctlsocket(CCD->s[0], FIONREAD, &bAv);
if (!bAv) bAv = 1;
if (bAv > 1024)
Buffer = (char*)malloc(bAv);
int retval = recv(CCD->s[0], Buffer, bAv, 0);// recv Buffer
if (retval == -1) break;
if (retval == 0) break;
rDat = _Handle->deserialize(Buffer, retval); //deserialize buffer
switch (rDat->Type)//TODO:handle create chat socket request && Handle errors//
{
case 0: // create chat socket
succ = hh->HashGrab(_Handle, rDat->fU, CCD->s[0]);
send(CCD->s[0], (char*)&succ, sizeof(int), 0);
return succ;
break;
case 1:// Login
succ = hh->HashGrab(_Handle, rDat->fU, rDat->fP);
send(CCD->s[0], (char*)&succ, sizeof(int), 0);
break;
case 2: // Create User
succ = hh->HashGrab(_Handle, rDat->fU);
if (!succ)
hh->HashAdd(_Handle, rDat->fU, rDat->fP);
send(CCD->s[0], (char*)&succ, sizeof(int), 0);
break;
case 3:// chat
//if (!hh->HashGrab(_Handle, rDat->fU)) { // check if logged in.
if (hh->HashGrab(_Handle, rDat->tU)) { // Check if other user is logged in;
if (hh->HashGrab(_Handle, rDat->fU, &CCD->s[1])) {// grab chat socket for user.
if (hh->HashGrab(_Handle, rDat->tU, &CCD->s[2])) {// grab chat socket for user.
if (cCH)
WaitForSingleObject(cCH, INFINITE);
CCD->rData = rDat;
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ChatLoop, (LPVOID)CCD, NULL, NULL);//create chat loop thread; //
}
}
//Handle socket is invalid;
}
//Handle *user* is not logged in;
// }
//Handle you're not logged in;
break;
default:
break;
}
}
}
return 0;
}
Download
https://github.com/HFaeiro/ChatServerAndClient
winapi, and winsock programmer
Nice post my dude. I don't need this but its one of those projects you download and play with anyways.
(Mar 7, 2018 11:37 PM)Leroy Jethro Gibbs Wrote: Nice post my dude. I don't need this but its one of those projects you download and play with anyways.
Haha glad you enjoyed it. Feel free to modify it to your liking.
winapi, and winsock programmer
(Mar 8, 2018 08:36 AM)aeiro Wrote: Haha glad you enjoyed it. Feel free to modify it to your liking.
Only time I've ever done this was with .NET. It semi worked. I used it for high school. It would log what you said to a text document and display it on all clients connected to it. It was shit. But it was a learning experience.
(Mar 8, 2018 10:05 AM)Leroy Jethro Gibbs Wrote: Only time I've ever done this was with .NET. It semi worked. I used it for high school. It would log what you said to a text document and display it on all clients connected to it. It was shit. But it was a learning experience.
This one doesn't log any messages. the client tells the server who it wants to talk to then the server just forwards that message to the corresponding server.
winapi, and winsock programmer
That's closer to C than C++.
Where is the OOP?
Mar 9, 2018 03:41 PM
(This post was last modified: Mar 9, 2018 03:52 PM by aeiro.)
(Mar 9, 2018 03:19 PM)Hoodie+ Wrote: That's closer to C than C++.
Where is the OOP?
I'm still in the process of learning. Currently learning templates right now.
If you could point me in a direction on how i could have used OOP here that would be great.
winapi, and winsock programmer
Great contribution OP! thanks
|