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