void start_test(int role) { int sk; int i = 0; DEBUG_PRINT(DEBUG_NONE, "\nStarting tests...\n"); repeat_count = repeat; if (MIXED == role) { repeat_count = repeat_count * msg_cnt; /* Repeat per assoc. */ mixed_mode_test(); return; } sk = socket_r(); bind_r(sk, &s_loc); /* Do we need to do bindx() to add any additional addresses? */ if (bindx_add_addrs) bindx_r(sk, bindx_add_addrs, bindx_add_count, SCTP_BINDX_ADD_ADDR); if (role == SERVER) { listen_r(sk, 100); } else { if (socket_type == SOCK_STREAM && connectx_count == 0) connect_r(sk, (struct sockaddr *)&s_rem, r_len); if (connectx_count != 0) connectx_r(sk, connectx_addrs, connectx_count); } if (!debug_level) { printf(" "); } for(i = 0; i < repeat_count; i++) { if (role == SERVER) { DEBUG_PRINT(DEBUG_NONE, "Server: Receiving packets.\n"); server(sk); } else { DEBUG_PRINT(DEBUG_NONE, "Client: Sending packets.(%d/%d)\n", i+1, repeat_count); client(sk); } fflush(stdout); } close_r(sk); } /* start_test() */
void start_test(int role) { int sk, pid; int i = 0; DEBUG_PRINT(DEBUG_NONE, "\nStarting tests...\n"); repeat_count = repeat; DEBUG_PRINT(DEBUG_MIN, "\tsocket(SOCK_STREAM, IPPROTO_SCTP)"); if ((sk = socket(s_loc.ss_family, SOCK_STREAM, IPPROTO_SCTP)) < 0 ) { fprintf(stderr, "\n\n\t\t*** socket: failed to create" " socket: %s ***\n", strerror(errno)); exit(1); } DEBUG_PRINT(DEBUG_MIN, " -> sk=%d\n", sk); bind_r(sk, &s_loc); if (role == SERVER) { listen_r(sk, 1); accept_r(sk); } else { connect_r(sk, (struct sockaddr *)&s_rem, r_len); } if ((pid = fork()) == 0) { settimerhandle(); printstatus(gsk); while(1); } else { if (!debug_level) { printf(" "); } for(i = 0; i < repeat_count; i++) { if (role == SERVER) { DEBUG_PRINT(DEBUG_NONE, "Server: Receiving packets.(%d/%d)\n", i+1, repeat_count); server(gsk); } else { DEBUG_PRINT(DEBUG_NONE, "Client: Sending packets.(%d/%d)\n", i+1, repeat_count); client(sk); } fflush(stdout); } if (role == SERVER) close_r(gsk); close_r(sk); } } /* start_test() */
int main(int argc, const char *argv[]) { struct sockaddr_in server_addr; char buf[MAXLINE]; int n, sock_fd; if(argc != 2) err_sys("Usage ./server server_addr msg"); //int socket_r(int domain, int type, int protocol); sock_fd = socket_r(AF_INET, SOCK_STREAM, 0); memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; //int inet_pton(int af, const char *src, void *dst); inet_pton(AF_INET, argv[1], &server_addr.sin_addr); server_addr.sin_port = htons(SERVER_PORT); //void connect_r(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen, void *arg); connect_r(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr), (void *)argv[1]); //ssize_t write_r(int fd, const void *ptr, size_t nbytes); fprintf(stdout, "\n"); while(fgets(buf, MAXLINE, stdin) != NULL){ if(write_r(sock_fd, buf, strlen(buf)) == -1) err_ret("write_r error"); //supplemention //ssize_t read_r(int fd, void *ptr, size_t nbytes); n = read(sock_fd, buf, MAXLINE); if(n == 0) fprintf(stdout, "peer side(%s port %d) has been closed.\n", argv[1], ntohs(server_addr.sin_port)); else { fprintf(stdout, "responsed from server:\n"); write_r(STDOUT_FILENO, buf, n); //fprintf(stdout, "\n"); } } //void close_r(int fd); close_r(sock_fd); return 0; }