-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
130 lines (106 loc) · 3.07 KB
/
client.cpp
File metadata and controls
130 lines (106 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include<iostream>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<unistd.h>
#include<sys/select.h>
#include<stdlib.h>
#include<stdio.h>
#include<strings.h>
#define BUFFER 1024
using namespace std;
int session_control=0;
int validity(char *cmd)
{
if(strcmp(cmd,"\n")){
if(!strncmp(cmd,"regi",4)||!strncmp(cmd,"logi",4)||!strncmp(cmd,"send",4))
{
char *token=strtok(cmd," "),*arg1,*arg2;
arg1=strtok(NULL," ");
arg2=strtok(NULL,"\n");
if(!strcmp(token,"register"))
{
if((arg1!=NULL) && (arg2!= NULL))
return 1;
}
if((!strcmp(token,"send"))&& session_control==1)
{
if((arg1!=NULL) && (arg2!= NULL))
return 1;
}
if((!strcmp(token,"login"))&& session_control==0)
{
if((arg1!=NULL) && (arg2!= NULL))
return 1;
}
}
char *token=strtok(cmd,"\n");
if((!strcmp(token,"logout") || !strcmp(token,"lobbystatus")) && session_control==1)
return 1;
}
return 0;
}
void beginClient(int myfd){
fd_set readset;
int result,count,num;
char rbuff[BUFFER],wbuff[BUFFER],checkInput[BUFFER];
bzero(rbuff,BUFFER);
bzero(wbuff,BUFFER);
while(1){
FD_ZERO(&readset);
FD_SET(myfd,&readset);
FD_SET(STDIN_FILENO,&readset);
result=select(myfd+1,&readset,NULL,NULL,NULL); //multiplexing on standard input and client fd
if(result>0){
if(FD_ISSET(STDIN_FILENO,&readset)){ //standard input check
num=read(0,wbuff,BUFFER);
strcpy(checkInput,wbuff);
if(validity(checkInput)){ //checking validity of command
send(myfd,wbuff,num,0); //sending command to connected socket
}
else
cerr<<"Invalid command. Please enter again.\n\n";
bzero(wbuff,BUFFER);
bzero(checkInput,BUFFER);
}
if(FD_ISSET(myfd,&readset)){ //socket check
count=recv(myfd,rbuff,BUFFER,0); //reading from connected socket
if(strncmp(rbuff,"Welcome",7)==0)
session_control=1;
cerr<<rbuff<<endl;
if(count==0){
close(myfd);
exit(0);
}
bzero(rbuff,BUFFER);
}
}//result>0
if(result<0)
perror("error:");
}//WHILE
return;
}
int main() {
struct sockaddr_un servaddr;
int myfd=socket(AF_LOCAL,SOCK_STREAM,0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sun_family=AF_LOCAL;
strcpy(servaddr.sun_path,"mysocket");
int conn_status=connect(myfd,(sockaddr*) &servaddr, sizeof(servaddr));
if(conn_status==-1){
cerr<<"\nCannot connect.";
return -1;
}
cout<<"\n\t\t\tWelcome To ChatOn \n";
cout<<"_________________________ MENU _____________________________\n\n";
cout<<"\t\t\t>>register<<\n";
cout<<"\t\t\t>>login<<\n";
cout<<"\t\t\t>>send<<\n";
cout<<"\t\t\t>>lobbystatus<<\n";
cout<<"\t\t\t>>logout<<\n\n";
cout<<"Enter the Command you want to perform :\n";
fcntl(myfd, 0, O_NONBLOCK); //non blocking sockets
beginClient(myfd);
close(myfd);
}