int main(int argc, char **argv) { int sockfd, rcvbuf, mss; socklen_t len; struct sockaddr_in servaddr; if (argc != 2) err_quit("usage: rcvbuf <IPaddrress>"); sockfd = Socket(AF_INET, SOCK_STREAM, 0); len = sizeof(rcvbuf); Getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &len); len = sizeof(mss); Getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, &mss, &len); printf("defaults: SO_RCVBUF = %d, MSS = %d\n", rcvbuf, mss); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(13); Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); Connect(sockfd, (SA *)&servaddr, sizeof(servaddr)); len = sizeof(rcvbuf); Getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &len); len = sizeof(mss); Getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, &mss, &len); printf("alter connect: SO_RCVBUF = %d, MSS = %d\n", rcvbuf, mss); exit(0); }
/* show snd/rcv buffer sizes for sockfd */ static void show_buff_szs(int sockfd, const char *type) { int sz; socklen_t len; sz = 0; len = (socklen_t)sizeof(int); Getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &sz, &len); printf("%s socket receive buffer: %d\n", type, sz); Getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sz, &len); printf("%s socket send buffer: %d\n", type, sz); }
void *get_info(void *arg) { int sockfd; int len,i=0,last_in = 0,last_out = 0;; struct tcp_info_user info; FILE *stream = fopen("rst_server","w+"); //接收线程detach自己 pthread_detach(pthread_self()); sockfd = *( (int*)arg ); i = 0; while(i < 100*200000 && openflag>0) { len = sizeof(info); errno = 0; Getsockopt(sockfd, SOL_TCP, TCP_INFO,(void *)&info, (socklen_t *)&len); if(errno != 0) { //perror("get sock error\n"); break; } if( (last_in != info.tcpi_segs_in) || (last_out != info.tcpi_segs_out) ) { sleep_ms(1); len = sizeof(info); Getsockopt(sockfd, SOL_TCP, TCP_INFO,(void *)&info, (socklen_t *)&len); fprintftcpinfo(stream,&info); //printf("i=%d\n",i); last_in = info.tcpi_segs_in; last_out = info.tcpi_segs_out; i++; continue; } sleep_ms(1); i++; } fclose(stream); return 0; }
void dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen) { int n; socklen_t len; char mesg[MAXLINE]; Signal(SIGINT, recvfrom_int); len = sizeof(n); Getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &n, &len); if(len == sizeof(n)) printf("initial SO_RCVBUF size: %d\n", n); else printf("Error getting SO_RCVBUF size.\n"); n = 220 * 1024; /* Set the socket recvbuf size. By default*/ /* it's 87380. */ Setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)); printf("Set new SO_RCVBUF: %d\n", n); for ( ; ; ) { len = clilen; Recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len); count++; } }
void *recv_function(void *arg) { int sockfd,len,i=0; struct tcp_info_user info; char buffer[MAX_PKT_SIZE] = {"welcome to linux hello\0"}; //MAX_PKT_SIZE //接收线程detach自己 pthread_detach(pthread_self()); sockfd = *( (int*)arg ); //sleep_ms(15*1000); len = sizeof(info); Getsockopt(sockfd, SOL_TCP, TCP_INFO,(void *)&info, (socklen_t *)&len); printf("----------------start read--------------\n"); printftcpinfo(&info); while(i < 200) { Read(sockfd,buffer,MAX_PKT_SIZE); //sleep_ms(2*1000); //len = sizeof(info); //Getsockopt(sockfd, SOL_TCP, TCP_INFO,(void *)&info, (socklen_t *)&len); //printf("\n----------read %d bytes--------------\n",(i+1)*num); //printftcpinfo(&info); i++; } return 0; }
/* use nonblock for connect */ int connect_nob(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen, int nsec) { int flags, n, error; socklen_t len; struct timeval tval; fd_set rset, wset; flags = Fcntl(sockfd, F_GETFL, 0); Fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); error = 0; /* when socket is nonblock connect return EINPROGRESS and three ways hand shake is still running */ if ((n = connect(sockfd, servaddr, addrlen)) < 0) { if (errno != EINPROGRESS) /* ignore the mistake */ return (-1); } /* nonbalck state the socket is connected now */ /* this might happen some times */ if (n == 0) goto done; FD_ZERO(&rset); FD_SET(sockfd, &rset); wset = rset; tval.tv_sec = nsec; tval.tv_usec = 0; if ((n = select(sockfd+1, &rset, &wset, NULL, nsec ? &tval : NULL)) < 0) err_sys("select error"); else if (n == 0) /* select time out */ { Close(sockfd); errno = ETIMEDOUT; err_sys("select timeout"); } if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) { len = sizeof(error); Getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len); } else err_quit("select error: sockfd not set"); done: /* wether n==0 or not bellow will excuted */ Fcntl(sockfd, F_SETFL, flags); /* set back to block */ if (error) /* connect error */ { Close(sockfd); errno = error; return (-1); } return (0); }
int main(int argc, char **argv) { int sockfd, n; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr; int rcv_sz, mss_sz; socklen_t len; if (argc != 2) err_quit("usage: a.out <IPaddress>"); len = sizeof(int); rcv_sz = mss_sz = 0; if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) err_sys("socket error"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(13); /* daytime server */ if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) err_quit("inet_pton error for %s", argv[1]); Getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &rcv_sz, &len); Getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, &mss_sz, &len); printf("RCVBUF: %d MSS: %d\n", rcv_sz, mss_sz); if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0) err_sys("connect error"); Getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &rcv_sz, &len); Getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, &mss_sz, &len); printf("RCVBUF: %d MSS: %d\n", rcv_sz, mss_sz); while ( (n = read(sockfd, recvline, MAXLINE)) > 0) { recvline[n] = 0; /* null terminate */ if (fputs(recvline, stdout) == EOF) err_sys("fputs error"); } if (n < 0) err_sys("read error"); exit(0); }
static int handle_kqueue_error(const struct pollfd_state *state){ socklen_t slen; int err; slen = sizeof(err); if(Getsockopt(state->pfd.fd,SOL_SOCKET,SO_ERROR,&err,&slen)){ return -1; } pmoan(err,"Socket error on %d\n",state->pfd.fd); return 0; }
int main(int argc, char **argv) { int listenfd, connfd; u_char *opts; pid_t childpid; socklen_t clilen, len; struct sockaddr_in cliaddr, servaddr; void sig_chld(int); opts = Malloc(44); listenfd = Socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); Listen(listenfd, LISTENQ); Signal(SIGCHLD, sig_chld); for ( ; ; ) { clilen = sizeof(cliaddr); if ( (connfd = accept(listenfd, (SA *) &cliaddr, &clilen)) < 0) { if (errno == EINTR) continue; /* back to for() */ else err_sys("accept error"); } len = 44; Getsockopt(connfd, IPPROTO_IP, IP_OPTIONS, opts, &len); if (len > 0) { printf("received IP options, len = %d\n", len); inet_srcrt_print(opts, len); } if ( (childpid = Fork()) == 0) /* child process */ { Close(listenfd); /* close listening socket */ str_echo(connfd); /* process the request */ exit(0); } Close(connfd); /* parent closes connected socket */ } }
void *get_info(void *arg) { int sockfd; int len,i=0,last_in = 0,last_out = 0;; struct tcp_info_user info; //接收线程detach自己 pthread_detach(pthread_self()); sockfd = *( (int*)arg ); i = 0; while(i < 100*200000) { len = sizeof(info); Getsockopt(sockfd, SOL_TCP, TCP_INFO,(void *)&info, (socklen_t *)&len); if( (last_in != info.tcpi_segs_in) || (last_out != info.tcpi_segs_out) ) { sleep_ms(1); len = sizeof(info); Getsockopt(sockfd, SOL_TCP, TCP_INFO,(void *)&info, (socklen_t *)&len); printftcpinfo(&info); printf("i=%d\n",i); last_in = info.tcpi_segs_in; last_out = info.tcpi_segs_out; } //sleep_ms(1); i++; } return 0; }
int main(int argc, char **argv) { int sockfd, mss, sendbuff; socklen_t optlen; sockfd = Socket(AF_INET, SOCK_STREAM, 0); /* Fetch and print the TCP maximum segment size. */ optlen = sizeof(mss); Getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, &mss, &optlen); printf("TCP mss = %d\n", mss); /* Set the send buffer size, then fetch it and print its value. */ sendbuff = 65536; Setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff)); optlen = sizeof(sendbuff); Getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen); printf("send buffer size = %d\n", sendbuff); exit(0); }
int sctp_get_no_strms(int sock_fd,struct sockaddr *to, socklen_t tolen) { int retsz; struct sctp_status status; retsz = sizeof(status); bzero(&status,sizeof(status)); status.sstat_assoc_id = sctp_address_to_associd(sock_fd,to,tolen); Getsockopt(sock_fd,IPPROTO_SCTP, SCTP_STATUS, &status, &retsz); return(status.sstat_outstrms); }
int main() { int Listenfd,connfd; int len,val; int last_in = 0,last_out = 0,i = 0; struct tcp_info_user info; socklen_t clilen; struct sockaddr_in cliaddr, servaddr; char writebuf[TRANSSIZE]; snprintf(writebuf,TRANSSIZE,"hello"); Listenfd = Socket(AF_INET,SOCK_STREAM,0); memset(&servaddr,0,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); Bind(Listenfd,(SA*)&servaddr,sizeof(servaddr)); Listen(Listenfd,LISTENQ); for( ; ;){ clilen = sizeof(cliaddr); connfd = Accept(Listenfd,(SA*)&cliaddr,&clilen); sleep(3); val = 1; len = sizeof(int); Setsockopt(connfd, SOL_TCP, TCP_NODELAY,(void *)&val, len); snprintf(writebuf,TRANSSIZE,"world01"); Write(connfd,writebuf,strlen(writebuf)+1); sleep_ms(10); snprintf(writebuf,TRANSSIZE,"world02"); Write(connfd,writebuf,strlen(writebuf)+1); sleep_ms(10); snprintf(writebuf,TRANSSIZE,"world03"); Write(connfd,writebuf,strlen(writebuf)+1); sleep_ms(10); snprintf(writebuf,TRANSSIZE,"world04"); Write(connfd,writebuf,strlen(writebuf)+1); sleep_ms(10); snprintf(writebuf,TRANSSIZE,"world05"); Write(connfd,writebuf,strlen(writebuf)+1); i = 0; while(i < 100*200) { len = sizeof(info); Getsockopt(connfd, SOL_TCP, TCP_INFO,(void *)&info, (socklen_t *)&len); if( (last_in != info.tcpi_segs_in) || (last_out != info.tcpi_segs_out) ) { sleep_ms(2); len = sizeof(info); Getsockopt(connfd, SOL_TCP, TCP_INFO,(void *)&info, (socklen_t *)&len); printftcpinfo(&info); printf("i=%d\n",i); last_in = info.tcpi_segs_in; last_out = info.tcpi_segs_out; } sleep_ms(10); i++; } //sleep(200); len = sizeof(info); Getsockopt(connfd, SOL_TCP, TCP_INFO,(void *)&info, (socklen_t *)&len); printftcpinfo(&info); printf("close\n"); Close(connfd); } return 0; }