板凳——————————————————c++(97)

#include
#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>

int main(int argc, const char* argv[]){
struct in_addr ia;
inet_aton(“172.16.2.6”, &ia);
printf(“ia.s_addr = 0x%x\n”, ia.s_addr);
printf(“real_ip = %s\n”, inet_ntoa(ia) );
Linux C与C++ 一线开发p501
int sfp, nfp;
struct sockaddr_in s_add, c_add;
socklen_t sin_size;
unsigned short portnum = 10051;
struct sockaddr_in serv;
socklen_t serv_len = sizeof(serv);

sfp = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == sfp){
     printf("socket fail! \r\n");
     return -1;
}
printf("socket ok !\r\n");
printf("ip = %s, port = %d\r\n", inet_ntoa(serv.sin_addr), ntohs(serv.sin_port));

int on = 1;
setsockopt(sfp, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
bzero(&s_add, sizeof(struct sockaddr_in));
s_add.sin_family = AF_INET;
s_add.sin_addr.s_addr = inet_addr("192.168.0.106");//("192.168.0.2")
s_add.sin_port = htons(portnum);
if(-1 == bind(sfp, (struct sockaddr *)(&s_add), sizeof(struct sockaddr))){
     printf("bind fail: %d!\r\n", errno);
     return -1;
}
printf("bind ok !\r\n");
getsockname(sfp, (struct sockaddr *)&serv, &serv_len );
printf("ip = %s, port = %d\r\n", inet_ntoa(serv.sin_addr), ntohs(serv.sin_port));


char hostname[30];
int flag = 0;
memset(hostname, 0x00, sizeof(hostname));
flag = gethostname(hostname, sizeof(hostname));
if(flag < 0){
    perror("gethostname error");
    return -1;
}
printf("hostname = %s\n", hostname);

Linux C与C++ 一线开发p504
struct hostent h;
if(argc != 2){
fprintf(stderr, “usage: getip address\n”);
return -1;
}
if((h = gethostbyname(argv[1])) == NULL){
herror(“gethostbyname”);
return -1;
}
printf(“Host name : %s\n”, h->h_name);
printf(“IP Address : %s\n”, inet_ntoa(
((struct in_addr *) h->h_addr)));

printf("Hello, I am a server, Welcome to connect me !\r\n");
s_add.sin_addr.s_addr = htonl(INADDR_ANY); 
if(-1 == listen(sfp, 5)){
   printf("listen fail !\r\n");
   return -1;
}
printf("listen ok\r\n");

while(1){
   sin_size = sizeof(struct sockaddr_in);
   nfp = accept(sfp, (struct sockaddr *)(&c_add), &sin_size);
   if(-1 == nfp){
       printf("accept fail !\r\n");
       return -1;
     }
     printf("accept ok!\r\nServer start get connect from ip = %s, port = %d\r\n", 
            inet_ntoa(c_add.sin_addr), ntohs(c_add.sin_port));
     if(-1 == write(nfp, "hello, client, you are welcome!\r\n", 32)){
        printf("write fail!\r\n");
        return -1;
     }
     printf("write ok!\r\n");
     close(nfp);
     puts("continue to listen(y/n)?");
     char ch[2];
     scanf("%s", ch, 2);
     if(ch [0] != 'y')
         break;
}
printf("bye!\n");
close(sfp);

return 0;

}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c13 c13.cpp
wannian07@wannian07-PC:~$ ./c13
ia.s_addr = 0x60210ac
real_ip = 172.16.2.6

Linux 查看本地ip

1.ifconfig -a

2.ip addr

socket ok !
ip = 0.0.0.0, port = 256
bind fail: 99!
socket ok !
ip = 0.0.0.0, port = 256
bind ok !
ip = 192.168.0.106, port = 10051

hostname = wannian07-PC

usage: getip address

Hello, I am a server, Welcome to connect me !
listen ok

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值