forked from thedarknight84/socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_esempio_calcolatrice.c
102 lines (86 loc) · 2.39 KB
/
server_esempio_calcolatrice.c
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
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<stdio.h>
#define N_CON 5
void
error (char *msg)
{
perror (msg);
exit (1);
}
void
main (int argc, char *argv[])
{
int b, sockfd, connfd, sin_size, l, n, len;
char operator;
int op1 = 0, op2 = 0, result = 0;
int portno;
if (argc < 2)
{
fprintf (stderr, "ERROR, check and add the port number\n");
exit (1);
}
portno = atoi (argv[1]);
/* socket() */
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
error ("ERROR opening socket\n");
printf ("%d\n", sockfd);
struct sockaddr_in servaddr;
struct sockaddr_in clientaddr;
servaddr.sin_family = AF_INET;
// servaddr.sin_addr.s_addr = inet_addr ("127.0.0.1");
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons (portno);
/* bind() */
if ((bind (sockfd, (struct sockaddr *) &servaddr, sizeof (servaddr))) < 0)
error ("ERROR on binding");
/* listen() */
listen (sockfd, N_CON);
sin_size = sizeof (struct sockaddr_in);
/* accept() */
connfd = accept (sockfd, (struct sockaddr *) &clientaddr, &sin_size);
if (connfd < 0)
error ("ERROR on accept");
int read1, read2, read3;
//read1 = read (connfd, &operator, 10);
read1 = recv (connfd, &operator, 10, 0);
// printf ("read1: %d\n", read1);
read2 = recv (connfd, &op1, sizeof (op1), 0);
// read2 = read (connfd, &op1, sizeof (op1));
// printf ("read2: %d\n", read2);
read3 = recv (connfd, &op2, sizeof (op2), 0);
// read3 = read (connfd, &op2, sizeof (op2));
// printf ("read3: %d\n", read3);
printf ("op1: %d, op2: %d\n", op1, op2);
switch (operator)
{
case '+':
result = op1 + op2;
printf ("Result sum is: %d + %d = %d\n", op1, op2, result);
break;
case '-':
result = op1 - op2;
printf ("Result differnce is: %d - %d = %d\n", op1, op2, result);
break;
case '*':
result = op1 * op2;
printf ("Result multiplication is: %d * %d = %d\n", op1, op2, result);
break;
case '/':
result = op1 / op2;
printf ("Result division is: %d / %d = %d\n", op1, op2, result);
break;
default:
printf ("ERROR: Unsupported Operation");
}
send (connfd, &result, sizeof (result), 0);
// write (connfd, &result, sizeof (result));
/* close() */
close (sockfd);
}