-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathit21238_server.c
More file actions
218 lines (175 loc) · 5.71 KB
/
it21238_server.c
File metadata and controls
218 lines (175 loc) · 5.71 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#define READ 0
#define WRITE 1
int fd[2];
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno , input;
char client_message[256];
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
if(pipe(fd)==-1)
error("ERROR opening pipe");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR on binding");
printf("listening...\n");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
printf("listened\n");
//create thread
/*int client_sock;
pthread_t client_threadid;
while((client_sock = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen) != -1))
{ pthread_create(&client_threadid,NULL,handle_connection,&sockfd);
printf("Here\n");*/
//get port number
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1)
perror("getsockname");
else
printf("port number %d\n", ntohs(sin.sin_port));
/* //get client ip address
char clntName[INET_ADDRSTRLEN];
FILE *output;
if(inet_ntop(AF_INET,&cli_addr.sin_addr.s_addr,clntName,sizeof(clntName))!=NULL) {
output = fopen("output.txt","a+");
fprintf(output,"%s%c%d",clntName,'/',ntohs(cli_addr.sin_port));
fclose(output);
} else {
printf("Unable to get address\n");
} */
if (newsockfd < 0) {
//error("ERROR on accept");
printf("%d\n",inet_ntoa(cli_addr.sin_addr));
}
while(1)
{
printf("while 1 loop \n");
sleep(1);
bzero(buffer, 256);
bzero(client_message, 256);
n = read(newsockfd,&input,sizeof(input));
if (n < 0)
error("ERROR reading from socket");
printf("input: %d\n",input);
//printf("%s \n",buffer);
//printf("Hello3\n");
printf("Buffer %s\n", buffer);
if(input==1)
{
//printf("User Input1:\n %s",client_message);
//Receive a message from client
while((read(newsockfd , buffer , 255)) > 0 )
{ printf("Hello \n");
printf("User Input:%s\n",buffer);
printf("Hello1 \n");
const char* words[3]; // 1
words[0] = "rock";
words[1] = "paper";
words[2]="scissors";
const char* random; // 2
srand(time(NULL)); //initialize the random seed
random = words[rand() % 3]; // 3
printf("server makes his choice randomly..\n");
sleep(2);
printf("%s \n",random);// 4
sleep(2);
printf("server's choice: %s \n" ,random);
printf("client's choice: %s \n" ,buffer);
if (!(strcmp(random,"rock")) && !(strcmp(buffer,"rock"))) {
//message = "isopalia";
printf("isopalia\n");
//send message to the client
n= write(sockfd,"isopalia",strlen("isopalia"));
}
else if (!(strcmp(random,"rock")) && !(strcmp(buffer,"paper"))) {
//message = "client is the winner";
printf("client is the winner");
n= write(sockfd,"client is the winner",strlen("client is the winner"));
}
else if (!(strcmp(random,"rock")) && !(strcmp(buffer,"scissors"))) {
//message = "server is the winner";
printf("server is the winner");
n= write(sockfd,"server is the winner",strlen("server is the winner"));
}
else if (!(strcmp(random,"paper")) && !(strcmp(buffer,"rock"))) {
//message = "server is the winner";
printf("server is the winner");
n= write(sockfd,"server is the winner",strlen("server is the winner"));
}
else if (!(strcmp(random,"paper")) && !(strcmp(buffer,"paper"))) {
//message = "isopalia";
printf("isopalia");
n= write(sockfd,"isopalia",strlen("isopalia"));
}
else if (!(strcmp(random,"paper")) && !(strcmp(buffer,"scissors"))) {
//message = "client is the winner";
printf("client is the winner");
n= write(sockfd,"client is the winner",strlen("client is the winner"));
}
else if (!(strcmp(random,"scissors")) && !(strcmp(buffer,"rock"))) {
//message = "client is the winner";
printf("client is the winner");
n= write(sockfd,"client is the winner",strlen("client is the winner"));
}
else if (!(strcmp(random,"scissors")) && !(strcmp(buffer,"paper"))) {
//message = "server is the winner";
printf("server is the winner");
n= write(sockfd,"server is the winner",strlen("server is the winner"));
}
else if (!(strcmp(random,"scissors")) && !(strcmp(buffer,"scissors"))) {
//message = "isopalia";
printf("isopalia");
n= write(sockfd,"isopalia",strlen("isopalia"));
} else {
printf("error!");
//close(sockfd);
//return 0;
}
//edw ok
}
}
else if(input==2)
{
printf("else if input 2");
close(sockfd);
break;
}
//while
}
printf("end of main");
//main
}