Skip to content

Instantly share code, notes, and snippets.

@SangSama
Last active April 17, 2020 10:13
Show Gist options
  • Save SangSama/68efc9c9b4282279db38c5a80ffc290c to your computer and use it in GitHub Desktop.
Save SangSama/68efc9c9b4282279db38c5a80ffc290c to your computer and use it in GitHub Desktop.
#include <iostream>
#include <WS2tcpip.h>
#include <winSock2.h>
#pragma comment(lib, "ws2_32")
#pragma warning(disable:4996)
DWORD WINAPI ClientThread(LPVOID);
void RemoveClient(SOCKET);
char* ids[64];
SOCKET clients[64];
int numClients = 0;
CRITICAL_SECTION cs;
int main()
{
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
InitializeCriticalSection(&cs);
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(9000);
SOCKET listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
bind(listener, (SOCKADDR*)&addr, sizeof(addr));
listen(listener, 5);
while (1) {
SOCKET client = accept(listener, NULL, NULL);
printf("Ket noi moi : %d\n", client);
CreateThread(0, 0, ClientThread, &client, 0, 0);
}
DeleteCriticalSection(&cs);
}
DWORD WINAPI ClientThread(LPVOID lpParam) {
SOCKET client = *(SOCKET*)lpParam;
char buf[256];
int ret;
char cmd[16], id[32], tmp[32];
const char* errorMsg = "Sai cu phap xin moi nhap lai\n";
const char* helloMsg = "Dang nhap theo cu phap \"[id:] [your_id]\".\n";
const char* logout = "Dang xuat thanh cong\n ";
send(client, helloMsg, strlen(helloMsg),0);
int checkLogin = 0;
char sendBuf[256];
while (1) {
//kiem tra dang nhap
if (checkLogin == 0) {
ret = recv(client, buf, sizeof(buf), 0);
if (ret <= 0) {
closesocket(client);
return 0;
}
buf[ret] = 0;
printf("Received: %s", buf);
//kiem tra cu phap
ret = sscanf(buf, "%s %s %s", cmd, id, tmp);
if (ret == 2) {
if (strcmp(cmd, "id:") == 0) {
const char* okMsg = "Dang nhap thanh cong\nDang xuat theo cu phap [exit]\n";
send(client, okMsg, strlen(okMsg), 0);
EnterCriticalSection(&cs);
ids[numClients] = id;
clients[numClients] = client;
numClients++;
// thong bao cho cac user khac co nguoi dang nhap
char noticeUser[256];
sprintf(noticeUser, "%s: %s %s", "User: ", id, " da tham gia nhom chat");
for (int i = 0; i < numClients; i++)
{
if (client != clients[i]) {
send(clients[i], noticeUser, sizeof(noticeUser), 0);
}
}
checkLogin = 1;
LeaveCriticalSection(&cs);
}
else {
send(client, errorMsg, strlen(errorMsg), 0);
}
}
else {
send(client, errorMsg, strlen(errorMsg), 0);
}
}
// chuyen tiep tin nhan
if (checkLogin == 1) {
ret = recv(client, buf, sizeof(buf), 0);
if (ret <= 0) {
closesocket(client);
return 0;
}
buf[ret] = 0;
printf("Recevied: %s\n", buf);
// Đăng xuất gan lai checklogin = 0
if (strcmp(buf,"exit") == 0) {
send(client, logout, strlen(logout), 0);
printf("Co user: %s dang xuat\n", client);
RemoveClient(client);
checkLogin = 0;
}
else {
ret = sscanf(buf, "%s", cmd);
if (ret == -1 || cmd[0] != '@') {
send(client, errorMsg, strlen(errorMsg), 0);
}
else {
sprintf(sendBuf, "%s: %s", id, buf + strlen(cmd) + 1);
if (strcmp(cmd, "@all") == 0) {
// chuyen tiep tin nhan
for (int i = 0; i < numClients; i++)
{
if (client != clients[i]) {
send(clients[i], sendBuf, sizeof(sendBuf), 0);
}
}
}
else {
for (int i = 0; i < numClients; i++)
{
if (strcmp(ids[i], cmd + 1) == 0) {
send(clients[i], sendBuf, sizeof(sendBuf), 0);
}
}
}
}
}
}
}
RemoveClient(client);
closesocket(client);
}
void RemoveClient(SOCKET client)
{
int i = 0;
for (; i < numClients; i++)
if (clients[i] == client) break;
if (i < numClients - 1)
clients[i] = clients[numClients - 1];
numClients--;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment