static void tcpsend_connect(t_tcpsend *x, t_symbol *hostname, t_floatarg fportno) { struct sockaddr_in server; struct hostent *hp; int sockfd; int portno = fportno; int intarg; memset(&server, 0, sizeof(server)); if (x->x_fd >= 0) { iemnet_log(x, IEMNET_ERROR, "already connected"); return; } /* resolve hostname provided as argument */ server.sin_family = AF_INET; hp = gethostbyname(hostname->s_name); if (hp == 0) { iemnet_log(x, IEMNET_ERROR, "bad host '%s'?", hostname->s_name); return; } /* create a socket */ sockfd = socket(AF_INET, SOCK_STREAM, 0); DEBUG("send socket %d\n", sockfd); if (sockfd < 0) { iemnet_log(x, IEMNET_ERROR, "unable to open socket"); sys_sockerror("socket"); return; } /* for stream (TCP) sockets, specify "nodelay" */ intarg = 1; if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&intarg, sizeof(intarg)) < 0) { iemnet_log(x, IEMNET_ERROR, "unable to enable immediate sending"); sys_sockerror("setsockopt"); } /* connect socket using hostname provided as argument */ memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); /* assign client port number */ server.sin_port = htons((u_short)portno); iemnet_log(x, IEMNET_VERBOSE, "connecting to port %d", portno); /* try to connect. */ if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0) { iemnet_log(x, IEMNET_ERROR, "unable to initiate connection on socket %d", sockfd); sys_sockerror("connect"); iemnet__closesocket(sockfd, 1); return; } x->x_fd = sockfd; x->x_sender=iemnet__sender_create(sockfd, NULL, NULL, 0); outlet_float(x->x_obj.ob_outlet, 1); }
static void tcpsend_connect(t_tcpsend *x, t_symbol *hostname, t_floatarg fportno) { struct sockaddr_in server; struct hostent *hp; int sockfd; int portno = fportno; int intarg; memset(&server, 0, sizeof(server)); if (x->x_fd >= 0) { error("tcpsend: already connected"); return; } /* create a socket */ sockfd = socket(AF_INET, SOCK_STREAM, 0); DEBUG("send socket %d\n", sockfd); if (sockfd < 0) { sys_sockerror("tcpsend: socket"); return; } /* connect socket using hostname provided in command line */ server.sin_family = AF_INET; hp = gethostbyname(hostname->s_name); if (hp == 0) { post("tcpsend: bad host?\n"); return; } /* for stream (TCP) sockets, specify "nodelay" */ intarg = 1; if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&intarg, sizeof(intarg)) < 0) post("tcpsend: setsockopt (TCP_NODELAY) failed\n"); memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); /* assign client port number */ server.sin_port = htons((u_short)portno); post("tcpsend: connecting to port %d", portno); /* try to connect. */ if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0) { sys_sockerror("tcpsend: connecting stream socket"); sys_closesocket(sockfd); return; } x->x_fd = sockfd; x->x_sender=iemnet__sender_create(sockfd, 0); outlet_float(x->x_obj.ob_outlet, 1); }
static void *udpclient_child_connect(void *w) { t_udpclient *x = (t_udpclient*) w; struct sockaddr_in server; struct hostent *hp; int sockfd; int broadcast = 1;/* nonzero is true */ if (x->x_sender) { error("[%s] already connected", objName); return (x); } /* create a socket */ sockfd = socket(AF_INET, SOCK_DGRAM, 0); DEBUG("send socket %d\n", sockfd); if (sockfd < 0) { sys_sockerror("udpclient: socket"); return (x); } /* Based on zmoelnig's patch 2221504: Enable sending of broadcast messages (if hostname is a broadcast address)*/ #ifdef SO_BROADCAST if( 0 != setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (const void *)&broadcast, sizeof(broadcast))) { error("[%s] couldn't switch to broadcast mode", objName); } #endif /* SO_BROADCAST */ /* connect socket using hostname provided in command line */ server.sin_family = AF_INET; hp = gethostbyname(x->x_hostname); if (hp == 0) { error("[%s] bad host '%s'?", objName, x->x_hostname); return (x); } memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); /* assign client port number */ server.sin_port = htons((u_short)x->x_port); DEBUG("connecting to port %d", x->x_port); /* try to connect. */ if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0) { sys_sockerror("udpclient: connecting stream socket"); sys_closesocket(sockfd); return (x); } x->x_fd = sockfd; x->x_addr = ntohl(*(long *)hp->h_addr); x->x_sender=iemnet__sender_create(sockfd); x->x_receiver=iemnet__receiver_create(sockfd, x, udpclient_receive_callback); x->x_connectstate = 1; clock_delay(x->x_clock, 0); return (x); }
static void udpsend_connect(t_udpsend *x, t_symbol *hostname, t_floatarg fportno) { struct sockaddr_in server; int sockfd; int portno = fportno; int broadcast = 1;/* nonzero is true */ if (x->x_sender) { error("[%s] already connected", objName); return; } /* create a socket */ sockfd = socket(AF_INET, SOCK_DGRAM, 0); DEBUG("send socket %d\n", sockfd); if (sockfd < 0) { sys_sockerror("[udpsend] socket"); return; } /* Based on zmoelnig's patch 2221504: Enable sending of broadcast messages (if hostname is a broadcast address)*/ #ifdef SO_BROADCAST if( 0 != setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (const void *)&broadcast, sizeof(broadcast))) { error("[%s] couldn't switch to broadcast mode", objName); } #endif /* SO_BROADCAST */ /* connect socket using hostname provided in command line */ server.sin_family = AF_INET; do { #if 0 struct addrinfo * addr=NULL; if(getaddrinfo(hostname->s_name, NULL, NULL, &addr)) { error("[%s] bad host '%s'?", objName, hostname->s_name); return; } else { struct addrinfo * res; for (res = addr; res != NULL; res = res->ai_next) { struct sockaddr_in *sa = (struct sockaddr_in *) res->ai_addr; int len = res->ai_addrlen; // memcpy((char *)&server.sin_addr, (char *)res->ai_addr, hp->h_length); // LATER check how to do that... } } freeaddrinfo(addr); #else struct hostent *hp = gethostbyname(hostname->s_name); if (hp == 0) { error("[%s] bad host '%s'?", objName, hostname->s_name); return; } memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); #endif } while(0); /* assign client port number */ server.sin_port = htons((u_short)portno); DEBUG("connecting to port %d", portno); /* try to connect. */ if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0) { sys_sockerror("[udpsend] connecting stream socket"); sys_closesocket(sockfd); return; } x->x_sender=iemnet__sender_create(sockfd, 0); outlet_float(x->x_obj.ob_outlet, 1); }
static void *tcpclient_child_connect(void *w) { t_tcpclient *x = (t_tcpclient*) w; struct sockaddr_in server; struct hostent *hp; int sockfd; t_iemnet_sender*sender; t_iemnet_receiver*receiver; if (x->x_fd >= 0) { error("%s_connect: already connected", objName); return (x); } /* create a socket */ sockfd = socket(AF_INET, SOCK_STREAM, 0); DEBUG("send socket %d\n", sockfd); if (sockfd < 0) { sys_sockerror("tcpclient: socket"); return (x); } /* connect socket using hostname provided in command line */ server.sin_family = AF_INET; hp = gethostbyname(x->x_hostname); if (hp == 0) { sys_sockerror("tcpclient: bad host?\n"); return (x); } memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); /* assign client port number */ server.sin_port = htons((u_short)x->x_port); /* try to connect */ if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0) { sys_sockerror("tcpclient: connecting stream socket"); sys_closesocket(sockfd); return (x); } sender=iemnet__sender_create(sockfd); receiver=iemnet__receiver_create(sockfd, x, tcpclient_receive_callback); /* update the tcpclient-object (thread safe) */ sys_lock(); x->x_fd = sockfd; x->x_addr = ntohl(*(long *)hp->h_addr); x->x_sender=sender; x->x_receiver=receiver; x->x_connectstate = 1; /* use callback to set outlet in main thread */ clock_delay(x->x_clock, 0); sys_unlock(); return (x); }