本文主要探讨linux网络相关知识,详细介绍看本博客其他博文。
网络基础(参考本博客其他文章:基础网络知识,socket网络编程,基于socket的聊天室和简易ftp)
路由器是局域网和外部网络通信出入口
DNS实现域名和IP地址之间转换
NAT是局域网主机发送数据包给外网将主机内网IP转换为外网IP
网段 = IP地址 & 子网掩码
demo:
编写client和server,server用于存储个人信息,client下发个人信息增删改查等信息到server,server执行并反馈结果
server
#include
#include
#include
#include
#include
#include
#include
#define SERPORT 8001
#define SERADDR "192.168.200.181"
#define BACKLOG 100
#define CMD_REGISTER 1
#define CMD_CHECK 2
#define CMD_GETINFO 3
#define CMD_DELETE 4
typedef struct people_info
{
char name[20];
int age;
int cmd;
char result[20];
} info;
typedef struct mark_flag
{
int flg;
int zero;
} mark;
char name[3][10] = {0};
int age[3] = {0};
int cli_fd;
info st;
mark flag;
void have_connect()
{
struct sockaddr_in ser_addr = {0};
struct sockaddr_in cli_addr = {0};
socklen_t len = sizeof(cli_addr);
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1)
{
perror("socket");
exit(-1);
}
ser_addr.sin_family = AF_INET;
ser_addr.sin_port = htons(SERPORT);
ser_addr.sin_addr.s_addr = inet_addr(SERADDR);
bind(sockfd, (const struct sockaddr *)&ser_addr, sizeof(ser_addr));
listen(sockfd, BACKLOG);
cli_fd = accept(sockfd, (struct sockaddr *)&cli_addr,&len);
send(cli_fd,"connect sucess",strlen("connect sucess"),0);
printf("%s connect ...\n",inet_ntoa(cli_addr.sin_addr));
}
void find_info()
{
int i;
for(i = 0;i < (sizeof(age)/sizeof(age[0]));i++)
{
if(!strncmp(name[i],st.name,strlen(st.name)))
flag.flg = i;
if(age[i] == 0)
flag.zero = i;
}
}
void register_info()
{
find_info();
if(flag.flg != -1)
{
strcpy(st.result,"user existed");
}
if(flag.zero == -1 && flag.flg == -1)
{
strcpy(st.result,"user full");
}
if(flag.zero != -1 && flag.flg == -1)
{
strcpy(name[flag.zero],st.name);
age[flag.zero] = st.age;
strcpy(st.result,"register ok");
}
}
void get_info()
{
find_info();
if(flag.flg == -1)
{
strcpy(st.result,"have no user");
}
else
{
st.age = age[flag.flg];
strcpy(st.name,name[flag.flg]);
}
}
void check_info()
{
find_info();
if(flag.flg == -1)
{
strcpy(st.result,"have no user");
}
else
{
strcpy(st.result,"user existed");
}
}
void delete_info()
{
find_info();
if(flag.flg == -1)
{
strcpy(st.result,"have no user");
}
else
{
age[flag.flg] = 0;
memset(name[flag.flg],0,sizeof(name[flag.flg]));
strcpy(st.result,"user deleted");
}
}
int main()
{
have_connect();
while(1)
{
memset(&st,0,sizeof(st));
recv(cli_fd, &st, sizeof(info), 0);
memset(st.result,0,sizeof(st.result));
flag.flg = -1;
flag.zero = -1;
if(st.cmd == CMD_REGISTER)
{
register_info();
}
if(st.cmd == CMD_GETINFO)
{
get_info();
}
if(st.cmd == CMD_CHECK)
{
check_info();
}
if(st.cmd == CMD_DELETE)
{
delete_info();
}
send(cli_fd,&st,sizeof(st),0);
}
return 0;
}
client
#include
#include
#include
#include
#include
#include
#include
#define SERPORT 8001
#define SERADDR "192.168.200.181"
#define CMD_REGISTER 1
#define CMD_CHECK 2
#define CMD_GETINFO 3
#define CMD_DELETE 4
typedef struct people_info
{
char name[20];
int age;
int cmd;
char result[20];
} info;
int sockfd;
info st;
void have_connect()
{
struct sockaddr_in ser_addr = {0};
char buf[20] = {0};
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1)
{
perror("socket");
exit(-1);
}
ser_addr.sin_family = AF_INET;
ser_addr.sin_port = htons(SERPORT);
ser_addr.sin_addr.s_addr = inet_addr(SERADDR);
bind(sockfd, (const struct sockaddr *)&ser_addr, sizeof(ser_addr));
connect(sockfd, (const struct sockaddr *)&ser_addr, sizeof(ser_addr));
recv(sockfd,buf, sizeof(buf), 0);
printf("%s\n",buf);
}
int main()
{
int flag;
have_connect();
while(1)
{
memset(&st,0,sizeof(st));
printf("ex:1 : register 2 : check 3 : get 4 : delete,please input:");
scanf("%d",&flag);
if(flag ==1)
{
printf("name:");
scanf("%s",st.name);
printf("age:");
scanf("%d",&st.age);
st.cmd = CMD_REGISTER;
}
else
{
printf("name:");
scanf("%s",st.name);
if(flag ==2)
st.cmd = CMD_CHECK;
if(flag ==3)
st.cmd = CMD_GETINFO;
if(flag ==4)
st.cmd = CMD_DELETE;
if(flag == 0 || flag > 4)
printf("input error\n");
}
send(sockfd,&st,sizeof(st),0);
recv(sockfd,&st, sizeof(st), 0);
if(strlen(st.result))
{
printf("%s\n",st.result);
}
else
{
printf("name : %s, age : %d\n",st.name,st.age);
}
}
}
结果显示: