-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathechoservert.c
48 lines (43 loc) · 1.24 KB
/
echoservert.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
/*
* echoservert.c - A concurrent echo server using threads
*/
#include "csapp.h"
#include "echo.h"
void *echo_thread(void *vargp);
int main(int argc, char **argv)
{
int port = atoi(argv[1]);
struct sockaddr_in clientaddr;
int clientlen=sizeof(clientaddr);
pthread_t tid;
int listenfd = Open_listenfd(port);
while (1) {
int *connfdp = Malloc(sizeof(int));
struct hostent *hp;
char *haddrp;
int client_port;
*connfdp = Accept(listenfd, (SA *) &clientaddr, &clientlen);
/* determine the domain name and IP address of the client */
hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
sizeof(clientaddr.sin_addr.s_addr), AF_INET);
haddrp = inet_ntoa(clientaddr.sin_addr);
client_port = ntohs(clientaddr.sin_port);
printf("Server connected to %s (%s), port %d\n",
hp->h_name, haddrp, client_port);
Pthread_create(&tid, NULL, echo_thread, connfdp);
}
}
/* thread routine */
void *echo_thread(void *vargp)
{
char prefix[40];
pthread_t tid = pthread_self();
int connfd = *((int *)vargp);
Pthread_detach(pthread_self());
Free(vargp);
printf("Served by thread %d\n", (int) tid);
sprintf(prefix, "Thread %d ", (int) tid);
echo(connfd, prefix);
Close(connfd);
return NULL;
}