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
|
...
void* thread_proc(void *arg);
...
pthread_t thread_id;
/* create a streaming socket */
simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int sockOp = setsockopt(simpleSocket, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
/* retrieve the port number for listening */
simplePort = atoi(argv[1]);
/* setup the address structure */
/* user INADDR_ANY to bind all local addresses */
bzero(&simpleServer, sizeof(simpleServer));
simpleServer.sin_family = AF_INET;
simpleServer.sin_addr.s_addr = htonl(INADDR_ANY);
simpleServer.sin_port = htons(simplePort);
/* bind to the address and port with our socket */
returnStatus = bind(simpleSocket, (struct sockaddr *)&simpleServer, sizeof(simpleServer));
/* tell the socket we are ready to accept connections */
returnStatus = listen(simpleSocket, 5);
int result;
/* ACCEPT */
while (1) {
simpleChildSocket = accept(simpleSocket,NULL, NULL);
result = pthread_create(&thread_id, NULL, thread_proc, (void *) simpleChildSocket);
pthread_detach(thread_id);
sched_yield();
} //while
void* thread_proc(void *arg) {
int sock = (int) arg;
/* handle the new connection request */
/* receive remote polling location ID from the client */
int rcvBytes;
unsigned char rcvBuffer[16600];
rcvBytes = read(sock, rcvBuffer, sizeof(rcvBuffer));
....
/* close socket */
close(simpleChildSocket);
} //thread_proc
|