int ACE_TMAIN(int argc, ACE_TCHAR* argv[]){ ACE_SOCK_Acceptor acceptor; ACE_INET_Addr addr(10009); if(acceptor.open(addr,1)){ ACE_ERROR_RETURN((LM_ERROR, "%p\n", "open"),1); } else if(acceptor.get_local_addr(addr) == -1){ ACE_ERROR_RETURN((LM_ERROR, "%p\n", "get_local_addr"),1); } ACE_DEBUG((LM_INFO, "(%P|%t) starting server at port %d\n",addr.get_port_number())); ACE_Thread_Manager* mgr = ACE_Thread_Manager::instance(); while(true){ ACE_SOCK_Stream stream; if(acceptor.accept(stream) == -1){ ACE_ERROR((LM_ERROR, "%p\n","accept")); continue; }else{ ACE_DEBUG((LM_DEBUG, "(%P|%t) spawning one thread\n")); handle_input(mgr, commu, stream.get_handle()); } } return 0; }
int SocketTwoWayStream::open(ACE_SOCK_Acceptor& acceptor) { int result = acceptor.accept(stream); if (result>=0) { happy = true; } updateAddresses(); return result; }
int main(int argc, char* argv[]){ if (argc > 1){ return 1; } cout << argv[0] << endl; ACE_INET_Addr addr(1234, ACE_LOCALHOST); ACE_SOCK_Stream stream; ACE_SOCK_Acceptor acceptor; int success = acceptor.open(addr, 1); ACE_TCHAR addrStr[20]; if (success > 0) { addr.addr_to_string(addrStr, 20); } //* success = acceptor.accept(stream); if (success < 0) { cout << "Cannot accept" << endl; return 1; } //*/ char buf[BUFSIZ]; int n; char *msg = "You are connected"; stream.send_n(msg, strlen(msg)); stream.close(); /* while (stream.recv(buf, BUFSIZ)) { // _write(1, buf, n); cout << buf << endl; } //*/ cout << endl << "Done" << endl; return 0; }
int Pipe::open (void) { ACE_INET_Addr my_addr; ACE_SOCK_Acceptor acceptor; ACE_SOCK_Connector connector; ACE_SOCK_Stream reader; ACE_SOCK_Stream writer; int result = 0; // Bind listener to any port and then find out what the port was. if (acceptor.open (ACE_Addr::sap_any) == -1 || acceptor.get_local_addr (my_addr) == -1) result = -1; else { int af = my_addr.get_type (); const ACE_TCHAR *local = ACE_LOCALHOST; #if defined (ACE_HAS_IPV6) if (af == AF_INET6) local = ACE_IPV6_LOCALHOST; #endif /* ACE_HAS_IPV6 */ ACE_INET_Addr sv_addr (my_addr.get_port_number (), local, af); // Establish a connection within the same process. if (connector.connect (writer, sv_addr) == -1) result = -1; else if (acceptor.accept (reader) == -1) { writer.close (); result = -1; } } // Close down the acceptor endpoint since we don't need it anymore. acceptor.close (); if (result == -1) return -1; this->handles_[0] = reader.get_handle (); this->handles_[1] = writer.get_handle (); return 0; }
int accept_connections(){ if (peer_acceptor_.get_local_addr(server_addr_) == -1){ ACE_ERROR_RETURN((LM_ERROR, "%p\n", "Error in get_local_addr"), 1); } ACE_DEBUG((LM_DEBUG, "Starting server at port %d\n", server_addr_.get_port_number())); while (1){ ACE_Time_Value timeout(ACE_DEFAULT_TIMEOUT); if (peer_acceptor_.accept(new_stream_, &client_addr_, &timeout)){ ACE_ERROR((LM_ERROR, "%p\n", "accept")); continue; } else{ ACE_DEBUG((LM_DEBUG, "Connection established with remote %s:%d\n", client_addr_.get_host_name(), client_addr_.get_port_number())); handle_connection(); } } }
int main() { ACE_INET_Addr server_addr; ACE_SOCK_Acceptor acceptor; ACE_SOCK_Stream peer; if (-1 == server_addr.set(22334)) { log("server_addr.set faild\n"); return 1; } if (-1 == acceptor.open(server_addr)) { log("acceptor.open failed\n"); return 1; } while(1) { if (-1 == acceptor.accept(peer)) { log("acceptor.accept failed\n"); return 1; } peer.disable(ACE_NONBLOCK); auto_ptr<char> pathname(get_url_pathname(&peer)); ACE_Mem_Map mapped_file(pathname.get()); if (-1 == (peer.send_n(mapped_file.addr(), mapped_file.size()))) { log("peer.send_n failed\n"); return 1; } peer.close(); } return acceptor.close() == -1 ? 1 : 0; }
int Peer_Handler::open (void *) { if (host_ != 0) // Connector { ACE_INET_Addr addr (port_, host_); ACE_SOCK_Connector connector; // Establish connection with server. if (connector.connect (stream_, addr) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "connect"), -1); ACE_DEBUG ((LM_DEBUG, "(%t) connected.\n")); } else // Acceptor { ACE_SOCK_Acceptor acceptor; ACE_INET_Addr local_addr (port_); if ((acceptor.open (local_addr) == -1) || (acceptor.accept (this->stream_) == -1)) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "accept failed"), -1); ACE_DEBUG ((LM_DEBUG, "(%t) accepted.\n")); } int result = this->rd_stream_.open (*this); if (result != 0) return result; result = this->wr_stream_.open (*this); if (result != 0) return result; result = this->rd_stream_.read (this->mb_, this->mb_.size ()); return result; }
int Handler_Factory::create_handler (ACE_SOCK_Acceptor &acceptor, Handler * (*handler_factory) (ACE_HANDLE), const char *handler_type) { ACE_SOCK_Stream new_stream; if (acceptor.accept (new_stream) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "accept"), -1); Handler *handler; ACE_ALLOCATOR_RETURN (handler, (*handler_factory) (new_stream.get_handle ()), -1); ACE_DEBUG ((LM_DEBUG, "(%P|%t) spawning %s handler\n", handler_type)); if (handler->open () == -1) return -1; #if defined (ACE_MT_SAFE) // Spawn a new thread and run the new connection in that thread of // control using the <server> function as the entry point. return handler->activate (); #else handler->svc (); handler->close (0); return 0; #endif /* ACE_HAS_THREADS */ }
int ACE_Pipe::open (int buffer_size) { ACE_TRACE ("ACE_Pipe::open"); #if defined (ACE_LACKS_SOCKETPAIR) ACE_INET_Addr my_addr; ACE_SOCK_Acceptor acceptor; ACE_SOCK_Connector connector; ACE_SOCK_Stream reader; ACE_SOCK_Stream writer; int result = 0; # if defined (ACE_WIN32) ACE_INET_Addr local_any (static_cast<u_short> (0), ACE_LOCALHOST); # else ACE_Addr local_any = ACE_Addr::sap_any; # endif /* ACE_WIN32 */ // Bind listener to any port and then find out what the port was. if (acceptor.open (local_any) == -1 || acceptor.get_local_addr (my_addr) == -1) result = -1; else { ACE_INET_Addr sv_addr (my_addr.get_port_number (), ACE_LOCALHOST); // Establish a connection within the same process. if (connector.connect (writer, sv_addr) == -1) result = -1; else if (acceptor.accept (reader) == -1) { writer.close (); result = -1; } } // Close down the acceptor endpoint since we don't need it anymore. acceptor.close (); if (result == -1) return -1; this->handles_[0] = reader.get_handle (); this->handles_[1] = writer.get_handle (); # if !defined (ACE_LACKS_TCP_NODELAY) int one = 1; // Make sure that the TCP stack doesn't try to buffer small writes. // Since this communication is purely local to the host it doesn't // affect network performance. if (writer.set_option (ACE_IPPROTO_TCP, TCP_NODELAY, &one, sizeof one) == -1) { this->close (); return -1; } # endif /* ! ACE_LACKS_TCP_NODELAY */ # if defined (ACE_LACKS_SO_RCVBUF) && defined (ACE_LACKS_SO_SNDBUF) ACE_UNUSED_ARG (buffer_size); # endif # if !defined (ACE_LACKS_SO_RCVBUF) if (reader.set_option (SOL_SOCKET, SO_RCVBUF, reinterpret_cast <void *> (&buffer_size), sizeof (buffer_size)) == -1 && errno != ENOTSUP) { this->close (); return -1; } # endif /* !ACE_LACKS_SO_RCVBUF */ # if !defined (ACE_LACKS_SO_SNDBUF) if (writer.set_option (SOL_SOCKET, SO_SNDBUF, reinterpret_cast <void *> (&buffer_size), sizeof (buffer_size)) == -1 && errno != ENOTSUP) { this->close (); return -1; } # endif /* !ACE_LACKS_SO_SNDBUF */ #elif defined (ACE_HAS_STREAM_PIPES) || defined (__QNX__) ACE_UNUSED_ARG (buffer_size); if (ACE_OS::pipe (this->handles_) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("pipe")), -1); #if !defined(__QNX__) int arg = RMSGN; // Enable "msg no discard" mode, which ensures that record // boundaries are maintained when messages are sent and received. if (ACE_OS::ioctl (this->handles_[0], I_SRDOPT, (void *) arg) == -1 || ACE_OS::ioctl (this->handles_[1], I_SRDOPT, (void *) arg) == -1) { this->close (); ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("ioctl")), -1); } #endif /* __QNX__ */ #else /* ! ACE_LACKS_SOCKETPAIR && ! ACE_HAS_STREAM_PIPES */ if (ACE_OS::socketpair (AF_UNIX, SOCK_STREAM, 0, this->handles_) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("socketpair")), -1); # if defined (ACE_LACKS_SO_SNDBUF) && defined (ACE_LACKS_SO_RCVBUF) ACE_UNUSED_ARG (buffer_size); # endif # if !defined (ACE_LACKS_SO_RCVBUF) if (ACE_OS::setsockopt (this->handles_[0], SOL_SOCKET, SO_RCVBUF, reinterpret_cast <const char *> (&buffer_size), sizeof (buffer_size)) == -1 && errno != ENOTSUP) { this->close (); return -1; } # endif # if !defined (ACE_LACKS_SO_SNDBUF) if (ACE_OS::setsockopt (this->handles_[1], SOL_SOCKET, SO_SNDBUF, reinterpret_cast <const char *> (&buffer_size), sizeof (buffer_size)) == -1 && errno != ENOTSUP) { this->close (); return -1; } # endif /* ! ACE_LACKS_SO_SNDBUF */ # if defined (ACE_OPENVMS) && !defined (ACE_LACKS_TCP_NODELAY) int one = 1; // OpenVMS implements socketpair(AF_UNIX...) by returning AF_INET sockets. // Since these are plagued by Nagle as any other INET socket we need to set // TCP_NODELAY on the write handle. if (ACE_OS::setsockopt (this->handles_[1], ACE_IPPROTO_TCP, TCP_NODELAY, reinterpret_cast <const char *> (&one), sizeof (one)) == -1) { this->close (); return -1; } # endif /* ACE_OPENVMS && !ACE_LACKS_TCP_NODELAY */ #endif /* ! ACE_LACKS_SOCKETPAIR && ! ACE_HAS_STREAM_PIPES */ // Point both the read and write HANDLES to the appropriate socket // HANDLEs. return 0; }
int ACE_TMAIN(int argc, ACE_TCHAR *argv[]) { ACE_SOCK_Stream connection_stream; int c; printf("HZ = %d\n", HZ); if (argc < 2) goto usage; while ((c = getopt (argc, argv, "drstU:uvBDTb:f:l:n:p:A:O:L:xh:")) != -1) { switch (c) { case 'h': host = optarg; break; case 'x': new_line = 1; break; case 'L': title = optarg; break; case 'B': b_flag = 1; break; case 't': trans = 1; break; case 'r': trans = 0; break; case 'd': options |= SO_DEBUG; break; case 'D': #ifdef TCP_NODELAY nodelay = 1; #else fprintf (stderr, "ttcp: -D option ignored: TCP_NODELAY socket option not supported\n"); #endif break; case 'n': nbuf = atoi (optarg); break; case 'l': data_buf_len = atoi (optarg); break; case 's': sinkmode = !sinkmode; break; case 'p': port = atoi (optarg); break; case 'U': domain = PF_UNIX; domainname = optarg; break; case 'u': udp = 1; break; case 'v': verbose = 1; break; case 'A': bufalign = atoi (optarg); break; case 'O': bufoffset = atoi (optarg); break; case 'b': #if defined(SO_SNDBUF) || defined(SO_RCVBUF) sockbufsize = atoi (optarg); #else fprintf (stderr, "ttcp: -b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported\n"); #endif break; case 'f': fmt = *optarg; break; case 'T': touchdata = 1; break; default: goto usage; } } /* if transmitter, create remote address to transmit to. */ if (trans) { if (address.set (port, host) == -1) perror ("address.set"), exit (1); } /* else, receiver create address to listen on */ else { address.set (port); } total_msg_len = sizeof (long) + data_buf_len; // allocate the buffer message_buf = (Data_Control_Message *) malloc (total_msg_len); if (message_buf == 0) err ("malloc"); // if (bufalign != 0) // message_buf += (bufalign - ((int) message_buf % bufalign) + bufoffset) % bufalign; // let's go ahead and set the control message for every send right now message_buf->size_ = data_buf_len; session_control_buf.nbuf_ = nbuf; session_control_buf.size_ = data_buf_len; // // print out option values for trans and receiver // if (trans) { fprintf (stdout, "ttcp-t: data_buf_len=%d, nbuf=%d, align=%d/%d, port=%d", data_buf_len, nbuf, bufalign, bufoffset, port); if (sockbufsize) fprintf (stdout, ", sockbufsize=%d", sockbufsize); fprintf (stdout, " %s -> %s\n", domain == PF_INET ? (udp ? "udp" : "tcp") : "unix", host == 0 ? domainname : host); } else // receiver { fprintf (stdout, "ttcp-r: data_buf_len=%d, nbuf=%d, align=%d/%d, port=%d", data_buf_len, nbuf, bufalign, bufoffset, port); if (sockbufsize) fprintf (stdout, ", sockbufsize=%d", sockbufsize); fprintf (stdout, " %s\n", domain == PF_INET ? (udp ? "udp" : "tcp") : "unix"); } mes ("socket"); // // connect and accept // if (!udp) { signal (SIGPIPE, (SIG_TYP) sigpipe); /* the transmitter will set options and connect to receiver */ if (trans) { // turn off weird ack things if (nodelay) { struct protoent *p = getprotobyname ("tcp"); if (p && connection_stream.set_option (p->p_proto, TCP_NODELAY, (char *)& one, sizeof (one))) err ("setsockopt: nodelay"); mes ("nodelay"); } if (connector_factory.connect (connection_stream, address) == -1) perror ("connection failed"), exit (1); fprintf (stdout, "ttcp-t: data_buf_len=%d, nbuf=%d, align=%d/%d, port=%d", data_buf_len, nbuf, bufalign, bufoffset, port); if (sockbufsize) { if (connection_stream.set_option (SOL_SOCKET, SO_SNDBUF, (char *) &sockbufsize, sizeof sockbufsize) == -1) err ("acceptor_factory.set_option"); mes ("sndbuf"); } } /* receiver will listen for connections from the transmitter */ else { if (acceptor_factory.open (address, 1) == -1) perror ("acceptor open"), exit (1); if (sockbufsize) { if (connection_stream.set_option (SOL_SOCKET, SO_RCVBUF, (char *) &sockbufsize, sizeof sockbufsize) == -1) err ("acceptor_factory.set_option"); mes ("rcvbuf"); } ACE_INET_Addr remote_address; if (acceptor_factory.accept (connection_stream, (ACE_Addr *) &remote_address) == -1) perror ("acceptor accept"), exit (1); // set the window size fprintf (stderr, "ttcp-r: accept from %s\n", remote_address.get_host_name()); } } // // start timer // errno = 0; if (trans) { pattern (& (message_buf->data_), data_buf_len); prep_timer (); ACE_DEBUG ((LM_DEBUG, "Sending session control message" " nbuf %d, size %d\n", session_control_buf.nbuf_, session_control_buf.size_)); if (connection_stream.send_n ((char *) &session_control_buf, sizeof (Session_Control_Message)) != sizeof (Session_Control_Message)) ACE_ERROR_RETURN ((LM_ERROR, "%p send session control failed\n", "ttcp"), -1); long ack; int send_result; while (nbuf--) { send_result = connection_stream.send_n ((char *) message_buf, total_msg_len); if (send_result != total_msg_len) ACE_ERROR_RETURN ((LM_ERROR, "%p only sent %d of %d bytes on call %d\n", "ttcp", send_result, total_msg_len, numCalls + 1), -1); numCalls++; nbytes += data_buf_len; if (connection_stream.recv_n ((char *) &ack, sizeof ack) != sizeof ack) ACE_ERROR_RETURN ((LM_ERROR, "%p recv of ack failed\n", "ttcp"), -1); if (ack != data_buf_len) ACE_DEBUG ((LM_DEBUG, "received ack for only %d bytes\n", ack)); } printf("Client finished.\n"); } else { prep_timer (); if (connection_stream.recv_n ((char *) &session_control_buf, sizeof (Session_Control_Message)) != sizeof (Session_Control_Message)) ACE_ERROR_RETURN ((LM_ERROR, "%p recv session control failed\n", "ttcp"), -1); ACE_DEBUG ((LM_DEBUG, "received session control message" " nbuf %d, size %d\n", session_control_buf.nbuf_, session_control_buf.size_)); nbuf = session_control_buf.nbuf_; // ignore session_control_buf.size_ for now long cnt; while (nbuf--) { if (connection_stream.recv_n ((char *) message_buf, sizeof (long)) != sizeof (long)) ACE_ERROR_RETURN ((LM_ERROR, "%p recv data control failed\n", "ttcp"), -1); cnt = connection_stream.recv_n (& (message_buf->data_), message_buf->size_); if (cnt != message_buf->size_) ACE_ERROR_RETURN ((LM_ERROR, "recv data failed\n"), -1); numCalls++; nbytes += cnt; if (connection_stream.send_n ((char *) &cnt, sizeof cnt) != sizeof cnt) ACE_ERROR_RETURN ((LM_ERROR, "%p send ack failed\n", "ttcp"), -1); } printf("Server finished.\n"); } /* if (errno) err ("IO"); */ // // stop the timer // (void) read_timer (stats, sizeof (stats)); if (udp && trans) { (void) Nwrite (connection_stream, message_buf, 4); /* rcvr end */ (void) Nwrite (connection_stream, message_buf, 4); /* rcvr end */ (void) Nwrite (connection_stream, message_buf, 4); /* rcvr end */ (void) Nwrite (connection_stream, message_buf, 4); /* rcvr end */ } if (cput <= 0.0) cput = 0.001; if (realt <= 0.0) realt = 0.001; #if defined (LM_RESULTS) if (trans && (title != 0)) { double tmp; FILE *fd; char filename[BUFSIZ]; ACE_OS::sprintf (filename, "%s.results", title); fd = fopen(filename,"a+"); if (new_line) fprintf(fd,"\n -l %ldk \t", data_buf_len/1024); tmp = ((double) nbytes) / realt; fprintf(fd,"%.2f ", tmp * 8.0 / 1024.0 / 1024.0); fclose(fd); } #endif fprintf (stdout, "ttcp%s: %ld bytes in %.2f real seconds = %s/sec +++\n", trans ? "-t" : "-r", nbytes, realt, outfmt (((double) nbytes) / realt)); if (verbose) { fprintf (stdout, "ttcp%s: %ld bytes in %.2f CPU seconds = %s/cpu sec\n", trans ? "-t" : "-r", nbytes, cput, outfmt (((double) nbytes) / cput)); } fprintf (stdout, "ttcp%s: %d I/O calls, msec/call = %.2f, calls/sec = %.2f\n", trans ? "-t" : "-r", numCalls, 1024.0 * realt / ((double) numCalls), ((double) numCalls) / realt); fprintf (stdout, "ttcp%s: %s\n", trans ? "-t" : "-r", stats); if (verbose) { fprintf (stdout, "ttcp%s: buffer address %#x\n", trans ? "-t" : "-r", message_buf); } exit (0); usage: fprintf (stderr, Usage); return 1; }
void TestDriver::run_i() { ACE_Message_Block buffer (1000000); ACE_SOCK_Acceptor acceptor; if (acceptor.open(sub_addr_) == -1) { ACE_ERROR((LM_ERROR, "%p\n", "open")); throw TestException(); } ACE_SOCK_Stream peer; if (acceptor.accept(peer) == -1) { ACE_ERROR((LM_ERROR, "%p\n", "accept")); throw TestException(); } #if defined (ACE_DEFAULT_MAX_SOCKET_BUFSIZ) # if !defined (ACE_LACKS_SOCKET_BUFSIZ) // set connection options int snd_size = ACE_DEFAULT_MAX_SOCKET_BUFSIZ; int rcv_size = ACE_DEFAULT_MAX_SOCKET_BUFSIZ; int nodelay =1; if (peer.set_option (IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof (nodelay)) == -1) { ACE_ERROR((LM_ERROR, "(%P|%t) Subscriber failed to set TCP_NODELAY\n")); } if (peer.set_option (SOL_SOCKET, SO_SNDBUF, (void *) &snd_size, sizeof (snd_size)) == -1 && errno != ENOTSUP) { ACE_ERROR((LM_ERROR, "(%P|%t) TcpSubscriber failed to set the send buffer size to %d errno %m\n", snd_size)); } if (peer.set_option (SOL_SOCKET, SO_RCVBUF, (void *) &rcv_size, sizeof (int)) == -1 && errno != ENOTSUP) { ACE_ERROR((LM_ERROR, "(%P|%t) TcpSubscriber failed to set the receive buffer size to %d errno %m \n", rcv_size)); } # endif /* !ACE_LACKS_SOCKET_BUFSIZ */ #endif /* !ACE_DEFAULT_MAX_SOCKET_BUFSIZ */ ACE_DEBUG((LM_DEBUG, "(%T) Subscriber running.\n")); unsigned total_packets = num_packets_ + 500; int result; for (unsigned pkt_cnt = 0; pkt_cnt < total_packets; ++pkt_cnt) { if ((result = peer.recv(buffer.wr_ptr(), num_bytes_per_packet_)) == 0) { // The publisher has disconnected - check if this was unexpected. ACE_ERROR((LM_ERROR, "(%P|%t) Publisher disconnected at packet %d.\n", pkt_cnt)); throw TestException(); } else if (result < 0) { // Something bad happened ACE_ERROR((LM_ERROR, "(%P|%t) bad read\n")); throw TestException(); } else if ((unsigned) result != num_bytes_per_packet_) { // Something bad happened ACE_ERROR((LM_ERROR, "(%P|%t) read %d bytes but expected %d\n", result, num_bytes_per_packet_)); throw TestException(); } // only send 4 back result = 4; peer.send_n(buffer.wr_ptr(), result); } // Close the acceptor so that no more clients will be taken in. acceptor.close(); if (verbose_) { ACE_DEBUG((LM_DEBUG, "(%P|%t) Subscriber has completed.\n")); } }