diff --git a/Socket Client/main.c b/Socket Client/main.c new file mode 100644 index 0000000..686b58d --- /dev/null +++ b/Socket Client/main.c @@ -0,0 +1,260 @@ +#ifndef WIN32_LEAG_AND_MEAN +#define WIN32_LEAG_AND_MEAN +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +//#include +#include + +#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;i0); + + 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;i1) + { + res=send(client, enc,strlen(enc),0); + printf("\n"); + if(res !=strlen(enc)) + { + printf("---SYSTEM--- = Send Failed.\n"); + } + } + } + } + } + + return 0; + } diff --git a/Socket Client/start.bat b/Socket Client/start.bat new file mode 100644 index 0000000..7a77318 --- /dev/null +++ b/Socket Client/start.bat @@ -0,0 +1,4 @@ +gcc main.c -o client -l ws2_32 +client +pause +cls \ No newline at end of file diff --git a/Socket_Multi/main.c b/Socket_Multi/main.c new file mode 100644 index 0000000..34fc798 --- /dev/null +++ b/Socket_Multi/main.c @@ -0,0 +1,465 @@ +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif + + +#include +#include +#include +#include +#include +#include +#include + +#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;i0) + { + //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 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; j0) + { + 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"); + strcat(sysM,&arName[i][0]); + strncat(sysM," Has Disconnected\n",strlen(sysM)); + strcpy(&arName[i][0],""); + + for(int j = 0; j