Пример #1
0
void creat_client()
{	
	int fd;
	struct sockaddr_in cliddr,servddr;
	char *ip = "127.0.0.1";
	char buf[1000] = {0};
	
	cliddr.sin_family = AF_INET;
	Inet_pton(AF_INET,ip,&cliddr.sin_addr);
	//随机端口
	cliddr.sin_port = htons(0);
	
	fd = Socket(AF_INET,SOCK_STREAM,0);
	//Bind(fd,(struct sockaddr *)&cliddr,sizeof(cliddr));

	servddr.sin_family = AF_INET;
	Inet_pton(AF_INET,g_data.server.ip,&servddr.sin_addr);
	servddr.sin_port = htons(g_data.server.port);
	
	Connect(fd,(struct sockaddr *)&servddr,sizeof(servddr));
	
	while(fgets(buf,DATA_MAX_LEN,stdin) !=  NULL)
	{
		buf[strlen(buf) -1] = 0;//不带回车
		Send(fd,buf,strlen(buf),0);
		int n =Recv(fd,buf,sizeof(buf),0);
		buf[n] = 0;
		printf("from server:%s\n",buf);
	}
	
	Close(fd);
	
}
Пример #2
0
void *thread_send_query(void *arg)
{
    int fd;
	struct sockaddr_in cliddr,servddr;
	char *ip = "127.0.0.1";
	char buf[1000] = {0};
	
	cliddr.sin_family = AF_INET;
	Inet_pton(AF_INET,ip,&cliddr.sin_addr);
	//随机端口
	cliddr.sin_port = htons(0);
	
	fd = Socket(AF_INET,SOCK_STREAM,0);
	//Bind(fd,(struct sockaddr *)&cliddr,sizeof(cliddr));

	servddr.sin_family = AF_INET;
	Inet_pton(AF_INET,g_data.server.ip,&servddr.sin_addr);
	servddr.sin_port = htons(g_data.server.port);
	
	Connect(fd,(struct sockaddr *)&servddr,sizeof(servddr));
	
    for (int i = 0; i < g_data.per_thread_loop_num; ++i)
    {   
        snprintf(buf, sizeof(buf), "%s", "hello, huangben!");
	    Send(fd,buf,strlen(buf),0);
		int n =Recv(fd,buf,sizeof(buf),0);
		buf[n] = 0;
		printf("from server:%s\n",buf);
    }
	
	Close(fd);
    return NULL;
}
Пример #3
0
int find_peer(int sockfd)
{
    struct sockaddr_in servaddr;
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(CHAT_PORT);

    /* Remove the last octet from the address */
    char *dot = strrchr(subnet_address, '.');
    dot[1] = 0;

    int i = start_idx;
    char try_address[strlen(subnet_address) + 4];
    do {
        snprintf(try_address, sizeof(try_address), "%s%d", subnet_address, i);
        printf("Trying %s\n", try_address);
        ++i;

        Inet_pton(AF_INET, try_address, &servaddr.sin_addr);
    } while (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0);

    printf("Bound!\n");

    return -1;
}
Пример #4
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);
}
Пример #5
0
int
main(int argc, char **argv)
{
	int	sockfd;
	struct 	sockaddr_in servaddr;
	int 	pdes[2];
	char    recvline[MAXLINE + 1];
	int    	n;	

	if (argc != 3)
                err_quit("usage: echocli01 <IPaddress> <write PIPE descriptor> \n\r");

        pdes[1] = *argv[2];


        n = write(pdes[1] , "Time Client\n", 12);
	if( n < 0) printf("\n\r Time_Client: Error in writing to pipe");

	sockfd = Socket(AF_INET, SOCK_STREAM, 0);
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(8888);
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	Connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) ; 
	
        write(pdes[1] , "Connection to server established\n  ", 32);
	while ( (n = Read(sockfd, recvline, MAXLINE)) > 0) {
            recvline[n] = 0;         /* null terminate */
            Fputs(recvline, stdout);
	}

	exit(0);
}
Пример #6
0
int
main(int argc, char **argv)
{
	int sock_fd;
	struct sockaddr_in servaddr;
	struct sctp_event_subscribe evnts;

	if(argc != 2)
		err_quit("Missing host argument - use '%s host'\n",
		       argv[0]);
        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);
/* include mod_client04 */
	bzero(&evnts, sizeof(evnts));
	evnts.sctp_data_io_event = 1;
	evnts.sctp_association_event = 1;
	Setsockopt(sock_fd,IPPROTO_SCTP, SCTP_EVENTS,
		   &evnts, sizeof(evnts));

	sctpstr_cli(stdin,sock_fd,(SA *)&servaddr,sizeof(servaddr));
/* end mod_client04 */
	close(sock_fd);
	return(0);
}
Пример #7
0
int main(int argc, char **argv)
{
	char *spfd=argv[2];
	int pfd=atoi(spfd);

	Writen(pfd,"Child is Running.\n",strlen("Child is running.\n"));
	
	printf("Time -- %s\n\n", argv[1]);
	
	int sockfd;
	struct sockaddr_in servaddr;

	sockfd=Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family=AF_INET;
	servaddr.sin_port=htons(TIME_PORT);
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	int connstatus=connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

	if(connstatus==-1)
	{
		Writen(pfd,"Server is Closed!\n",strlen("Server is Closed!\n"));
		return 1;
	}

	str_clib(stdin, sockfd, pfd);

	exit(0);
	
}
Пример #8
0
int
main(int argc, char **argv)
{
    int i, sockfd[5];
    struct sockaddr_in servaddr;

    if(argc != 2)
        err_quit("usage: tcpcli <IPaddress>");

    for(i = 0; i< 5; i++) {

        sockfd[i] = Socket(AF_INET, SOCK_STREAM, 0);

        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(SERV_PORT);
        Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

        Connect(sockfd[i], (SA *)&servaddr, sizeof(servaddr));
    }

    str_cli(stdin, sockfd[0]);

    exit(0);
}
Пример #9
0
int
main(int argc, char **argv)
{
	int					sockfd;
	struct sockaddr_in	servaddr;

	if (argc != 2)
		err_quit("usage: tcpcli <IPaddress>");

	sockfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(13);		/* daytime server */
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	Signal(SIGPIPE, sig_pipe);

	Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

	sleep(2);
	Write(sockfd, "hello", 5);
	sleep(2);
	Write(sockfd, "world", 5);

	exit(0);
}
Пример #10
0
int
main(int argc, char **argv)
{
	int					sockfd;
	struct sockaddr_in	servaddr;

	if (argc != 2)
		err_quit("usage: udpcli <IPaddress>");

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	sockfd = Socket(AF_INET, SOCK_DGRAM, 0);

	bzero(&cliaddr, sizeof(cliaddr));
	cliaddr.sin_family = AF_INET;
	cliaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	cliaddr.sin_port = htons(0); /* force assignment of ephemeral port */
	Bind(sockfd, (SA *) &cliaddr, sizeof(cliaddr));

	dg_cli(stdin, sockfd, (SA *) &servaddr, sizeof(servaddr));

	exit(0);
}
Пример #11
0
int main(int argc,char *argv[]){
	int cli_parent_fd = strtol(argv[2],NULL,10);
	int sockfd,confd;
	int port_num = 5590;
	char feedback[MAXLINE];

	struct sockaddr_in servaddr;

	//Create a TCP socket that will connect to the port of time server.
	sockfd = Socket(AF_INET,SOCK_STREAM,0);
	bzero(&servaddr,sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(port_num);

	Inet_pton(AF_INET,argv[1],&servaddr.sin_addr);

	//Connect to the server using the socket just created.
	confd = connect(sockfd,(SA*)&servaddr,sizeof(servaddr));
	if(confd < 0){
		//If connection with the server fails.
		bzero(feedback,sizeof(feedback));
		strcpy(feedback,"[Client][Time]Server could not be reached\n");
		Write(cli_parent_fd,feedback,strlen(feedback));
		close(cli_parent_fd);
		exit(0);
	}

	time_server(cli_parent_fd,sockfd,port_num);
	close(sockfd);
	close(cli_parent_fd);
	close(confd);
	exit(0);

}
Пример #12
0
int main(int argc, char** argv) {
    
    struct rlimit limit;

    getrlimit (RLIMIT_STACK, &limit);
    printf ("\nStack Limit = %ld and %ld max\n", limit.rlim_cur, limit.rlim_max);
  
	int			sockfd;
	struct sockaddr_in	servaddr;

	if (argc != 2)
		err_quit("usage: tcpcli <IPaddress>");

	sockfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons( SERV_PORT);
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

	str_cli( stdin, sockfd);		/* do it all */

        close( sockfd);

	exit(0);
}
Пример #13
0
int main(int argc, char **argv)
{
    int sockfd, n;
    char recvline[MAXLINE+1];
    struct sockaddr_in servaddr;

    if(argc != 2)
    {
        err_quit("usage: a.out <IPaddress>");
    }

    sockfd = Socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(13); /* daytime server */
    Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

    Connect(sockfd, (SA*)&servaddr, sizeof(servaddr));

    while((n = read(sockfd, recvline, MAXLINE)) > 0)
    {
        recvline[n] = 0;
        if(fputs(recvline, stdout) == EOF)
        {
            err_sys("fputs error");
        }
    }
    if(n < 0)
    {
        err_sys("read error");
    }
    exit(0);
}
Пример #14
0
int
main(int argc, char **argv)
{
	int					sockfd;
	socklen_t			len;
	struct sockaddr_in	cliaddr, servaddr;

	if (argc != 2)
		err_quit("usage: udpcli <IPaddress>");

	sockfd = Socket(AF_INET, SOCK_DGRAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

	len = sizeof(cliaddr);
	Getsockname(sockfd, (SA *) &cliaddr, &len);
	printf("local address %s\n", Sock_ntop((SA *) &cliaddr, len));

	exit(0);
}
Пример #15
0
int main(int argc, char **argv)
{
	int sockfd, n;
	char recvline[MAXLINE+1];
	struct sockaddr_in servaddr;

	if (argc != 2)
		err_quit("Usage: cli <Ipaddress>");

	sockfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

//	if (connect_nob(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
//		err_sys("connect error");

    connect_nob(sockfd, (SA *)&servaddr, sizeof(servaddr), 10);

	while ((n = read(sockfd, recvline, MAXLINE)) > 0)
	{
		recvline[n] = 0;
		if (fputs(recvline, stdout) == EOF)
			err_sys("fputs error");
	}
	if (n < 0)
		err_sys("read error");
	exit(0);
}
Пример #16
0
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);
}
Пример #17
0
int main(int argc,char *argv[])
{
	int listenfd,optval;
	struct sockaddr_in servaddr;

	if(argc != 2)
		err_quit("usage:tcpsrvipv4 <IPAddress>");

	listenfd = Socket(AF_INET,SOCK_STREAM,0);

	bzero(&servaddr,sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(9999);

	Inet_pton(AF_INET,argv[1],&servaddr.sin_addr);

	optval = 1;
	Setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval));

	Bind(listenfd,(SA *)&servaddr,sizeof(servaddr));

	Listen(listenfd,LISTENQ);

	pause();
}
Пример #18
0
int sock_init(char *ip, int port, struct sockaddr_in *addr)
{
    int sockfd = -1;
    int optval = 1;

    if ( port < 1024)
        return NG;
    bzero(addr, sizeof(struct sockaddr));

    sockfd = x_socket(AF_INET, SOCK_DGRAM, 0);
    addr->sin_family = AF_INET;
    addr->sin_port = htons(port);
    
    if (ip == NULL) {
        addr->sin_addr.s_addr = htonl(INADDR_ANY);
        x_bind(sockfd, (SA *)addr, sizeof(struct sockaddr));
        printf("bind OK\n");
    }
    else  {
        Inet_pton(AF_INET, ip, &(addr->sin_addr));
        x_connect(sockfd, (SA *)addr, sizeof(struct sockaddr));
    }

    x_setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int));


    return sockfd;
}
Пример #19
0
int
main(int argc, char **argv)
{
	int					sockfd;
	void				sig_alrm(int);
	struct itimerval	val;
	struct sockaddr_in	servaddr;

	if (argc != 2)
		err_quit("usage: tcpcli <IPaddress>");

	sockfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

		/* Set interval timer to go off before 3WHS completes */
	Signal(SIGALRM, sig_alrm);
	val.it_interval.tv_sec  = 0;
	val.it_interval.tv_usec = 0;
	val.it_value.tv_sec  = 0;
	val.it_value.tv_usec = 50000;	/* 50 ms */
	if (setitimer(ITIMER_REAL, &val, NULL) == -1)
		err_sys("setitimer error");

	Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

	str_cli(stdin, sockfd);		/* do it all */

	exit(0);
}
Пример #20
0
int
main(int argc, char **argv)
{
	int					sockfd;
	struct linger		ling;
	struct sockaddr_in	servaddr;

	if (argc != 2)
		err_quit("usage: tcpcli <IPaddress>");

	sockfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

	str_cli(stdin, sockfd);		/* do it all */

	ling.l_onoff = 1;
	ling.l_linger = 0;
	Setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));

	exit(0);
}
Пример #21
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);
}
Пример #22
0
void SetCliServAddr(sockaddr_in *servaddr, const char *serverip){
	if(servaddr == NULL)
		DieWithSystemMessage("servaddr is error");
	bzero(servaddr, sizeof(*servaddr));
	servaddr->sin_family = AF_INET;
	servaddr->sin_port = htons(SERV_PORT);
	Inet_pton(AF_INET, serverip, &(servaddr->sin_addr));
}
Пример #23
0
static void update_try_address(char *try_address, size_t try_address_len,
                               char *subnet_address, int host, 
                               struct sockaddr_in *servaddr)
{
    snprintf(try_address, try_address_len, "%s%d", subnet_address, host);

    Inet_pton(AF_INET, try_address, &servaddr->sin_addr);
}
int
main(int argc, char **argv)
{
	int		sockfd;
	struct sockaddr_in servaddr, cliaddr;
    int val;

	if (argc != 4)
		err_quit("usage: %s <LocalAddress> <IPaddress> <Port>", *argv);

	sockfd = Socket(AF_INET, SOCK_STREAM, 0);

    val = 1;
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
        err_sys("setsockopt SO_REUSEADDR error");

	bzero(&servaddr, sizeof servaddr);
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(atoi(argv[3]));
	Inet_pton(AF_INET, argv[2], &servaddr.sin_addr);

    /*
     * Bind local:1500
     */
    bzero(&cliaddr, sizeof cliaddr);
    cliaddr.sin_family = AF_INET;
    cliaddr.sin_port = htons(1500);
    if (!inet_aton(argv[1], &cliaddr.sin_addr))
        err_sys("inet_aton error");

    for (;;) {
        val = bind(sockfd, (SA *) &cliaddr, sizeof(cliaddr));
        if (val == 0)
            break;
        if (errno == EADDRINUSE)
            continue;
        err_sys("bind error");
    }

    /* Block here to test the race condition */
    printf("press any key to continue\n");
    fgetc(stdin);
    if (ferror(stdin))
        err_sys("fgetc error");
    printf("continue now\n");

retry:
	val = connect(sockfd, (SA *) & servaddr, sizeof servaddr);
    if (val < 0) {
        if (errno == EADDRINUSE)
            goto retry;
        err_sys("connect error");
    }

	str_cli(stdin, sockfd);	/* do it all */

	exit(0);
}
Пример #25
0
int
main(int argc, char **argv)
{
    int					sockfd;
    char				*buf;
    pid_t				pid;
    ssize_t				n;
    struct rt_msghdr	*rtm;
    struct sockaddr		*sa, *rti_info[RTAX_MAX];
    struct sockaddr_in	*sin;

    if (argc != 2)
        err_quit("usage: getrt <IPaddress>");

    sockfd = Socket(AF_ROUTE, SOCK_RAW, 0);	/* need superuser privileges */

    buf = Calloc(1, BUFLEN);	/* and initialized to 0 */

    rtm = (struct rt_msghdr *) buf;
    rtm->rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
    rtm->rtm_version = RTM_VERSION;
    rtm->rtm_type = RTM_GET;
    rtm->rtm_addrs = RTA_DST;
    rtm->rtm_pid = pid = getpid();
    rtm->rtm_seq = SEQ;

    sin = (struct sockaddr_in *) (rtm + 1);
    sin->sin_len = sizeof(struct sockaddr_in);
    sin->sin_family = AF_INET;
    Inet_pton(AF_INET, argv[1], &sin->sin_addr);

    Write(sockfd, rtm, rtm->rtm_msglen);

    do {
        n = Read(sockfd, rtm, BUFLEN);
    } while (rtm->rtm_type != RTM_GET || rtm->rtm_seq != SEQ ||
             rtm->rtm_pid != pid);
    /* end getrt1 */

    /* include getrt2 */
    rtm = (struct rt_msghdr *) buf;
    sa = (struct sockaddr *) (rtm + 1);
    get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    if ( (sa = rti_info[RTAX_DST]) != NULL)
        printf("dest: %s\n", Sock_ntop_host(sa, sa->sa_len));

    if ( (sa = rti_info[RTAX_GATEWAY]) != NULL)
        printf("gateway: %s\n", Sock_ntop_host(sa, sa->sa_len));

    if ( (sa = rti_info[RTAX_NETMASK]) != NULL)
        printf("netmask: %s\n", Sock_masktop(sa, sa->sa_len));

    if ( (sa = rti_info[RTAX_GENMASK]) != NULL)
        printf("genmask: %s\n", Sock_masktop(sa, sa->sa_len));

    exit(0);
}
Пример #26
0
in_addr_t
InAddr_of_string ( const char *s )
{
	struct in_addr x;

	ASSERT ( s != NULL );

	Inet_pton ( AF_INET, s, &x );

	return x.s_addr;
}
Пример #27
0
int
main(int argc, char **argv)
{
	int					sockfd, sockfd2;
	struct sockaddr_in	servaddr;

	if (argc != 2)
		err_quit("usage: tcpcli <IPaddress>");

	// 1. inquiry the server if the file is duplicated by fingerprint (a RTT)

	sockfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT+1); //
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

	inquiry_by_fp(sockfd, "not_used");		/* do it all */

	close(sockfd); // yes close sockfd

	// 2. transfer not duplicated chunks to server 
	sockfd2 = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT); //
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	Connect(sockfd2, (SA *) &servaddr, sizeof(servaddr));

	transfer_file(sockfd2, 0);

	close(sockfd2);

	exit(0);
}
Пример #28
0
int main(int argc, char** argv)
{
    int sockfd, n;
    struct sockaddr_in servaddr;
    struct itimerval val;
    fd_set rset, wset;

    if (argc != 2) err_quit("usage: a.out <IPaddress>");

    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); /* echo server */
    Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

    /* Set interval timer to go off before 3WHS completes */
    Signal(SIGALRM, sig_alrm);
    val.it_interval.tv_sec = 0;
    val.it_interval.tv_usec = 0;
    val.it_value.tv_sec = 0;
    val.it_value.tv_usec = 50000; /* 50 ms */
    if (setitimer(ITIMER_REAL, &val, NULL) == -1) err_sys("setitimer error");

again:
    if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) < 0) {
        if (errno == EINTR) {
#ifdef notdef
            goto again; /* second call to connect() -> EADDRINUSE */
#endif
#ifdef notdef
            printf("interrupted system call\n");
            exit(0);
#endif
        } else
            err_sys("connect error");
    }

    FD_ZERO(&rset);
    FD_SET(sockfd, &rset);
    wset = rset;
    sleep(4);
    n = Select(sockfd + 1, &rset, &wset, NULL, NULL);
    printf("select returned %d\n", n);
    if (FD_ISSET(sockfd, &rset)) printf("socket is readable\n");
    if (FD_ISSET(sockfd, &wset)) printf("socket is writable\n");

    str_cli(stdin, sockfd); /* do it all */

    exit(0);
}
Пример #29
0
int main(int argc, char **argv){
	int res = 0;
	struct timeval tv1, tv2;
	double total;
	char *directory = NULL;
	long count;

	if (argc != 3)
		err_quit("usage: client <IPaddress> <file dir>");
	serverip = argv[1];
	directory = argv[2];

	gettimeofday(&tv1, NULL);

	sockfd4pox = get_udp_socket(QUICK_PORT);// channel with POX

	bzero(&servaddrfp, sizeof(servaddrfp));
	servaddrfp.sin_family = AF_INET;
	servaddrfp.sin_port = htons(SERV_PORT+1);
	Inet_pton(AF_INET, argv[1], &servaddrfp.sin_addr);
	sockfd2serverfp = Socket(AF_INET, SOCK_DGRAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
	sockfd2server = Socket(AF_INET, SOCK_DGRAM, 0);

	uploadDir(directory);

	gettimeofday(&tv2, NULL);
	printf("Time cost = %lfms\n",(tv2.tv_sec-tv1.tv_sec)*1000.0+(tv2.tv_usec-tv1.tv_usec)/1000.0);

	close(sockfd4pox);
	close(sockfd2server);
	close(sockfd2serverfp);
	exit(0);
}
Пример #30
0
int
UdpSockByAddr(char *ipaddr, int port /* host byte order */ )
{
  int         sockfd;

  sockfd = Socket(AF_INET, SOCK_DGRAM, 0);

  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(port);
  Inet_pton(AF_INET, ipaddr, &servaddr.sin_addr);

  return(sockfd);
}