Released
Released
This commit is contained in:
parent
c5c79e3b59
commit
1672fc1590
4 changed files with 733 additions and 0 deletions
260
Socket Client/main.c
Normal file
260
Socket Client/main.c
Normal file
|
@ -0,0 +1,260 @@
|
||||||
|
#ifndef WIN32_LEAG_AND_MEAN
|
||||||
|
#define WIN32_LEAG_AND_MEAN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <windows.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#include <iphlpapi.h>
|
||||||
|
#include <process.h>
|
||||||
|
#include <stdatomic.h>
|
||||||
|
//#include <unistd.h>
|
||||||
|
#include <conio.h>
|
||||||
|
|
||||||
|
#pragma comment(lib, "Ws2_32.lib")
|
||||||
|
|
||||||
|
#define BUFLEN 512
|
||||||
|
#define PORT 27015
|
||||||
|
#define ADDRESS "93.190.8.248"
|
||||||
|
int first= !0;
|
||||||
|
int pass= 0;
|
||||||
|
int key=3;
|
||||||
|
char si = 126;
|
||||||
|
|
||||||
|
//global Running variable
|
||||||
|
_Atomic char running = 0;
|
||||||
|
|
||||||
|
DWORD WINAPI sendThreadFunc(LPVOID lpParam);
|
||||||
|
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
printf("Welcome!\n");
|
||||||
|
int res;
|
||||||
|
char dec[BUFLEN];
|
||||||
|
int tilda =0;
|
||||||
|
|
||||||
|
//Init
|
||||||
|
WSADATA wsaData;
|
||||||
|
res = WSAStartup(MAKEWORD(2,2), &wsaData);
|
||||||
|
if(res)
|
||||||
|
{
|
||||||
|
printf("Startup Failed: %d\n", res);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//SETUP Client
|
||||||
|
|
||||||
|
//Construct socket
|
||||||
|
SOCKET client;
|
||||||
|
client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
|
if(client == INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
printf("Error with construction: %d\n", WSAGetLastError());
|
||||||
|
WSACleanup();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
//connect to adress
|
||||||
|
struct sockaddr_in address;
|
||||||
|
address.sin_family = AF_INET;
|
||||||
|
address.sin_addr.s_addr = inet_addr(ADDRESS);
|
||||||
|
address.sin_port = htons(PORT);
|
||||||
|
res = connect(client, (struct sockaddr *)&address, sizeof(address));
|
||||||
|
if(res == SOCKET_ERROR)
|
||||||
|
{
|
||||||
|
printf("Connection Failed : %d\n",WSAGetLastError());
|
||||||
|
closesocket(client);
|
||||||
|
WSACleanup();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else if (client == INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
printf("Connection Failed : %d\n",WSAGetLastError());
|
||||||
|
WSACleanup();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Connection Parameters = %s:%d\n", ADDRESS, PORT);
|
||||||
|
running = !0;
|
||||||
|
|
||||||
|
//Main Loop
|
||||||
|
|
||||||
|
// Send Thread
|
||||||
|
DWORD thrdId;
|
||||||
|
HANDLE sendThread = CreateThread(NULL, 0, sendThreadFunc, &client, 0, &thrdId);
|
||||||
|
if(sendThread)
|
||||||
|
{
|
||||||
|
printf("Send Thread Started with Thread ID: %d\n", thrdId);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Send thread failed: %d\n", GetLastError());
|
||||||
|
}
|
||||||
|
|
||||||
|
//receive loop
|
||||||
|
char recvbuf[BUFLEN];
|
||||||
|
do
|
||||||
|
{
|
||||||
|
res = recv(client, recvbuf, BUFLEN, 0);
|
||||||
|
recvbuf[res] = '\0';
|
||||||
|
strcpy(dec,recvbuf);
|
||||||
|
tilda = 0;
|
||||||
|
if(res >0)
|
||||||
|
{
|
||||||
|
for(int i=0;i<strlen(dec);i++)
|
||||||
|
{
|
||||||
|
if(dec[i]==126 && tilda==0)
|
||||||
|
{
|
||||||
|
dec[i]=26;
|
||||||
|
for(int j=i+1;j<strlen(dec);j++)
|
||||||
|
{
|
||||||
|
dec[j]=dec[j]-key;
|
||||||
|
}
|
||||||
|
tilda=1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("%s\n",dec);
|
||||||
|
}
|
||||||
|
else if(!res)
|
||||||
|
{
|
||||||
|
printf("Connection closed.\n");
|
||||||
|
running = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Receive Failed: %d\n",WSAGetLastError());
|
||||||
|
running = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while(res >0);
|
||||||
|
|
||||||
|
running = 0;
|
||||||
|
|
||||||
|
//connection finished, terminator for thread
|
||||||
|
if(CloseHandle(sendThread))
|
||||||
|
{
|
||||||
|
printf("Send thread closed successfully.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
//CleanUP
|
||||||
|
|
||||||
|
res = shutdown(client, SD_BOTH);
|
||||||
|
if(res==SOCKET_ERROR)
|
||||||
|
{
|
||||||
|
printf("Shutdown failed : %d\n",WSAGetLastError());
|
||||||
|
closesocket(client);
|
||||||
|
WSACleanup();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
closesocket(client);
|
||||||
|
WSACleanup();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
DWORD WINAPI sendThreadFunc(LPVOID lpParam)
|
||||||
|
{
|
||||||
|
SOCKET client = *(SOCKET*)lpParam;
|
||||||
|
char sendbuf[BUFLEN];
|
||||||
|
int sendbuflen, res;
|
||||||
|
char name[20];
|
||||||
|
char passw[20];
|
||||||
|
char enc[BUFLEN];
|
||||||
|
|
||||||
|
while(running)
|
||||||
|
{
|
||||||
|
strcpy(enc,"");
|
||||||
|
strcpy(sendbuf,"");
|
||||||
|
if(first!=0)
|
||||||
|
{
|
||||||
|
//Name
|
||||||
|
strcpy(name,"");
|
||||||
|
printf("\t\t\tName = ");
|
||||||
|
scanf("%s",name);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
res=send(client, name,strlen(name),0);
|
||||||
|
if(res!=strlen(name))
|
||||||
|
{
|
||||||
|
printf("\n---Name couldn't send, your first successful message will be your name\n");
|
||||||
|
}
|
||||||
|
first=0;
|
||||||
|
}
|
||||||
|
if(pass==0)
|
||||||
|
{
|
||||||
|
//PassW
|
||||||
|
strcpy(passw,"");
|
||||||
|
printf("\t\t\tPassword = ");
|
||||||
|
scanf("%s",passw);
|
||||||
|
printf("\n");
|
||||||
|
res=send(client, passw,strlen(passw),0);
|
||||||
|
if(res!=strlen(passw))
|
||||||
|
{
|
||||||
|
printf("\n---Password couldn't send, your first successful message will be your password\n");
|
||||||
|
}
|
||||||
|
pass=1;
|
||||||
|
system("cls");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fgets(sendbuf, BUFLEN-1, stdin);
|
||||||
|
sendbuf[strlen(sendbuf)-1]='\0';
|
||||||
|
sendbuflen = strlen(sendbuf);
|
||||||
|
if(sendbuf[0]!='\n' && !(sendbuf[0]=='\t'&&sendbuflen==2))
|
||||||
|
{
|
||||||
|
if(sendbuf[0]!='/')
|
||||||
|
{
|
||||||
|
strcat(enc,&si);
|
||||||
|
strcat(enc,sendbuf);
|
||||||
|
for(int i =1;i<strlen(enc);i++)
|
||||||
|
{
|
||||||
|
enc[i]=enc[i]+key;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
strcat(enc,sendbuf);
|
||||||
|
}
|
||||||
|
enc[strlen(enc)]='\0';
|
||||||
|
if(strlen(enc)>1)
|
||||||
|
{
|
||||||
|
res=send(client, enc,strlen(enc),0);
|
||||||
|
printf("\n");
|
||||||
|
if(res !=strlen(enc))
|
||||||
|
{
|
||||||
|
printf("---SYSTEM--- = Send Failed.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
4
Socket Client/start.bat
Normal file
4
Socket Client/start.bat
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
gcc main.c -o client -l ws2_32
|
||||||
|
client
|
||||||
|
pause
|
||||||
|
cls
|
465
Socket_Multi/main.c
Normal file
465
Socket_Multi/main.c
Normal file
|
@ -0,0 +1,465 @@
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <windows.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#include <iphlpapi.h>
|
||||||
|
|
||||||
|
#pragma comment(lib, "Ws_32.lib")
|
||||||
|
|
||||||
|
#define BUFLEN 512
|
||||||
|
#define PORT 27015
|
||||||
|
#define ADDRESS "93.190.8.248"
|
||||||
|
#define MAX_CLIENTS 5
|
||||||
|
|
||||||
|
|
||||||
|
//FUNCS
|
||||||
|
|
||||||
|
int dc(int i);
|
||||||
|
int mte(char*);
|
||||||
|
|
||||||
|
//variables
|
||||||
|
fd_set socketSet;
|
||||||
|
SOCKET clients[MAX_CLIENTS];
|
||||||
|
SOCKET sd, max_sd;
|
||||||
|
|
||||||
|
int curNoClients = 0;
|
||||||
|
struct sockaddr_in clientAddr;
|
||||||
|
int clientAddrlen;
|
||||||
|
char running = !0;
|
||||||
|
char recvbuf[BUFLEN];
|
||||||
|
char sysM[BUFLEN];
|
||||||
|
char message[BUFLEN+50];
|
||||||
|
char lastIP[32];
|
||||||
|
char lastPort[16];
|
||||||
|
char arIP[MAX_CLIENTS][32];
|
||||||
|
char arPort[MAX_CLIENTS][16];
|
||||||
|
char dIP[MAX_CLIENTS*5][32];
|
||||||
|
char dPort[MAX_CLIENTS*5][16];
|
||||||
|
int dcl=0;
|
||||||
|
int first[MAX_CLIENTS][1];
|
||||||
|
char lastName[20];
|
||||||
|
char arName[MAX_CLIENTS][20];
|
||||||
|
char dName[MAX_CLIENTS*5][20];
|
||||||
|
char pass[]="mandalina";
|
||||||
|
char gpass[20];
|
||||||
|
int arPas[MAX_CLIENTS];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
printf("Hi!\n");
|
||||||
|
|
||||||
|
int res, sendRes;
|
||||||
|
|
||||||
|
//Init
|
||||||
|
WSADATA wsaData;
|
||||||
|
res = WSAStartup(MAKEWORD(2,2), &wsaData);
|
||||||
|
if(res)
|
||||||
|
{
|
||||||
|
printf("Startup failed : %d\n",res);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Setup Server
|
||||||
|
|
||||||
|
//Construct
|
||||||
|
SOCKET listener;
|
||||||
|
listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
|
if (listener == INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
printf("Error with construction: %d\n",WSAGetLastError());
|
||||||
|
WSACleanup();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Setup for multi
|
||||||
|
char multiple = !0;
|
||||||
|
res = setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &multiple, sizeof(multiple));
|
||||||
|
if (res < 0)
|
||||||
|
{
|
||||||
|
printf("Multiple Client Setup Failed : %d\n", WSAGetLastError());
|
||||||
|
closesocket(listener);
|
||||||
|
WSACleanup();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Bind Address
|
||||||
|
struct sockaddr_in address;
|
||||||
|
address.sin_family = AF_INET;
|
||||||
|
address.sin_addr.s_addr = inet_addr(ADDRESS);
|
||||||
|
address.sin_port = htons(PORT);
|
||||||
|
bind(listener, (struct sockaddr*)&address,sizeof(address));
|
||||||
|
if ( res == SOCKET_ERROR)
|
||||||
|
{
|
||||||
|
printf("Bind failed &d\n", WSAGetLastError());
|
||||||
|
closesocket(listener);
|
||||||
|
WSACleanup();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//set listener
|
||||||
|
res = listen(listener, SOMAXCONN);
|
||||||
|
if ( res == SOCKET_ERROR)
|
||||||
|
{
|
||||||
|
printf("Listen failed &d\n", WSAGetLastError());
|
||||||
|
closesocket(listener);
|
||||||
|
WSACleanup();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Accepting on %s:%d\n", ADDRESS, PORT);
|
||||||
|
|
||||||
|
//Main Loop
|
||||||
|
|
||||||
|
//clear client array
|
||||||
|
memset(clients, 0, MAX_CLIENTS * sizeof(SOCKET));
|
||||||
|
for(int i=0;i<MAX_CLIENTS;i++)
|
||||||
|
{
|
||||||
|
strcpy(&arIP[i][0],"");
|
||||||
|
strcpy(&arPort[i][0],"");
|
||||||
|
strcpy(&arName[i][0],"");
|
||||||
|
first[i][0]=-1;
|
||||||
|
arPas[i]=-1;
|
||||||
|
}
|
||||||
|
for(int i=0;i<MAX_CLIENTS*5;i++)
|
||||||
|
{
|
||||||
|
strcpy(&dIP[i][0],"");
|
||||||
|
strcpy(&dPort[i][0],"");
|
||||||
|
strcpy(&dName[i][0],"");
|
||||||
|
}
|
||||||
|
strcpy(sysM,"");
|
||||||
|
strcpy(lastIP,"");
|
||||||
|
strcpy(lastPort,"");
|
||||||
|
strcpy(message,"");
|
||||||
|
strcpy(lastName,"");
|
||||||
|
|
||||||
|
|
||||||
|
//Loop
|
||||||
|
while(running)
|
||||||
|
{
|
||||||
|
strcpy(sysM,"");
|
||||||
|
//clear the set
|
||||||
|
FD_ZERO(&socketSet);
|
||||||
|
|
||||||
|
//add listener
|
||||||
|
FD_SET(listener, &socketSet);
|
||||||
|
for(int i = 0;i<MAX_CLIENTS;i++)
|
||||||
|
{
|
||||||
|
sd = clients[i];
|
||||||
|
if(sd>0)
|
||||||
|
{
|
||||||
|
//add an active client to the set
|
||||||
|
FD_SET(sd, &socketSet);
|
||||||
|
}
|
||||||
|
if(sd>max_sd)
|
||||||
|
{
|
||||||
|
max_sd = sd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int activity = select(max_sd+1, &socketSet, NULL, NULL, NULL);
|
||||||
|
if (activity < 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
//determine if listener socket has activity
|
||||||
|
if(FD_ISSET(listener, &socketSet))
|
||||||
|
{
|
||||||
|
//accept connection
|
||||||
|
sd = accept(listener, NULL, NULL);
|
||||||
|
if(sd == INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
printf("Error Accepting : %d \n", WSAGetLastError());
|
||||||
|
}
|
||||||
|
//get client info
|
||||||
|
clientAddrlen = sizeof(address);
|
||||||
|
getpeername(sd, (struct sockaddr*)&clientAddr, &clientAddrlen);
|
||||||
|
strcat(lastIP,inet_ntoa(clientAddr.sin_addr));
|
||||||
|
itoa(ntohs(clientAddr.sin_port),lastPort,10);
|
||||||
|
printf("Client connected at %s:%s\n", lastIP,lastPort);
|
||||||
|
|
||||||
|
|
||||||
|
//add to array
|
||||||
|
if(curNoClients >= MAX_CLIENTS )
|
||||||
|
{
|
||||||
|
printf("FULL\n");
|
||||||
|
send(sd,"\n ---SERVER--- = Server is FULL, Going to be Disconnected\n",strlen("\n ---SYSTEM--- = Server is FULL, Going to be Disconnected\n"),0);
|
||||||
|
closesocket(sd);
|
||||||
|
shutdown(sd, SD_BOTH);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//scan through list
|
||||||
|
for(int i = 0; i< MAX_CLIENTS; i++)
|
||||||
|
{
|
||||||
|
if(!clients[i])
|
||||||
|
{
|
||||||
|
clients[i]= sd;
|
||||||
|
printf("Added to the List at index: %d\n", i);
|
||||||
|
strcpy(&arIP[i][0],lastIP);
|
||||||
|
strcpy(&arPort[i][0],lastPort);
|
||||||
|
first[i][0]=1;
|
||||||
|
arPas[i]=0;
|
||||||
|
curNoClients++;
|
||||||
|
strcat(sysM,"\n---SERVER--- = ");
|
||||||
|
strncat(sysM,lastIP,strlen(sysM));
|
||||||
|
strncat(sysM,":",strlen(sysM));
|
||||||
|
strncat(sysM,lastPort,strlen(sysM));
|
||||||
|
strncat(sysM," Has Connected\n",strlen(sysM));
|
||||||
|
|
||||||
|
for(int j = 0; j<MAX_CLIENTS; j++)
|
||||||
|
{
|
||||||
|
if(sd==clients[j] || arPas[j]==0 || first[j][0]==1)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
send(clients[j],sysM, strlen(sysM), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
strcpy(sysM,"");
|
||||||
|
strcpy(lastIP,"");
|
||||||
|
strcpy(lastPort,"");
|
||||||
|
strcpy(lastName,"");
|
||||||
|
}
|
||||||
|
// iterate through clients
|
||||||
|
for(int i=0;i<MAX_CLIENTS;i++)
|
||||||
|
{
|
||||||
|
strcpy(message,"");
|
||||||
|
if(!clients[i])
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
sd = clients[i];
|
||||||
|
|
||||||
|
//determine if clients have activity
|
||||||
|
if(FD_ISSET(sd, &socketSet))
|
||||||
|
{
|
||||||
|
//get message
|
||||||
|
res = recv(sd, recvbuf, BUFLEN, 0);
|
||||||
|
if(res > 0)
|
||||||
|
{
|
||||||
|
//print message
|
||||||
|
recvbuf[res] = '\0';
|
||||||
|
printf("%d.Client -Received (%d): %s \n",i,res,recvbuf);
|
||||||
|
|
||||||
|
|
||||||
|
//test if leave
|
||||||
|
if(!memcmp(recvbuf, "/leave", 6))
|
||||||
|
{
|
||||||
|
dc(i);
|
||||||
|
}
|
||||||
|
//test if quit
|
||||||
|
else if(!memcmp(recvbuf, "/quit", 5*sizeof(char)))
|
||||||
|
{
|
||||||
|
running = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//Name
|
||||||
|
if(first[i][0]==1)
|
||||||
|
{
|
||||||
|
strcpy(lastName,recvbuf);
|
||||||
|
if(lastName[0]==13)
|
||||||
|
{
|
||||||
|
strcpy(lastName,"Unknown");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
strcpy(&arName[i][0],lastName);
|
||||||
|
strcpy(lastName,"");
|
||||||
|
first[i][0]=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Password
|
||||||
|
else if(arPas[i]==0)
|
||||||
|
{
|
||||||
|
strcpy(gpass,recvbuf);
|
||||||
|
if(strcmp(pass,gpass)==0)
|
||||||
|
{
|
||||||
|
arPas[i]=1;
|
||||||
|
send(sd,"---SERVER--- = Connection Successful\n",strlen("---SERVER--- = Connection Successful\n"),0);
|
||||||
|
strcat(sysM,"---SERVER--- = ");
|
||||||
|
strcat(sysM,&arIP[i][0]);
|
||||||
|
strcat(sysM,":");
|
||||||
|
strcat(sysM,&arPort[i][0]);
|
||||||
|
strcat(sysM," is Authorized as ");
|
||||||
|
strcat(sysM,&arName[i][0]);
|
||||||
|
mte(sysM);
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
send(sd,"Password is wrong\n",strlen("Password is wrong\n"),0);
|
||||||
|
dc(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//send message to everyone
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for(int j = 0; j<MAX_CLIENTS; j++)
|
||||||
|
{
|
||||||
|
if(sd==clients[j] || arPas[j]==0 || first[j][0]==1)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
//NAME is ADDRESS
|
||||||
|
/*strcat(message,&arIP[i][0]);
|
||||||
|
strcat(message,":");
|
||||||
|
strcat(message,&arPort[i][0]);
|
||||||
|
strcat(message," ");
|
||||||
|
strcat(message,"=");
|
||||||
|
strcat(message," ");
|
||||||
|
strcat(message,recvbuf);
|
||||||
|
send(clients[j],message,strlen(message),0);*/
|
||||||
|
|
||||||
|
//NAME is NAME
|
||||||
|
strcat(message,&arName[i][0]);
|
||||||
|
strcat(message," ");
|
||||||
|
strcat(message,"=");
|
||||||
|
strcat(message," ");
|
||||||
|
strcat(message,recvbuf);
|
||||||
|
send(clients[j],message,strlen(message),0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dc(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//disconnect everyone
|
||||||
|
for(int i = 0; i<MAX_CLIENTS;i++)
|
||||||
|
{
|
||||||
|
if(clients[i]>0)
|
||||||
|
{
|
||||||
|
dc(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//shutdown client
|
||||||
|
//res = shutdown(sd, SD_BOTH);
|
||||||
|
if(res == SOCKET_ERROR)
|
||||||
|
{
|
||||||
|
printf("Client shutdown failed : %d \n",WSAGetLastError());
|
||||||
|
|
||||||
|
}
|
||||||
|
closesocket(sd);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//ShutDown
|
||||||
|
closesocket(listener);
|
||||||
|
//Cleanup
|
||||||
|
res = WSACleanup();
|
||||||
|
if(res)
|
||||||
|
{
|
||||||
|
printf("Cleanup failed %d\n",res);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printf("DONE\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int dc(int i)
|
||||||
|
{
|
||||||
|
|
||||||
|
//close message
|
||||||
|
getpeername(sd, (struct sockaddr*)&clientAddr, &clientAddrlen);
|
||||||
|
strcat(lastIP,inet_ntoa(clientAddr.sin_addr));
|
||||||
|
itoa(ntohs(clientAddr.sin_port),lastPort,10);
|
||||||
|
printf("Client disconnected at %s:%s\n", lastIP,lastPort);
|
||||||
|
closesocket(sd);
|
||||||
|
shutdown(sd, SD_BOTH);
|
||||||
|
clients[i]=0;
|
||||||
|
strcpy(&arIP[i][0],"");
|
||||||
|
strcpy(&arPort[i][0],"");
|
||||||
|
|
||||||
|
first[i][0]=-1;
|
||||||
|
arPas[i]=-1;
|
||||||
|
curNoClients--;
|
||||||
|
if(dcl<MAX_CLIENTS*5)
|
||||||
|
{
|
||||||
|
strcpy(&dIP[dcl][0],lastIP);
|
||||||
|
strcpy(&dPort[dcl][0],lastPort);
|
||||||
|
strcpy(&dName[dcl][0],&arName[i][0]);
|
||||||
|
dcl++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("\nDCList is Full !\n");
|
||||||
|
}
|
||||||
|
strcat(sysM,"\n---SERVER--- = ");
|
||||||
|
strcat(sysM,lastIP);
|
||||||
|
strcat(sysM,":");
|
||||||
|
strcat(sysM,lastPort);
|
||||||
|
strcat(sysM,"-->");
|
||||||
|
strcat(sysM,&arName[i][0]);
|
||||||
|
strncat(sysM," Has Disconnected\n",strlen(sysM));
|
||||||
|
strcpy(&arName[i][0],"");
|
||||||
|
|
||||||
|
for(int j = 0; j<MAX_CLIENTS; j++)
|
||||||
|
{
|
||||||
|
if(sd==clients[j] || arPas[j]==0 || first[j][0]==1)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
send(clients[j],sysM, strlen(sysM), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
strcpy(sysM,"");
|
||||||
|
strcpy(lastIP,"");
|
||||||
|
strcpy(lastPort,"");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int mte(char* message)
|
||||||
|
{
|
||||||
|
for(int j = 0; j<MAX_CLIENTS; j++)
|
||||||
|
{
|
||||||
|
if(arPas[j]==0 || first[j][0]==1)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
send(clients[j],message,strlen(message),0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
4
Socket_Multi/run.bat
Normal file
4
Socket_Multi/run.bat
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
gcc main.c -o server -l ws2_32
|
||||||
|
server
|
||||||
|
pause
|
||||||
|
cls
|
Loading…
Add table
Add a link
Reference in a new issue