示例#1
0
int main(int argc,char **argv)
{
	if(argc!=2)
	{
		printf("usage:%s <IPport>\n",argv[0]);
		return 0;
	}
	
	InetAddress clientAddr(atoi(argv[1]));
	std::unique_ptr<Acceptor> acceptor(new Acceptor(clientAddr));
	while(true)
	{
		TcpStreamPtr stream=acceptor->accept();
		struct SessionMessage sessionMessage={0,0};
		if(stream->receiveAll(&sessionMessage,sizeof sessionMessage)!=sizeof sessionMessage)
		{
			printf("read error\n");
			return 0;
		}
		sessionMessage.number=ntohl(sessionMessage.number);
		sessionMessage.length=ntohl(sessionMessage.length);
		printf("sessionMessage.number=%d sessionMessage.length=%d\n",sessionMessage.number,sessionMessage.length);	
		const int total_length=static_cast<int>(sizeof(int32_t)+sessionMessage.length);
		struct PayLoadMessage *payLoadMessage=static_cast<PayLoadMessage*>(::malloc(total_length));
		for(int i=0;i<sessionMessage.number;++i)
		{
			payLoadMessage->length=0;
			int nr=stream->receiveAll(&payLoadMessage->length,sizeof payLoadMessage->length);
			
			assert(nr==sizeof payLoadMessage->length);
			payLoadMessage->length=ntohl(payLoadMessage->length);
			nr=stream->receiveAll(&payLoadMessage->data,payLoadMessage->length);
			assert(nr==payLoadMessage->length);
		
			int ack=static_cast<int>(sizeof (payLoadMessage->length) + payLoadMessage->length);
			ack=htonl(ack);
			int nw=stream->sendAll(&ack,sizeof ack);
			assert(nw==sizeof ack);		
		}
		free(payLoadMessage);
	}
	
	return 0;
}
示例#2
0
int ACE_TMAIN (int argc, ACE_TCHAR **argv){

  // Initialize the options manager
  Options_Manager optsMgr(argc, argv, ACE_TEXT ("client-opts"));

  // show usage if requested
  if (optsMgr._usage) {
    optsMgr._show_usage(stderr, ACE_TEXT ("client-opts"));
    return 1;
  }

  // If SCTP is not installed then terminate the program, unless TCP
  // was specified.
#ifndef ACE_HAS_SCTP
  if (optsMgr.test_transport_protocol == IPPROTO_SCTP)
    ACE_ERROR_RETURN((LM_ERROR,
                      ACE_TEXT ("SCTP was NOT installed when this binary was compiled.\nSOCK_STREAM_clt may still be run using TCP via the '-t tcp' option.\n")),
                     1);
#endif

  // check that valid options were specified
  if (optsMgr._error) {
    ACE_OS::fprintf (stderr, "ERROR: %s\n", ACE_TEXT_ALWAYS_CHAR (optsMgr._error_message));
    return 1;
  }


  // Create the address that we want the client to connect to
  ACE_INET_Addr serverAddr(Options_Manager::server_port,
                           Options_Manager::server_host);

  // Create the address that we want the client to connect FROM
  ACE_INET_Addr clientAddr(Options_Manager::client_port,
                           Options_Manager::client_connect_addr);

  // object that manages the connection to the server
  ACE_SOCK_Connector connector;

  // object that manages the data xfer between the client and server
  ACE_SOCK_Stream dataStream;

  // connect to the server
  if (connector.connect (dataStream,
                         serverAddr,
                         0,clientAddr, 0, 0, 0, // ALL DEFAULT ARGUMENTS
                         Options_Manager::test_transport_protocol) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("(%P|%t) %p\n"),
                       ACE_TEXT ("connection failed")),
                      1);

  // run the test
  ACE_SCTP::HIST testResultsHistogram = 0;
  // connection is closed by runTest* functions
  testResultsHistogram = runTest(dataStream);

  // report out the test statistics.
  // all histograms created are placed onto a linked list that report()
  // can access. So the histogram created in one of the *_test() functions
  // will be reported out by the report() call
  if (testResultsHistogram)
    ACE_SCTP::report();

  return 0;
}