Example #1
0
int
main(int argc, char **argv)
{
	int sock_fd;
	struct sockaddr_in servaddr;
	int echo_to_all=0;

	if(argc < 2)
		err_quit("Missing host argument - use '%s host [echo]'\n",
		       argv[0]);
	if(argc > 2) {
		printf("Echoing messages to all streams\n");
		echo_to_all = 1;
	}
        sock_fd = Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	// 下面這一個是多餘的,沒有效果
	// servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(SERV_PORT);
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	SctpTurnOnAllEvent(sock_fd);

	if(echo_to_all == 0)
		sctpstr_cli(stdin,sock_fd,(SA *)&servaddr,sizeof(servaddr));
	else
		sctpstr_cli_echoall(stdin,sock_fd,(SA *)&servaddr,sizeof(servaddr));
	Close(sock_fd);
	return(0);
}
Example #2
0
int
main(int argc, char **argv)
{
  int sock_fd;
  struct sockaddr_in servaddr;
  struct sctp_event_subscribe evnts;
  int echo_to_all=0;

  if(argc < 2)
    err_quit("Missing host argument - use '%s host [echo]'\n",
           argv[0]);
  if(argc > 2) {
    printf("Echoing messages to all streams\n");
    echo_to_all = 1;
  }
        sock_fd = Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(SERV_PORT);
  Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

  bzero(&evnts, sizeof(evnts));
  evnts.sctp_data_io_event = 1;
  Setsockopt(sock_fd,IPPROTO_SCTP, SCTP_EVENTS,
       &evnts, sizeof(evnts));
  if(echo_to_all == 0)
    sctpstr_cli(stdin,sock_fd,(SA *)&servaddr,sizeof(servaddr));
  else
    sctpstr_cli_echoall(stdin,sock_fd,(SA *)&servaddr,sizeof(servaddr));
  Close(sock_fd);
  return(0);
}
Example #3
0
//==============================main function==================================
int main(int argc,char **argv)
{
    int sock_fd;	// client所建立的socket
    struct sockaddr_in servaddr;	// 記錄server的sockaddr_in的相關資訊
    struct sctp_event_subscribe evnts;	// 設定要觀查的事件
    int echo_to_all = 0;		// 決定server是否有多個
    int ret_value = 0;

    // 沒有設定server的ip
    if (argc < 2) {
        printf("Missing host argument - use '%s host [echo]'\n",argv[0]);
        exit(-1);
    }
    // 設定多個server的ip
    if (argc > 2) {
        printf("Echoing messages to all streams\n");
        echo_to_all = 1;
    }

    // init setting
    sock_fd = socket(AF_INET,SOCK_SEQPACKET,IPPROTO_SCTP);	// 設定為sctp為one-to-many的型態
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    // servaddr.sin_addr.s_addr = htonl(INADDR_ANY);	// 感覺這一行應該是多餘的
    servaddr.sin_port = htons(SERV_PORT);
    ret_value = inet_pton(AF_INET,argv[1],&servaddr.sin_addr);	// 把字串轉換成網路位元組序二進位值,當回傳為1時,表轉換成功
    if (ret_value != 1) {
        printf("translate server ip address error!!\n");
        exit(-1);
    }

    // 設定要觀察的事件,所有都清除,只設定io event
    memset(&evnts,0,sizeof(evnts));
    evnts.sctp_data_io_event = 1;	// 監控sctp的data的io事件:1是打開的意思
    ret_value = setsockopt(sock_fd,IPPROTO_SCTP,SCTP_EVENTS,&evnts,sizeof(evnts));
    if (ret_value == -1) {
        printf("setsockopt error\n");
        exit(-1);
    }

    ret_value = connect(sock_fd,(struct sockaddr *) &servaddr,sizeof(servaddr));
    if (ret_value == -1) {
        printf("connect error\n");
        exit(-1);
    }

    if (echo_to_all == 0) {
        // 此client只傳給一個server
        sctpstr_cli(stdin,sock_fd,(struct sockaddr *) &servaddr,sizeof(servaddr));
    }
    else {
        // 要設定傳給許多個server
        sctpstr_cli_echoall(stdin,sock_fd,(struct sockaddr *) NULL,0);
    }
    close(sock_fd);
    return 0;
}