#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <netinet/in.h>
#include <resolv.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <iostream>
#include <string>
using namespace std;
void* SocketHandler(void*);
int get_port(void);
typedef struct {
char* user_name[50];
sockaddr_in* user_socket;
} user;
user* users [50];
int main(int argv, char** argc){
char buffer[1024];
int buffer_len = 1024;
int bytecount;
int host_port;
struct sockaddr_in my_addr;
printf("=== Welcome to the Chat client!! ===\n");
host_port=get_port();
printf("Chat Server Running on 127.0.0.1:%d,\n",host_port);
//initialize socket
int hsock;
int err;
int* p_int ;
int* csock;
sockaddr_in sadr;
socklen_t addr_size = 0;
pthread_t thread_id=0;
//socket creation
hsock = socket(AF_INET, SOCK_STREAM, 0);//IP4,TCP,Default based on previous args
//error control
if(hsock == -1){
printf("Error initializing socket %d\n", errno);
goto FINISH;
}
p_int = (int*)malloc(sizeof(int));
*p_int = 1;
//set socket option
if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
(setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){
printf("Error setting options %d\n", errno);
free(p_int);
goto FINISH;
}
free(p_int);
my_addr.sin_family = AF_INET ;
my_addr.sin_port = htons(host_port);
memset(&(my_addr.sin_zero), 0, 8);
my_addr.sin_addr.s_addr = INADDR_ANY ;
if( bind( hsock, (sockaddr*)&my_addr, sizeof(my_addr)) == -1 ){
fprintf(stderr,"Error binding to socket, make sure nothing else is listening on this port %d\n",errno);
goto FINISH;
}
if(listen( hsock, 10) == -1 ){
fprintf(stderr, "Error listening %d\n",errno);
goto FINISH;
}
addr_size = sizeof(sockaddr_in);
while(true){
printf("waiting for a connection\n");
csock = (int*)malloc(sizeof(int));
//establish connection
if((*csock = accept(hsock, (sockaddr*)&sadr, &addr_size))!= -1){
memset(buffer, 0, buffer_len);
//recerive username
if((bytecount = recv(*csock, buffer, buffer_len, 0))== -1){
fprintf(stderr, "Error receiving user name %d\n", errno);
goto FINISH;
}
//TODO validate user name
char* name= buffer;//get user name
cout<<"Client "<< name<<" connected from "<<inet_ntoa(sadr.sin_addr)<<": "<< sadr.sin_port<<"\n";
//create new thread to handle one client
pthread_create(&thread_id,0,&SocketHandler, (void*)csock );
pthread_detach(thread_id);
}
else{
fprintf(stderr, "Error accepting %d\n", errno);
}
}
FINISH:
;
}
int get_port( ){
int portNo;
printf("Enter service port :");
scanf("%d",&portNo);
printf("\n");
return portNo;
}
void* SocketHandler(void* lp){
int *csock = (int*)lp;
char buffer[1024];
int buffer_len = 1024;
int bytecount;
while(1){
memset(buffer, 0, buffer_len);
if((bytecount = recv(*csock, buffer, buffer_len, 0))== -1){
fprintf(stderr, "Error receiving data %d\n", errno);
goto FINISH;
}
//receive operations
string op_type;
op_type = strtok (buffer," ");
switch( op_type )
{
case "show":
capa++;
default :
printf("No such op\n");
}
if((bytecount = send(*csock, buffer, strlen(buffer), 0))== -1){
fprintf(stderr, "Error sending data %d\n", errno);
goto FINISH;
}
printf("Sent bytes %d\n", bytecount);
}
FINISH:
free(csock);
}