Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //server.c
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <pthread.h>
- #include <signal.h>
- #include <string.h>
- #define MAXCON 100
- int keepAlive = 1;
- int rs;
- int thrcount=0;
- pthread_t* thr[MAXCON];
- pthread_t daemonthr;
- struct users {
- char username[1024];
- char password[1024];
- int status;
- };
- typedef struct users users;
- users uservect[1024];
- int usercnt;
- struct thrarg {
- int sock;
- int thrindex;
- struct sockaddr_in caddr;
- int caddr_len;
- };
- typedef struct thrarg thrarg;
- pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
- pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
- int findFreeThread() {
- int i;
- for(i=0;i<MAXCON;i++) {
- if(thr[i] == NULL) {
- return i;
- }
- }
- return -1;
- }
- int logcount() {
- int i;
- int k=0;
- for(i=0;i<MAXCON;i++) {
- if(uservect[i].status>0) k++;
- }
- return k;
- }
- int response(char* username, char* password) {
- printf("%s %s \n",username,password);
- int i;
- for(i=0;i<usercnt;i++) {
- if(strcmp(username,uservect[i].username) == 0) {
- if(strcmp(password,uservect[i].password) == 0 ) {
- if(uservect[i].status == 0) {
- uservect[i].status = 1;
- return logcount();
- }
- return -2;
- }
- return -1;
- }
- }
- return -1;
- }
- void logout(char* username) {
- int i;
- for(i=0;i<usercnt;i++) {
- if(strcmp(username,uservect[i].username) == 0) {
- uservect[i].status = 0;
- }
- }
- }
- void serve(thrarg* arg) {
- pthread_mutex_lock(&mtx);
- printf("Threadcount:%d\n",thrcount);
- thrcount++;
- if(thrcount % 3 == 0) {
- pthread_cond_signal(&cond);
- }
- pthread_mutex_unlock(&mtx);
- char buf[1024];
- int k = recv(arg->sock,buf,1024,0);
- if(k<=0) {
- perror("Could not receive");
- }
- buf[k] = 0;
- char username[1024];
- strcpy(username,buf);
- k = recv(arg->sock,buf,1024,0);
- if(k<=0) {
- perror("Could not receive password");
- }
- buf[k] = 0;
- char password[1024];
- strcpy(password,buf);
- pthread_mutex_lock(&mtx);
- printf("Username:%s\nPassword:%s\n",username,password);
- pthread_mutex_unlock(&mtx);
- int resp = response(username,password);
- pthread_mutex_lock(&mtx);
- printf("%d\n",resp);
- pthread_mutex_unlock(&mtx);
- resp = htonl(resp);
- k = send(arg->sock,&resp,sizeof(int),0);
- if(k<=0) {
- perror("Could not send");
- }
- pthread_mutex_lock(&mtx);
- if(resp>0) {
- //logout(username);
- }
- thrcount--;
- free(thr[arg->thrindex]);
- thr[arg->thrindex] = NULL;
- free(arg);
- pthread_mutex_unlock(&mtx);
- }
- void handler() {
- keepAlive = 0;
- if(close(rs)<0) {
- perror("Could not close socket");
- return;
- }
- int i;
- for(i=0;i<MAXCON;i++) {
- pthread_t* p = thr[i];
- if(p) {
- pthread_join(*p,NULL);
- }
- }
- pthread_cond_signal(&cond);
- pthread_join(daemonthr,NULL);
- return;
- }
- void readUsers() {
- FILE* fd = fopen("users.txt","r");
- if(fd==0) {
- printf("Could not open file!\n");
- exit(3);
- }
- int i=0;
- while( !feof(fd) ) {
- fscanf(fd,"%s %s",uservect[i].username, uservect[i].password);
- //printf("%s %s\n",uservect[i].username,uservect[i].password);
- i++;
- }
- usercnt = i;
- }
- void daemonf() {
- while (keepAlive) {
- pthread_mutex_lock(&mtx);
- pthread_cond_wait(&cond,&mtx);
- if(thrcount % 3 == 0) {
- printf("Connection is multiple of 3 : %d \n",thrcount);
- }
- pthread_mutex_unlock(&mtx);
- }
- }
- int main() {
- pthread_create(&daemonthr,NULL,(void * )daemonf,NULL);
- readUsers();
- rs = socket(AF_INET,SOCK_STREAM,0);
- if(rs < 0) {
- perror("Could not create rendezvous socket!");
- }
- struct sockaddr_in addr,caddr;
- addr.sin_family = AF_INET;
- addr.sin_port = htons(1754);
- addr.sin_addr.s_addr = INADDR_ANY;
- if(bind(rs,(struct sockaddr*) &addr,sizeof(addr))<0) {
- perror("Could not bind server");
- }
- if(listen(rs,5) < 0 ) {
- perror("Could not listen");
- }
- signal(SIGINT,handler);
- int sock;
- unsigned int len = sizeof(caddr);
- while(keepAlive) {
- sock = accept(rs,(struct sockaddr*) &caddr,&len);
- if((sock<0)&&keepAlive) {
- perror("Could not accept connection!");
- }
- int k = findFreeThread();
- if(k<0) {
- close(sock);
- continue;
- }
- if(len&&keepAlive) {
- printf("Incomming connection from %s on port %d\n",inet_ntoa(caddr.sin_addr),ntohs(caddr.sin_port));
- }
- thrarg *arg = malloc(sizeof(thrarg));
- arg->sock = sock;
- arg->thrindex = k;
- arg->caddr = caddr;
- arg->caddr_len = len;
- thr[k] = malloc(sizeof(pthread_t));
- pthread_create(thr[k],NULL,(void*) serve,arg);
- }
- //join remaining threads
- /*
- int i;
- for(i=0;i<MAXCON;i++) {
- pthread_t* p = thr[i];
- if(p) {
- pthread_join(*p,NULL);
- }
- }*/
- return 0;
- }
- //client.c
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <pthread.h>
- #include <signal.h>
- #include <string.h>
- int main(int argc,char** argv) {
- if(argc<3) {
- printf("Give the username and password as a parameter!\n");
- return 1;
- }
- char username[1024];
- strcpy(username,argv[1]);
- char password[1024];
- strcpy(password,argv[2]);
- int sock = socket(AF_INET,SOCK_STREAM,0);
- if(sock<0) {
- perror("Could not create socket");
- }
- struct sockaddr_in addr;
- addr.sin_family = AF_INET;
- addr.sin_port = htons(1754);
- addr.sin_addr.s_addr = inet_addr("127.0.0.1");
- if( connect(sock,(struct sockaddr*) &addr,sizeof(addr) ) < 0 ) {
- perror("Could not connect to server");
- }
- int k = send(sock,username,strlen(username),0);
- if(k<=0) {
- perror("Could not send username");
- }
- sleep(1);
- k = send(sock,password,strlen(password),0);
- if(k<=0) {
- perror("Could not send password");
- }
- int resp;
- k = recv(sock,&resp,sizeof(int),0);
- if(k<=0) {
- perror("Could not receive response");
- }
- resp = ntohl(resp);
- printf("Response:%d\n",resp);
- close(sock);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement