3 * Copyright (c) 2006 acmesystems.it - john@acmesystems.it
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19 * Feedback, Bugs... info@acmesystems.it
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
37 #define SOCKET_PORT 369
40 typedef struct _MP3_TCP_SOCKET {
47 static MP3_TCP_SOCKET mp3_tcp_socket;
49 int mp3_tcp_socket_setup(void){
50 struct sockaddr_in myaddr;
52 FD_ZERO(&mp3_tcp_socket.master);
53 FD_ZERO(&mp3_tcp_socket.clients);
55 if ((mp3_tcp_socket.listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
59 if (setsockopt(mp3_tcp_socket.listener, SOL_SOCKET,
60 SO_REUSEADDR, &yes, sizeof(int)) == -1) {
64 myaddr.sin_family = AF_INET;
65 myaddr.sin_port = htons(SOCKET_PORT);
66 myaddr.sin_addr.s_addr = INADDR_ANY;
67 memset(&(myaddr.sin_zero), '\0', 8);
68 if (bind(mp3_tcp_socket.listener, (struct sockaddr *)&myaddr, sizeof(struct sockaddr)) == -1) {
72 if (listen(mp3_tcp_socket.listener, 3) == -1) {
76 FD_SET(mp3_tcp_socket.listener, &mp3_tcp_socket.master);
77 mp3_tcp_socket.max = mp3_tcp_socket.listener;
78 printf("Started tcp socket on port %d\n", SOCKET_PORT);
82 int mp3_tcp_socket_handle(void){
83 struct sockaddr_in remoteaddr;
94 mp3_tcp_socket.clients = mp3_tcp_socket.master;
96 if (select(mp3_tcp_socket.max + 1, &mp3_tcp_socket.clients,
97 NULL, NULL, &tv) == -1) {
100 for(i = 0; i <= mp3_tcp_socket.max; i++) {
101 if (FD_ISSET(i, &mp3_tcp_socket.clients)) {
102 if (i == mp3_tcp_socket.listener) {
103 addrlen = sizeof(remoteaddr);
104 if ((newfd = accept(mp3_tcp_socket.listener,
105 (struct sockaddr *)&remoteaddr,
107 perror("error whilst accepting socket");
110 printf("New TCP connection from %s\n",
111 inet_ntoa(remoteaddr.sin_addr));
112 FD_SET(newfd, &mp3_tcp_socket.master);
113 if (newfd > mp3_tcp_socket.max) {
114 mp3_tcp_socket.max = newfd;
118 if ((nbytes = recv(i, buf, sizeof(buf), 0)) <= 0) {
120 perror("selectserver: socket hung up\n");
122 perror("error whilst receiving socket");
125 FD_CLR(i, &mp3_tcp_socket.master);
128 printf("Got data : %s\n", buf);
129 mp3_parser_incoming(buf,buf_out);
130 if(*buf_out != '\0'){
131 send(i, buf_out, strlen(buf_out), 0);
134 FD_CLR(i, &mp3_tcp_socket.master);
142 int mp3_tcp_socket_cleanup(void){