本文整理汇总了C++中Inet_pton函数的典型用法代码示例。如果您正苦于以下问题:C++ Inet_pton函数的具体用法?C++ Inet_pton怎么用?C++ Inet_pton使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Inet_pton函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: creat_client
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: Inet_pton
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: find_peer
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: main
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: main
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: main
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: main
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: main
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: main
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: main
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: main
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: main
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: main
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: main
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: main
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);
}