void receive_tcp_clients(char *argv[]) { while (!stop) { struct sockaddr_storage client_addr; socklen_t addr_len = sizeof(struct sockaddr_storage); int client = accept(sock, (struct sockaddr *) &client_addr, &addr_len); if (stop) { break; } if (client < 0) { exit_with_error("accept failed"); } handle_tcp_client(client, argv); } }
/* * === FUNCTION ====================================================================== * Name: main * Description: * ===================================================================================== */ int main ( int argc, char *argv[] ) { if (2 != argc) die_with_user_msg("Parameter(s)", "<Server Pot>") ; in_port_t serv_port = atoi(argv[1]) ; /* Create socket for incoming connections */ int serv_sock ; EV_TEST(-1, serv_sock, socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)) /* Construct local address structre */ struct sockaddr_in6 serv_addr ; memset(&serv_addr, 0, sizeof(serv_addr)) ; serv_addr.sin6_family = AF_INET6 ; serv_addr.sin6_addr = in6addr_any ; /* any incoming interface */ serv_addr.sin6_port = htons(serv_port) ; /* print server socket address information */ printf("server addr informeation\n") ; print_socket_addr6((struct sockaddr *)&serv_addr, stdout) ; /* Bind to the local address */ E_TEST(-1, bind(serv_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) /* Make the sockte so it will listen for incoming connections */ E_TEST(-1, listen(serv_sock, MAXPENDING)) for (;;) { struct sockaddr_in6 clnt_addr ; socklen_t clnt_addr_len = sizeof(clnt_addr) ; /* Wait for a clinet to connect */ int clnt_sock = accept(serv_sock, (struct sockaddr *)&clnt_addr, &clnt_addr_len) ; if(clnt_sock < 0) die_with_sys_msg("accept() failed") ; /* show clinet socket information */ printf("client addr informeation\n") ; print_socket_addr6((struct sockaddr *)&clnt_addr, stdout) ; handle_tcp_client(clnt_sock) ; } return EXIT_SUCCESS; } /* ---------- end of function main ---------- */
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen; struct sockaddr_in serv_addr, cli_addr; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); /* local port */ portno = atoi(argv[1]); /* construct loccal address structure */ bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); /* bind to local address */ if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); /* mark the socket so it will listen for incoming connections */ if (listen(sockfd,MAXPENDING) < 0) error("ERROR listen() failed"); //http://cs.baylor.edu/~donahoo/practical/CSockets/code/TCPEchoServer.c while (1) { /* set the size of the in-out parameter */ clilen = sizeof(cli_addr); /* wait for a client to connect */ newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); printf( "Handling client %s\n", inet_ntoa( serv_addr.sin_addr ) ); handle_tcp_client(newsockfd); /* handle (newsockfd) bzero(buffer,bufsize); n = read(newsockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); n = write(newsockfd,"I got your message",18); if (n < 0) error("ERROR writing to socket"); return 0; */ } //http://cs.baylor.edu/~donahoo/practical/CSockets/code/HandleTCPClient.c //recv is blocking ...therefore use 'read' ?! //http://www.scottklement.com/rpg/socktut/nonblocking.html //or is it the opposite ?!: //http://jeremy.zawodny.com/blog/archives/010484.html void handle_tcp_client(int clntSocket) { //char echoBuffer[bufsize]; /* Buffer for echo string */ int recvMsgSize; /* Size of received message */ //int n; char buffer[bufsize]; //char *buffer[bufsize], *tok; /* Receive message from client */ if ((recvMsgSize = recv(clntSocket, echoBuffer, bufsize, 0)) < 0) error("recv() failed"); /* Send received string and receive again until end of transmission */ while (recvMsgSize > 0) /* zero indicates end of transmission */ { /* Echo message back to client */ if (send(clntSocket, echoBuffer, recvMsgSize, 0) != recvMsgSize) error("send() failed"); /* See if there is more data to receive */ if ((recvMsgSize = recv(clntSocket, echoBuffer, bufsize, 0)) < 0) error("recv() failed"); } close(clntSocket); /* Close client socket */ }
/*********************************************** Method: p2p_tcp_server Description: The peer's tcp server. Ran as a separate thread of execution. ***********************************************/ void * p2p_tcp_server ( void * server_port ) { struct sockaddr_in serv_addr; struct sockaddr_in cli_addr; int cli_len; int * int_port; unsigned short port; int client_sock_fd; int sock_fd; /*---------------------------------------- Two step cast to avoid warnings.. ----------------------------------------*/ port = (unsigned int)server_port; if ((sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) p2p_client_abort("P2P TCP Server: socket() failed"); /* Construct local address structure */ memset(&serv_addr, 0, sizeof(serv_addr)); /* Zero out structure */ serv_addr.sin_family = AF_INET; /* Internet address family */ serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ serv_addr.sin_port = htons(port); /* Local port */ /* Bind to the local address */ if (bind(sock_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { printf("Bind failed...\n"); p2p_client_abort("P2P TCP Server: bind() failed"); } printf("P2P TCP Server: Running at port %d\n", port); while( alive ) { /* Mark the socket so it will listen for incoming connections */ if (listen(sock_fd, 10) < 0) { printf("Listen failed...\n"); p2p_client_abort("P2P TCP Server: listen() failed"); } /* Set the size of the in-out parameter */ cli_len = sizeof(cli_addr); /* Wait for a client to connect */ if ((client_sock_fd = accept(sock_fd, (struct sockaddr *)&cli_addr, &cli_len)) < 0) p2p_client_abort("P2P TCP Server: accept() failed"); /* clntSock is connected to a client! */ printf("P2P TCP Server: Handling new client %s\n", inet_ntoa(cli_addr.sin_addr)); handle_tcp_client(client_sock_fd); } printf("P2P TCP Server: Going mimis now..\n"); /*---------------------------------------- Exit execution ----------------------------------------*/ pthread_exit(NULL); return(NULL); }