1.网络地址
1.ip地址+端口号
2.协议
1.TCP协议:面向连接,例如打电话模式,是一种可靠连接
2UDP协议:面向报文,例如发短信,适用于发送信息量较大数据对可靠性要求不高的,是一种不可靠连接
3.字节序
1.小端:数据的低位,对应存储于内存的低地址
2.大端:数据的高位,对应存储于内存的低地址
4.服务器与客户端
1.服务器:
1.socket建立通道
2.bind进行信息绑定
3.listen监听客户端连接
4.accept接收客户端连接
2.客户端:
1.socket连接通道
2.connect进行连接
3.双方:
1.都可以read读数据
2.都可以write发数据
5.相关API
1.socket:int socket(int domain, int type, int protocol),返回值服务器socket_fd,domian:ipv4网络类型,type:TCP/UDP协议类型,protocol:0可以自动根据domain和type进行默认配置
2.bind:int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen),sockfd:服务端socket_fd,addr:服务端信息配置,现在用sockaddr_in类型配置,注意 数据类型转化,addrlen:配置数据类型大小,一般sizeof(struct sockaddr)
/*struct sockaddr_in{ //IPv4 socket address sa_family_t sin_family; //Address family(AF_INET) in_port_t sin_port; //Port number struct in_addr sin_addr; //IPv4 addreddr //IPv4 4-byte address in_addr_t s_addr; //unsigned 32-bit integer};,一般不填*/
3.listen:int listen(int sockfd, int backlog),sockfd:服务端socket_fd,backlog:客户端最大连接数量
4.accept:int accept(int sockfd, struct sockaddr *c_addr, socklen_t *c_addr_len),返回值是所连接的客户端的sock_fd,sockfd:服务端socket_fd,c_addr:客户端信息配置,现在用sockaddr_in类型配置,注意 数据类型转化,c_addr_len:客户端信息配置的数据类型大小
5.connect:int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen),sockfd:服务端的socket_fd,addr:服务端信息配置,现在用sockaddr_in类型配置,注意 数据类型转化,addrlen:配置数据类型大小,一般sizeof(struct sockaddr)
6.read:int read(int sockfd,char *read_buf,ssize_t size)
7.write:int write(int sockfd,char *read_buf,ssize_t size)
6.应用:连接1个,读写各1次
1.服务端Server:
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(){
//int socket(int domain, int type, int protocol);
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1){
printf("socket:error\n");
exit(-1);
}
// int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
/*struct sockaddr_in{ //IPv4 socket address
sa_family_t sin_family; //Address family(AF_INET)
in_port_t sin_port; //Port number
struct in_addr sin_addr; //IPv4 addreddr //IPv4 4-byte address
in_addr_t s_addr; //unsigned 32-bit integer
};*/
struct sockaddr_in addr_in;
struct sockaddr_in c_addr_in;
addr_in.sin_family = AF_INET; //ipv4 type
addr_in.sin_port = htons(8887); //diy port
inet_aton("192.168.0.31",&(addr_in.sin_addr));//ip
bind(sockfd,(struct sockaddr*)(&addr_in),(socklen_t)sizeof(struct sockaddr));
// int listen(int sockfd, int backlog);
listen(sockfd,10);
// int accept(int sockfd, struct sockaddr *c_addr, socklen_t *c_addr_len);
int c_addr_len = sizeof(struct sockaddr);
// char *inet_ntoa(struct in_addr in);
int c_sockfd = accept(sockfd,(struct sockaddr*)(&c_addr_in),&c_addr_len);
printf("c_sockfd = %d\n",c_sockfd);
if(c_sockfd == -1){
perror("accept");
} else{
printf("===connect===\n");
char *c_ip = inet_ntoa(c_addr_in.sin_addr);
printf("client ip is %s\n",c_ip);
char read_buf[128] = {0};
int n_read = read(c_sockfd,read_buf,128);
printf("read_buf = %s,n_read = %d\n",read_buf,n_read);
char* write_buf = "AOYU from Server";
write(c_sockfd,write_buf,strlen("AOYU from Server"));
}
return 0;
}
2.客户端Client:
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(){
//int socket(int domain, int type, int protocol);
int c_s_fd = socket(AF_INET,SOCK_STREAM,0);
if(c_s_fd == -1){
printf("socket:error\n");
exit(-1);
}
// int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
/*struct sockaddr_in{ //IPv4 socket address
sa_family_t sin_family; //Address family(AF_INET)
in_port_t sin_port; //Port number
struct in_addr sin_addr; //IPv4 addreddr //IPv4 4-byte address
in_addr_t s_addr; //unsigned 32-bit integer
};*/
struct sockaddr_in addr_in;
addr_in.sin_family = AF_INET; //ipv4 type
addr_in.sin_port = htons(8887); //diy port
inet_aton("192.168.0.31",&(addr_in.sin_addr));//ip
// int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
int c_cnn = connect(c_s_fd,(struct sockaddr *)(&addr_in),sizeof(struct sockaddr));
// char *inet_ntoa(struct in_addr in);
if(c_cnn == -1){
perror("connect:");
exit(-1);
} else{
printf("===connect===\n");
char* write_buf = "ChenXin from client";
write(c_s_fd,write_buf,strlen("ChenXin from client"));
char read_buf[128] = {0};
int n_read = read(c_s_fd,read_buf,128);
printf("read_buf = %s,n_read = %d\n",read_buf,n_read);
}
return 0;
}
7.应用2:服务器连接多个设备,读写不限次数
server:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc,char** argv){
if(argc!=3){
printf("para is error\n");
exit(-1);
}
//int socket(int domain, int type, int protocol);
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1){
printf("socket:error\n");
exit(-1);
}
// int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
/*struct sockaddr_in{ //IPv4 socket address
sa_family_t sin_family; //Address family(AF_INET)
in_port_t sin_port; //Port number
struct in_addr sin_addr; //IPv4 addreddr //IPv4 4-byte address
in_addr_t s_addr; //unsigned 32-bit integer
};*/
int c_sockfd = 0;
struct sockaddr_in addr_in;
struct sockaddr_in c_addr_in;
int num_c = 0;
char write_buf[128] = {0};
char read_buf[128] = {0};
int n_read;
addr_in.sin_family = AF_INET; //ipv4 type
addr_in.sin_port = htons(atoi(argv[2])); //diy port
inet_aton(argv[1],&(addr_in.sin_addr));//ip
bind(sockfd,(struct sockaddr*)(&addr_in),(socklen_t)sizeof(struct sockaddr));
// int listen(int sockfd, int backlog);
listen(sockfd,10);
// int accept(int sockfd, struct sockaddr *c_addr, socklen_t *c_addr_len);
int c_addr_len = sizeof(struct sockaddr);
// char *inet_ntoa(struct in_addr in);
// fork();
while(1){
c_sockfd = accept(sockfd,(struct sockaddr*)(&c_addr_in),&c_addr_len);
if(c_sockfd == -1){
perror("accept");
}
num_c++;
printf("==get connect:client ip is %s\n",inet_ntoa(c_addr_in.sin_addr));
if(fork() == 0){
if(fork()==0){
while(1){
memset(write_buf,0,sizeof(write_buf));
sprintf(write_buf,"welcome,NO %d\n",num_c);
write(c_sockfd,write_buf,strlen(write_buf));
sleep(1);
}
}
while(1){
memset(read_buf,0,128);
n_read = read(c_sockfd,read_buf,128);
printf("n_read = %d,read_buf = %s\n",n_read,read_buf);
}
}
}
return 0;
}
client:
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc,char** argv){
if(argc != 3){
printf("params is error\n");
exit(-1);
}
//int socket(int domain, int type, int protocol);
int c_s_fd = socket(AF_INET,SOCK_STREAM,0);
if(c_s_fd == -1){
printf("socket:error\n");
exit(-1);
}
struct sockaddr_in addr_in;
addr_in.sin_family = AF_INET; //ipv4 type
addr_in.sin_port = htons(atoi(argv[2])); //diy port
inet_aton(argv[1],&(addr_in.sin_addr));//ip
// int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
int c_cnn = connect(c_s_fd,(struct sockaddr *)(&addr_in),sizeof(struct sockaddr));
// char *inet_ntoa(struct in_addr in);
if(c_cnn == -1){
perror("connect:");
exit(-1);
} else{
printf("===connect===\n");
char write_buf[128] = {0};
char read_buf[128] = {0};
int n_read;
pid_t pid = fork();
if(pid == 0){
while(1){
memset(write_buf,0,sizeof(write_buf));
printf("input:");
gets(write_buf);
write(c_s_fd,write_buf,strlen(write_buf));
}
}
if(pid > 0){
while(1){
memset(read_buf,0,sizeof(write_buf));
n_read = read(c_s_fd,read_buf,128);
printf("n_read = %d\n,read_buf = %s\n",n_read,read_buf);
}
}
}
return 0;
}