jebes komentarje k itak niso prou

main
Gasper Spagnolo 2022-10-23 13:23:53 +02:00
parent fef1084436
commit c6c1b206be
1 changed files with 67 additions and 82 deletions

View File

@ -1,14 +1,3 @@
/*H********************************************************************************
* Ime datoteke: serverLinux.cpp
*
* Opis:
* Enostaven strežnik, ki zmore sprejeti le enega klienta naenkrat.
* Strežnik sprejme klientove podatke in jih v nespremenjeni obliki pošlje
* nazaj klientu - odmev.
*
*H*/
//Vkljuèimo ustrezna zaglavja
#include<stdio.h> #include<stdio.h>
#include<sys/socket.h> #include<sys/socket.h>
#include<netinet/in.h> #include<netinet/in.h>
@ -17,10 +6,6 @@
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
/*
Definiramo vrata (port) na katerem bo strežnik poslušal
in velikost medponilnika za sprejemanje in pošiljanje podatkov
*/
#define PORT 10000 #define PORT 10000
#define BUFFER_SIZE 256 #define BUFFER_SIZE 256
#define MAX_CLIENTS 5 #define MAX_CLIENTS 5
@ -49,103 +34,103 @@ int check_if_there_is_space_for_client(Client *socks) {
} }
void *handle_client(void *arg) { void *handle_client(void *arg) {
char buff[BUFFER_SIZE]; char buff[BUFFER_SIZE];
Client *c = (Client *)arg; Client *c = (Client *)arg;
int clientSock = c->clientSock; int clientSock = c->clientSock;
int sock_ix = c->sock_ix; int sock_ix = c->sock_ix;
int iResult; int iResult;
do{ do{
iResult = recv(clientSock, buff, BUFFER_SIZE, 0); iResult = recv(clientSock, buff, BUFFER_SIZE, 0);
if (iResult > 0) { if (iResult > 0) {
printf("Bytes received: %d\n", iResult); printf("Bytes received: %d\n", iResult);
iResult = send(clientSock, buff, iResult, 0 ); iResult = send(clientSock, buff, iResult, 0 );
if (iResult == -1) { if (iResult == -1) {
printf("send failed!\n"); printf("send failed!\n");
close(clientSock); close(clientSock);
break; break;
}
printf("Bytes sent: %d\n", iResult);
}
else if (iResult == 0) {
printf("Closing connection(%d), lasted %ld seconds\n", sock_ix, time(NULL) - c->connection_start);
} }
else{ printf("Bytes sent: %d\n", iResult);
printf("recv failed!\n"); }
close(clientSock); else if (iResult == 0) {
break; printf("Closing connection(%d), lasted %ld seconds\n", sock_ix, time(NULL) - c->connection_start);
} }
else{
printf("recv failed!\n");
close(clientSock);
break;
}
} while (iResult > 0); } while (iResult > 0);
close(clientSock); close(clientSock);
pthread_spin_lock(&slock); pthread_spin_lock(&slock);
c->sock_ix = -1; c->sock_ix = -1;
pthread_spin_unlock(&slock); pthread_spin_unlock(&slock);
pthread_exit(NULL); pthread_exit(NULL);
} }
int main(int argc, char **argv){ int main(int argc, char **argv){
int iResult; int iResult;
int listener= socket(AF_INET, SOCK_STREAM, 0); int listener= socket(AF_INET, SOCK_STREAM, 0);
if (listener == -1) { if (listener == -1) {
printf("Error creating clientSock\n"); printf("Error creating clientSock\n");
return 1; return 1;
} }
sockaddr_in listenerConf; sockaddr_in listenerConf;
listenerConf.sin_port=htons(PORT); listenerConf.sin_port=htons(PORT);
listenerConf.sin_family=AF_INET; listenerConf.sin_family=AF_INET;
listenerConf.sin_addr.s_addr=INADDR_ANY; listenerConf.sin_addr.s_addr=INADDR_ANY;
iResult = bind( listener, (sockaddr *)&listenerConf, sizeof(listenerConf)); iResult = bind( listener, (sockaddr *)&listenerConf, sizeof(listenerConf));
if (iResult == -1) { if (iResult == -1) {
printf("Bind failed\n"); printf("Bind failed\n");
close(listener); close(listener);
return 1; return 1;
} }
if ( listen( listener, 5 ) == -1 ) { if ( listen( listener, 5 ) == -1 ) {
printf( "Listen failed\n"); printf( "Listen failed\n");
close(listener); close(listener);
return 1; return 1;
} }
int clientSock; int clientSock;
pthread_spin_init(&slock, PTHREAD_PROCESS_PRIVATE); pthread_spin_init(&slock, PTHREAD_PROCESS_PRIVATE);
pthread_t t[MAX_CLIENTS]; pthread_t t[MAX_CLIENTS];
Client clients[MAX_CLIENTS]; Client clients[MAX_CLIENTS];
for(int i = 0; i < MAX_CLIENTS; i++) { for(int i = 0; i < MAX_CLIENTS; i++) {
clients[i].sock_ix = -1; clients[i].sock_ix = -1;
} }
while (1) while (1)
{ {
clientSock = accept(listener,NULL,NULL); clientSock = accept(listener,NULL,NULL);
if (clientSock == -1) { if (clientSock == -1) {
printf("Accept failed\n"); printf("Accept failed\n");
close(listener); close(listener);
return 1; return 1;
} }
pthread_spin_lock(&slock); pthread_spin_lock(&slock);
int sock_ix = check_if_there_is_space_for_client(clients); int sock_ix = check_if_there_is_space_for_client(clients);
pthread_spin_unlock(&slock); pthread_spin_unlock(&slock);
if (sock_ix < 0) { if (sock_ix < 0) {
printf("Too many clients\n"); printf("Too many clients\n");
close(clientSock); close(clientSock);
} else { } else {
clients[sock_ix].sock_ix = sock_ix; clients[sock_ix].sock_ix = sock_ix;
clients[sock_ix].clientSock = clientSock; clients[sock_ix].clientSock = clientSock;
clients[sock_ix].connection_start = time(NULL); clients[sock_ix].connection_start = time(NULL);
@ -154,10 +139,10 @@ int main(int argc, char **argv){
printf("Hello i am new client and my idx is %d, my sock descriptor %d\n", sock_ix, clientSock); printf("Hello i am new client and my idx is %d, my sock descriptor %d\n", sock_ix, clientSock);
pthread_create(&t[sock_ix], NULL, handle_client, (void *) &clients[sock_ix]); pthread_create(&t[sock_ix], NULL, handle_client, (void *) &clients[sock_ix]);
} }
} }
close(listener); close(listener);
pthread_spin_destroy(&slock); pthread_spin_destroy(&slock);
return 0; return 0;
} }