Linux网络编程

数据存储优先顺序转换

1
2
3
4
5
6
#include<arpa/inet.h>
uint16_t htons(uint16_t hostshort); //16位主机序转网络序
uint32_t htonl(uint32_t hostlong);//32位主机序转网络序
uint16_t ntohs(uint16_t netshort);//16位网络序转主机序
uint32_t ntohl(uint32_t netlong);//32位网络序转主机序
//出错返回-1

#地址格式转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int inet_aton(const char *srraddr,struct in_addr *addrptr);
//将点分十进制数的ip地址转换位网络字节序的32位二进制数,成功返回1,不成功返回0
char *inet_ntoa(struct in_addr inaddr);
//将网络字节序的32位二进制数值转化为点分法的十进制ip地址
in_addr_t inet_addr(const char *straddr);
//功能与inet_aton相同,传递方式不同
const chat* inet_pton(int family,const char *src,char *dst,socklen_t len);
//与inter_ntoa类似,其中len表示转换之后的长度(字符串长度)
int inet_pton(int family,const char *src,void *dst);
//和inet_aton类似,多了family函数,该参数指定为AF_INET, 表示为ipv4协议,如果为AF_INET6表示IPV6协议


# TCP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//服务端:socket--bind--listen--while(1){accept--recv--send--close}--close
//客户端:socket--connect--send--recv--close
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int socketFd=socket(AF_INET,SOCKSTREAM,0);
//AF_INET:ipv4 AF_INET6:ipv6
struct sockaddr_in serAddr;
memset(&serAddr,0,sizeof(serAddr);
serAddr.sin_family=AF_INET;
serAddr.sin_port=htons(atoi(port));
serAddr.sin_addr.s_addr=inet_addr(ip);
int resuce=1;
setsockopt(socketFd,SOL_SOCKET,SO_REUSEADDR,&resue,sizeof(int));
bind(socketFd,(struct sockaddr*)&serAddr,sizeof(struct sockaddr);
listen(socketFd,10);
//listen第二个参数为最大连接数
recv(socketFd,&buf,sizeof(buf),0);
//接收消息至缓冲区
send(int s,const void *msg,int len,unsigned int flags);
//s为accept的返回值,new_fd
//msg一般为字符串常量,len表示长度,flags一般为0
close(int fd);
1
2
3
4
5
6
7
8
//sockaddr_in结构体
struct sockaddr_in
{
usigned short int sin_family;
uint16_t sin_port;
struct in_addr sin_addr;
unsigned char sin_zero[8];//未使用
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//客户端
int connect(int sockfd,struct sockaddr *ser_addr,int addrlen);
//sockfd:socket的返回值
//serv_addr:结构体指针变量,存储着远程服务器的IP与端口号
//addrlen:结构体变量的长度

struct sockaddr_in seraddr;
memset(&seraddr,0,sizeof(struct sockaddr);
seraddr.sin_family=AF_INET:
seraddr.sin_port=htons(2345);
seraddr.sin_addr.s_addr=inet_addr(ip);
if(connect(sfd,(struct sockaddr*)&seraddr,sizeof(struct sockaddr))==-1){
perror("connect");
close(sfd);
exit(-1);
}
#UDP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int main()
{
int sfd=socket(AF_INET<SOCK_DGRAM,0);
if(-1==sfd)
{
perror("socket");
exit(-1);
}
struct sockaddr_in saddr;
bzero(&saddr,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_port=htons(2345);
saddr.sin_addr.s_addr=INADDR_ANY;
if(bind(sfd,(struct sockaddr*)&saddr,sizeof(struct sockaddr))==-1)
{
perror("bind");
close(sfd);
exit(-1);
}
char buf[512]={0};
while(1)
{
struct sockaddr_in fromaddr;
bzero(&fromaddr,sizeof(fromaddr));
int fromaddrlen=sizeof(struct sockaddr);
if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&fromaddr,&fromaddrlen)==-1)
{
perror("recvfrom);
close(sfd);
exit(-1);
}
printf("receive from %s:%d,the message is %s\n",inet_ntoa(fromaddr.sin_addr),ntohs(fromaddr.sin_port),buf);
sendto(sfd,"world",6,0,(struct sockaddr*)&fromaddr,sizeof(struct sockaddr));
}
close(sfd);
}