-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
145 lines (119 loc) · 3.23 KB
/
Copy pathclient.c
File metadata and controls
145 lines (119 loc) · 3.23 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
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <time.h>
#include <signal.h>
int main(int argc, char *argv[])
{
srand(time(NULL));
signal(SIGINT, SIG_IGN);
char msg[100];
int server, portNo;
struct sockaddr_in servAddr; // server socket address
if(argc != 3){
printf("Call model: %s <IP Address> <Port Number>\n", argv[0]);
exit(0);
}
if ((server = socket(AF_INET, SOCK_STREAM, 0)) < 0){
fprintf(stderr, "Error inside Socket Creation \n");
exit(1);
}
servAddr.sin_family = AF_INET;
sscanf(argv[2], "%d", &portNo);
servAddr.sin_port = htons((uint16_t)portNo);
if(inet_pton(AF_INET, argv[1], &servAddr.sin_addr) < 0){
fprintf(stderr, " Error inside inet_pton() \n");
exit(2);
}
if(connect(server, (struct sockaddr *) &servAddr, sizeof(servAddr))<0){
fprintf(stderr, " Error inside connect()\n");
exit(3);
}
int clientPts[2], ServerPts[2];
clientPts[0]=0;
clientPts[1]=0;
ServerPts[0]=0;
ServerPts[1]=0;
char ch;
while(1)
{
printf("\n <<<<<<<<<< Hit Enter to Start OR Hit 'Q' to Quit >>>>>>>>>> \n");
while(1)
{
//write operation//
ch=fgetc(stdin);
if(ch==0x0A)
{
clientPts[0] = (rand() % 6)+1;
clientPts[1]+=clientPts[0];
break;
}
else if(ch=='q')
{
clientPts[0]=0;
clientPts[1]=0;
write(server, &clientPts, sizeof(clientPts));
printf("\n\n I Quit, Have a great Day server. \n");
exit(7);
}
else
{
clientPts[0]=0;
printf("Hit Enter to Start");
}
}
printf("\nClient score :\n");
printf("Client current dice value : %d\n", clientPts[0]);
printf("Total client score : %d\n", clientPts[1]);
write(server, &clientPts, sizeof(clientPts));
//write operatin finished//
//Read operation//
if(clientPts[1] >= 100)
{
if (read(server, &msg, 50)<0){
fprintf(stderr, "Error in method : read() \n");
exit(3);
}
printf("\n\n <<<<<<<<<< Server Message >>>>>>>>>> %s \n",msg);
close(server);
exit(5);
}
//Read opration finished//
//Read operation//
if (read(server, &ServerPts, sizeof(ServerPts))<0){
fprintf(stderr, "Error in method : read() \n");
exit(3);
}
printf("\nServer score :-\n");
printf("Server current dice value : %d \n",ServerPts[0]);
printf("Total server score : %d\n",ServerPts[1]);
//Read opration finished//
//Read operation//
if(ServerPts[1] >= 100)
{
if (read(server, &msg, 50)<0){
fprintf(stderr, "Error in method : read() \n");
exit(3);
}
printf(" <<<<<<<<<< Server Message >>>>>>>>>> %s \n",msg);
close(server);
exit(5);
}
//Read opration finished//
//Read operation//
if (read(server, &msg, 50)<0){
fprintf(stderr, " Error in method : read() \n");
exit(3);
}
printf("\n\n <<<<<<<<<< Server Message >>>>>>>>>> %s \n",msg);
//Read opration finished//
}
exit(0);
}