1 | #include "server.h" |
---|
2 | |
---|
3 | int main(int argc, char* argv[]) |
---|
4 | { |
---|
5 | /* fd -> file descriptor */ |
---|
6 | int socket_server_fd = (int)NULL, |
---|
7 | socket_server_port = (int)NULL, |
---|
8 | socket_accept_fd = (int)NULL, |
---|
9 | sockaddr_in_size = sizeof(struct sockaddr_in), |
---|
10 | err = (int)NULL; |
---|
11 | |
---|
12 | /* Internet socket address stuct */ |
---|
13 | struct sockaddr_in server_addr, |
---|
14 | accept_addr; |
---|
15 | |
---|
16 | struct client * clients = (struct client *) NULL, |
---|
17 | * atual = (struct client *) NULL; |
---|
18 | pthread_t threads; |
---|
19 | |
---|
20 | /* clear screen */ |
---|
21 | system(__CLEAR_SCREEN__); |
---|
22 | |
---|
23 | if ( argc != 2 || (socket_server_port = strtol(argv[1], 0, 10)) == 0 ) |
---|
24 | usage(); |
---|
25 | |
---|
26 | /* make a socket */ |
---|
27 | socket_server_fd = socket(AF_INET, SOCK_STREAM, 0); |
---|
28 | if ( socket_server_fd == SOCKET_ERROR ) |
---|
29 | { |
---|
30 | printf("\nCould not make a socket\n"); |
---|
31 | return 0; |
---|
32 | } |
---|
33 | |
---|
34 | bzero((char *) &server_addr, sockaddr_in_size); |
---|
35 | bzero((char *) &accept_addr, sockaddr_in_size); |
---|
36 | |
---|
37 | /* fill address struct */ |
---|
38 | //server_addr.sin_addr.s_addr = inet_aton("10.15.20.42");//INADDR_ANY; |
---|
39 | //inet_aton(INADDR_ANY, &server_addr.sin_addr);//INADDR_ANY; |
---|
40 | server_addr.sin_addr.s_addr = INADDR_ANY; |
---|
41 | server_addr.sin_port = htons(socket_server_port); |
---|
42 | server_addr.sin_family = AF_INET; |
---|
43 | |
---|
44 | printf("\nBinding to port %d...\n", socket_server_port); |
---|
45 | |
---|
46 | /* bind to a port */ |
---|
47 | if ( bind(socket_server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR ) |
---|
48 | { |
---|
49 | printf("\nCould not connect to host\n"); |
---|
50 | exit(1); |
---|
51 | } |
---|
52 | |
---|
53 | /* get port number */ |
---|
54 | getsockname(socket_server_fd, (struct sockaddr *)&server_addr, (socklen_t *)&sockaddr_in_size); |
---|
55 | printf("\nopened socket as fd (%d) on port (%d) for stream i/o\n", socket_server_fd, ntohs(server_addr.sin_port)); |
---|
56 | |
---|
57 | printf("Server\n\ |
---|
58 | sin_family = %d\n\ |
---|
59 | sin_addr.s_addr = %d\n\ |
---|
60 | sin_port = %d\n" |
---|
61 | , server_addr.sin_family |
---|
62 | , server_addr.sin_addr.s_addr |
---|
63 | , ntohs(server_addr.sin_port) |
---|
64 | ); |
---|
65 | printf("\nMaking a listen queue of %d elements", QUEUE_SIZE); |
---|
66 | |
---|
67 | /* establish listen queue */ |
---|
68 | if ( listen(socket_server_fd, QUEUE_SIZE) == SOCKET_ERROR ) |
---|
69 | { |
---|
70 | printf("\nCould not listen\n"); |
---|
71 | return 0; |
---|
72 | } |
---|
73 | |
---|
74 | for ( ; ; ) |
---|
75 | { |
---|
76 | puts("\nWaiting for a connection\n"); |
---|
77 | |
---|
78 | /* get the connected socket */ |
---|
79 | socket_accept_fd = accept(socket_server_fd, (struct sockaddr *) &accept_addr, (socklen_t *) &sockaddr_in_size); |
---|
80 | if ( socket_accept_fd > -1 ) |
---|
81 | { |
---|
82 | /* |
---|
83 | char ip[46]; |
---|
84 | unsigned short port; |
---|
85 | printf("%d\n\n", inet_ntoa(server_addr.sin_addr.s_addr)); |
---|
86 | printf("%d\n\n", inet_ntoa(accept_addr.sin_addr.s_addr)); |
---|
87 | inet_ntop (AF_INET, &accept_addr.sin_addr, ip, sizeof (ip)); |
---|
88 | port = ntohs(accept_addr.sin_port); |
---|
89 | printf("connection from %s, port %hu\n", ip, port); |
---|
90 | */ |
---|
91 | |
---|
92 | |
---|
93 | /* |
---|
94 | if ( close(socket_accept_fd) == SOCKET_ERROR ) |
---|
95 | printf("\nCould not close socket\n"); |
---|
96 | */ |
---|
97 | |
---|
98 | if ( clients == (struct client *) NULL ) |
---|
99 | atual = clients; |
---|
100 | else |
---|
101 | while ( (atual = clients->next) != (struct client *) NULL ); |
---|
102 | |
---|
103 | atual = (struct client *) malloc(sizeof(struct client)); |
---|
104 | atual->next = (struct client *) NULL; |
---|
105 | atual->fd = socket_accept_fd; |
---|
106 | atual->sockaddr = accept_addr; |
---|
107 | atual->last = time(NULL); |
---|
108 | |
---|
109 | pthread_create(&threads, NULL, handler, (void *)atual); |
---|
110 | //clients = (struct client *)NULL; |
---|
111 | } |
---|
112 | puts("jah era #5"); |
---|
113 | } |
---|
114 | pthread_exit(NULL); |
---|
115 | } |
---|
116 | |
---|
117 | void * handler(void * pClient) |
---|
118 | { |
---|
119 | char pBuffer[BUFFER_SIZE]; |
---|
120 | struct client * client = (struct client *) pClient; |
---|
121 | int i = 0, err; |
---|
122 | time_t last; |
---|
123 | |
---|
124 | char ip[46]; |
---|
125 | unsigned short port; |
---|
126 | //printf("%d\n\n", inet_ntoa(client->fd)); |
---|
127 | //printf("%d\n\n", inet_ntoa(client->sockaddr.sin_addr.s_addr)); |
---|
128 | inet_ntop(AF_INET, &client->sockaddr.sin_addr, ip, sizeof (ip)); |
---|
129 | port = ntohs(client->sockaddr.sin_port); |
---|
130 | printf("\nGot a connection from %s, port %hu\n", ip, port); |
---|
131 | |
---|
132 | if ( close(client->fd) == SOCKET_ERROR ) |
---|
133 | { |
---|
134 | printf("\nCould not close socket\n"); |
---|
135 | return 0; |
---|
136 | } |
---|
137 | |
---|
138 | fflush(stdin); |
---|
139 | fflush(stdout); |
---|
140 | free(client); |
---|
141 | pthread_exit(NULL); |
---|
142 | /* |
---|
143 | |
---|
144 | last = time(NULL); |
---|
145 | while ( time(NULL) - client->last < 10 ) |
---|
146 | { |
---|
147 | printf("%s\n\n", "teste"); |
---|
148 | sleep(1); |
---|
149 | } |
---|
150 | |
---|
151 | client->last = time(NULL); |
---|
152 | */ |
---|
153 | |
---|
154 | /** |
---|
155 | * number returned by read() and write() is the number of bytes |
---|
156 | * read or written, with -1 being that an error occured |
---|
157 | * write what we received back to the server |
---|
158 | **/ |
---|
159 | |
---|
160 | /* |
---|
161 | strcpy(pBuffer, MESSAGE); |
---|
162 | printf("\nSending \"%s\" to client\n",pBuffer); |
---|
163 | write(client->hSocket, pBuffer, strlen(pBuffer)+1); |
---|
164 | */ |
---|
165 | |
---|
166 | /* read from socket into buffer */ |
---|
167 | /*read(client->hSocket, pBuffer, sizeof(pBuffer)); |
---|
168 | |
---|
169 | printf("Received from client: %s\n", pBuffer); |
---|
170 | |
---|
171 | if ( strcmp(pBuffer, MESSAGE) == 0 ) |
---|
172 | printf("The messages match\n"); |
---|
173 | else |
---|
174 | printf("Something was changed in the message\n"); |
---|
175 | */ |
---|
176 | |
---|
177 | /*while (i < 10) |
---|
178 | { |
---|
179 | printf("%d\n", i); |
---|
180 | usleep(1000000); |
---|
181 | i++; |
---|
182 | }*/ |
---|
183 | |
---|
184 | /* close socket */ |
---|
185 | /*printf("\nClosing the socket"); |
---|
186 | if ( close(client->hSocket) == SOCKET_ERROR ) |
---|
187 | { |
---|
188 | printf("\nCould not close socket\n"); |
---|
189 | return 0; |
---|
190 | } |
---|
191 | |
---|
192 | fflush(stdin); |
---|
193 | fflush(stdout); |
---|
194 | free(client); |
---|
195 | pthread_exit(NULL); |
---|
196 | */ |
---|
197 | } |
---|