/
quote_server.c
206 lines (165 loc) · 6.38 KB
/
quote_server.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
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
/**********************************************************************
* quote_server.c --- Server portion of quote_client. Listens and
creates threads upon requests.
* - change SERVER_PORT value to that of your server
**********************************************************************/
/* Requisite includes */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#define SERVER_PORT 5001 /* CHANGE THIS TO THE PORT OF YOUR SERVER */
#define BUFFER_SIZE 1024
/*
* Sockets
o socket()
o bind()
o accept()
o listen()
o close()
* Threads
o pthread_create(pthread_exit()
* File locking
o flock()
o funlock()
o fcntl()
* You can also use normal POSIX mutex or semaphores to deal with concurrency issues pertaining to the log file.
* File handling
o fopen()
o fgets()
*/
/**********************************************************************
* functions
**********************************************************************/
void handle_connection (int sock) {
int msgsock, rval;
int i;
char buf[512] ;
msgsock = accept(sock,0,0);
for ( i = 0 ; i < 512 ; i++) buf[i] = ' ' ; /* clear buf array with spaces */
if ( (rval = read(msgsock, buf , 512)) < 0) { /* read from msgsock into buf */
perror("while reading from socket");
return;
}
write(1,buf,512);
close(msgsock) ; /* close socket -- connection is closed and socket is deallocated. */
}
/**********************************************************************
* main
**********************************************************************/
int main(int argc, char *argv[]) {
int sock; /* fd for socket connection */
struct sockaddr_in server; /* Socket info. for server */
struct hostent *host; /* Server info */
char selection[BUFFER_SIZE]; /* User's input */
char request[BUFFER_SIZE]; /* String rec'd from client */
char response[BUFFER_SIZE]; /* String sent to client */
int finished = 0;
if (argc != 2) {
printf("Usage: %s address\n", argv[0]);
}
/* Fill out sockaddr struct */
server.sin_family = AF_INET;
if ((host = gethostbyname(argv[1])) == NULL){
printf("%s: unknown host '%s'\n", argv[0], argv[1]);
exit(1);
}
server.sin_family = host->h_addrtype;
memcpy((char *) &server.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
server.sin_port = htons((u_short) SERVER_PORT);
/* Create socket */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("%s: Unable to open socket\n",argv[0]);
perror("Aborting");
exit(1);
}
/* Try to connect */
if (connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0){
printf("%s: Unable to connect to %s\n", argv[0], argv[1]);
perror("Aborting");
exit(1);
} else {
printf("\nConnection to the quote server established\n");
}
}
void main( int argc, char* argv[] )
{
int i, ready, sock1, sock2, length;
/* This receiver process will receive connection requests on these two sockets */
int msgsock ;
fd_set fdin;
struct timeval TO ;
struct sockaddr_in server, server2;
/* CREATE FIRST SOCKET */
sock1 = socket(AF_INET, SOCK_STREAM,0);
if ( sock1 < 0 ) {
perror("Problem in creating the first socket");
exit(0);
}
server.sin_addr.s_addr = INADDR_ANY ;
server.sin_port = 0 ;
if ( bind(sock1,(struct sockaddr *)&server, sizeof(server))) { /* Bind socket to a port */
perror ( "Binding stream socket");
exit(0);
}
length = sizeof(server);
if ( getsockname(sock1,(struct sockaddr *)&server, &length)) { /* get the port number assigned to this socket */
perror("Getting socket's port number");
exit(0);
}
fprintf(stderr, "Socket 1 is connected to port%5d\n\n", ntohs(server.sin_port));
/* CREATE SECOND SOCKET */
sock2 = socket(AF_INET, SOCK_STREAM, 0);
if ( sock2 < 0 ) {
perror("problem in creating the second socket");
exit(0);
}
server2.sin_addr.s_addr = INADDR_ANY;
server2.sin_port = 0 ;
if ( bind(sock2, (struct sockaddr * )&server2,sizeof(server2))){ /* bind second socket to a port */
perror("Binding stream socket");
exit(0);
}
length = sizeof(server2);
if ( getsockname(sock2,(struct sockaddr *)&server2,&length)){ /* get the port number for socket2 */
perror("getting socket's port number");
exit(0);
}
fprintf(stderr,"Socket 2 is connected to port %5d\n", ntohs(server2.sin_port));
/* Initialize socket fo receiving messages. */
/* ready to accept connection to sock1, up to 3 requests can be kept buffered */
listen(sock1,3);
listen(sock2,3);
/* Continuously wait for receiving messages on either of the two sockets. */
while (1) {
FD_ZERO( &fdin );
FD_SET( sock1, &fdin);
FD_SET( sock2, &fdin);
TO.tv_sec = 10 ;
TO.tv_usec = 0 ;
select(32, (fd_set *)&fdin, 0, 0, &TO); /* Block to receive call on either sock1 or sock2 */
/* timeout and continue after 10 seconds if no connection requests */
if (FD_ISSET(sock1, &fdin )) { /* check if connection requests for sock1 */
printf("\nMessage on the first socket\n");
/* accept connection on sock1. This will return a new socket indentifier, stored
in msgsock for the connection. All I/O for that connection is through msgsock. */
handle_connection(sock1);
FD_CLR(sock1, &fdin);
}
else if (FD_ISSET(sock2, &fdin)) { /* check if connection requests for sock2 */
printf("\nMessage on the second socket\n");
/* accept connection on sock2. This will return a new socket indentifier, stored
in msgsock for the connection. All I/O for that connection is through msgsock. */
handle_connection(sock2);
FD_CLR(sock2, &fdin);
}
else /* Select call completed due to timeout and there is no connection on any socket */
printf("\nTimeout occured\n");
};
}