赞
踩
管道(父子进程)、消息队列(内核经营消息队列)、共享内存(创建一个空间)、信号(通过pid号通信)、信号量(对临界资源,共享内存做P、V控制) 。
特点:依赖于Linux内核 A B两个通信基于内核。缺陷:无法多机通信 (不适用与两台不同的电脑)
网络编程:
协议(数据格式)(类似串口协议):TCP/UDP/THHP
Socket (套接字) :
TCP面向连接(如打电话要先拨号建立连接);UDP是无连接的,即发送数据之前 不需 要建立连接
TCP提供可靠的服务。也就是说,通过TCP连接传送的数据,无差错,不丢失,不重复,且按序到达;UDP尽最大努力交付,即不保证可靠交付
TCP面向字节流,实际上是TCP把数据看成一连串无结构的字节流;UDP是面向报文的UDP没有拥塞控制,因此网络出现拥塞不会使源主机的发送速率降低(对实时应用很有用,如IP电话,实时视频会议等)
每一条TCP连接只能是点到点的;UDP支持一对一,一对多,多对一和多对多的交互通信
TCP首部开销20字节;UDP的首部开销小,只有8个字节
TCP的逻辑通信信道是全双工的可靠信道,UDP则是不可靠信道
一台拥有IP地址的主机可以提供许多服务,比如Web服务、FTP服务、SMTP服务等
这些服务完全可以通过1个IP地址来实现。那么,主机是怎样区分不同的网络服务呢?显然不能只靠IP地址,因为IP 地址与网络服务的关系是一对多的关系。
实际上是通过“IP地址+端口号”来区 分不同的服务的。
端口提供了一种访问通道,
服务器一般都是通过知名端口号来识别的。例如,对于每个TCP/IP实现来说,FTP服务器的TCP端口号都是21,每个Telnet服务器的TCP端口号都是23,每个TFTP(简单文件传送协议)服务器的UDP端口号都是69。
Little endian: 小端字节序
Big endian :大端字节序
网络字节序=大端字节序
TCP server
TCP Client
scoket()函数
int socket(int domain, int type, int protocol);
// AF_INET 常用
bind()函数
int bind(int sockfd, const struct sockaddr *addr, socklent_t addrlen);
使用struct sockaddr_in
时需要强制转换为 (struct sockaddr *)struct sockaddr_in
int inet_aton(const char* straddr,struct in_addr *addrp);
//把字符串形式的“192.168.1.123”转为网络能识别的格式
char* inet_ntoa(struct in_addr inaddr);
//把网络格式的ip地址转为字符串形式
#include <netinet/in.h>
uint16_t htons(uint16_t host16bitvalue); //返回网络字节序的值uint32_t htonl(uint32_t host32bitvalue); //返回网络字节序的值uint16_t ntohs(uint16_t net16bitvalue); //返回主机字节序的值uint32_t ntohl(uint32_t net32bitvalue); //返回主机字节序的值
h代表host,n代表net,s代表short(两个字节),l代表long(4个字节),通过上面的4个函数可以实现主机字节序和网络字节序之间的转换。有时可以用INADDR_ANY,INADDR_ANY指定地址让操作系统自己获取
listen()函数
int listen(int sockfd, int backlog)// backlog :监听个数;
accept()函数
int accept(int sockfd,struct sockaddr *addr,socklen_t -*addrlen);
没接受到数据会阻塞在这里
数据收发常用第二套API:
connect()函数
int connect(int sockfd,const struct sockaddr *addr,socklen_t addrlen);
在Linux系统里找 struct sockaddr_in { }结构体
cd /usr/include/
进入该文件
grep "struct sockaddr_in {" * -nir
查找" "中的内容
vi linux/in.h +184
进入linux/in.h
文件的第184行
struct sockaddr_in {
__kernel_sa_family_t sin_family; /* Address family */
__be16 sin_port; /* Port number */
struct in_addr sin_addr; /* Internet address */
/* Pad to size of `struct sockaddr'. */
unsigned char __pad[__SOCK_SIZE__ - sizeof(short int) -
sizeof(unsigned short int) - sizeof(struct in_addr)];
};
struct in_addr {
__be32 s_addr;
};
127.0.0.1:会去获取本机IP地址
server.c
#include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> //#include <linux/in.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <stdlib.h> #include <string.h> int main() { int s_fd; int c_fd; int c_addrlen; struct sockaddr_in s_addr; struct sockaddr_in c_addr; s_addr.sin_family = AF_INET;//IPV4 s_addr.sin_port = htons(8899); //端口号 字节序转换 inet_aton("192.168.8.18",&s_addr.sin_addr);//ip地址 把字符串形式的“192.168.8.13”转为网络能识别的格式 //1.socket //int socket(int domain,int type,int protocol); s_fd = socket(AF_INET,SOCK_STREAM,0);//创建套接字 if(s_fd == -1){ perror("socket"); exit(-1); } //2.bind //int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); bind(s_fd,(struct sockaddr *)&s_addr,sizeof( struct sockaddr_in));//绑定IP地址和端口号 使用 struct sockaddr_in需要强制转换 //3.listen //int listen(int sockfd, int backlog); listen(s_fd,10);//监听10个 //4.accept //int accept(int sockfd,struct sockaddr *addr,socklen_t -*addrlen); c_addrlen = sizeof(struct sockaddr_in); c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&c_addrlen);//连接,完成3次握手的,将返回的网络描述符给c_fd,接着后面的操作,避免影响s_fd继续监听 if(c_fd == -1){ perror("accpet"); } printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr)); //获得客户端IP地址 while(1); //5.read/write //6.close close(s_fd); close(c_fd); return 0; }
打开Windows命令提示符:win+r
输入cmd
输入ping IP地址
能ping通,接着telnet IP地址 端口号
连接成功
如果提示:
在控制面板
->系统和安全
->程序
->启动或关闭Windows功能
->勾选上Telnet 客户端
或者Telnet Client
server2.c
#include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> //#include <linux/in.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <stdlib.h> #include <string.h> int main() { int s_fd; int c_fd; int c_addrlen; struct sockaddr_in s_addr; struct sockaddr_in c_addr; memset(&s_addr,0,sizeof( struct sockaddr_in)); //清空内容 memset(&c_addr,0,sizeof( struct sockaddr_in)); //清空内存 s_addr.sin_family = AF_INET; s_addr.sin_port = htons(8899); inet_aton("192.168.8.18",&s_addr.sin_addr); //1.socket //int socket(int domain,int type,int protocol); s_fd = socket(AF_INET,SOCK_STREAM,0); if(s_fd == -1){ perror("socket"); exit(-1); } //2.bind //int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); bind(s_fd,(struct sockaddr *)&s_addr,sizeof( struct sockaddr_in)); //3.listen //int listen(int sockfd, int backlog); listen(s_fd,10); //4.accept //int accept(int sockfd,struct sockaddr *addr,socklen_t -*addrlen); c_addrlen = sizeof(struct sockaddr_in); c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&c_addrlen); if(c_fd == -1){ perror("accpet"); } printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr)); //5.read/write write(c_fd,sendmessage,strlen(sendmessage)); nread = read(c_fd,readbuf,sizeof(readbuf)); if(nread == -1 ){ perror("read"); }else { printf("client:%s,message_size:%d\n",readbuf,nread); } while(1); //6.close close(s_fd); close(c_fd); return 0; }
运行结果:
old2_server.c
#include <stdio.h> #include <arpa/inet.h> #include <netinet/in.h> #include <stdlib.h> #include <string.h> //#include <linux/in.h> #include <sys/types.h> #include <sys/socket.h> int main(int agrc,char **argv) { int s_fd; int c_fd; int c_addrlen; int nread; char readbuf[128]; char *sendmessage = "I am server!"; struct sockaddr_in s_addr; struct sockaddr_in c_addr; memset(&s_addr,0,sizeof(struct sockaddr_in));//清理内存 memset(&c_addr,0,sizeof(struct sockaddr_in));//清理内存 s_addr.sin_family = AF_INET; s_addr.sin_port = htons(atoi(argv[2])); inet_aton(argv[1],&s_addr.sin_addr); //1.socket s_fd = socket(AF_INET,SOCK_STREAM,0); if(s_fd == -1){ perror("socket"); exit(-1); } //2.bind if(bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in)) == -1){ perror("bind"); exit(-1); } //3.listen if(listen(s_fd,5) == -1){ perror("listen"); exit(-1); } //4.accept c_addrlen = sizeof(struct sockaddr_in); while(1){ c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&c_addrlen); //等待接入 if(c_fd == -1){ perror("accept"); exit(-1); } printf("I get your IP :%s\n",inet_ntoa(c_addr.sin_addr)); //5.write/read if(fork() == 0){ nread = read(c_fd,readbuf,sizeof(readbuf)); if(nread == -1 ){ perror("read"); }else { printf("get message form client:%s,message_size:%d\n",readbuf,nread); } write(c_fd,sendmessage,strlen(sendmessage)); break; } } //6.close close(s_fd); close(c_fd); return 0; }
old2_client.c
#include <stdio.h> #include <arpa/inet.h> #include <netinet/in.h> #include <stdlib.h> #include <string.h> //#include <linux/in.h> #include <sys/types.h> #include <sys/socket.h> int main(int argc,char **argv) { int c_fd; int c_addrlen; int nread; char readbuf[128]; char *sendmessage = "I am client! "; struct sockaddr_in c_addr; memset(&c_addr,0,sizeof(struct sockaddr_in));//清理内存 c_addr.sin_family = AF_INET; c_addr.sin_port = htons(atoi(argv[2])); inet_aton(argv[1],&c_addr.sin_addr); //1.socket c_fd = socket(AF_INET,SOCK_STREAM,0);//创建套接字 if(c_fd == -1){ perror("socket"); exit(-1); } //2.connect connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr_in));//连接 //3.write/read write(c_fd,sendmessage,strlen(sendmessage)); nread = read(c_fd,readbuf,sizeof(readbuf)); if(nread == -1 ){ perror("read"); }else { printf("get message from server:%s,message_size:%d\n",readbuf,nread); } //6.close close(c_fd); return 0; }
运行结果:
old3_server.c
#include <stdio.h> #include <arpa/inet.h> #include <netinet/in.h> #include <stdlib.h> #include <unistd.h> #include <string.h> //#include <linux/in.h> #include <sys/types.h> #include <sys/socket.h> int main(int argc,char **argv) { int s_fd; int c_fd; int c_addrlen; int nread; char readbuf[128]; char sendmessage[128] = {0}; struct sockaddr_in s_addr; struct sockaddr_in c_addr; if(argc != 3){ printf("please input right param\n"); exit(-1); } memset(&readbuf,0,sizeof(readbuf)); memset(&s_addr,0,sizeof(struct sockaddr_in)); memset(&c_addr,0,sizeof(struct sockaddr_in)); s_addr.sin_family = AF_INET; s_addr.sin_port = htons(atoi(argv[2])); inet_aton(argv[1],&s_addr.sin_addr); //1.socket s_fd = socket(AF_INET,SOCK_STREAM,0); if(s_fd == -1){ perror("socket"); exit(-1); } //2.bind if(bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in)) == -1){ perror("bind"); exit(-1); } //3.listen if(listen(s_fd,5) == -1){ perror("listen"); exit(-1); } //4.accept c_addrlen = sizeof(struct sockaddr_in); while(1){ //主进程用于接收 c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&c_addrlen); if(c_fd == -1){ perror("accept"); exit(-1); } printf("I get your IP :%s\n",inet_ntoa(c_addr.sin_addr)); //5.write/read if(fork() == 0){ //创建子进程用于发送数据 while(1){ memset(&sendmessage,0,sizeof(sendmessage)); printf("input: "); gets(sendmessage); write(c_fd,sendmessage,strlen(sendmessage)); } } if(fork() == 0){ //创建子进程用于接受数据 while(1){ nread = read(c_fd,readbuf,sizeof(readbuf)); if(nread == -1 ){ perror("read"); }else { printf("get message form client:%s,message_size:%d\n",readbuf,nread); memset(&readbuf,0,sizeof(readbuf));//清除管道残留信息 sleep(1);//防止客服端断开服务端刷屏 } } } } //6.close close(s_fd); close(c_fd); return 0; }
old3_client.c
#include <stdio.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/in.h> #include <stdlib.h> #include <string.h> //#include <linux/in.h> #include <sys/types.h> #include <sys/socket.h> int main(int argc,char **argv) { int c_fd; int c_addrlen; int nread; char readbuf[128]; char sendmessage[128] = {0}; struct sockaddr_in c_addr; if(argc != 3){ printf("please input right param\n"); exit(-1); } memset(&readbuf,0,sizeof(readbuf)); memset(&c_addr,0,sizeof(struct sockaddr_in)); c_addr.sin_family = AF_INET; c_addr.sin_port = htons(atoi(argv[2])); inet_aton(argv[1],&c_addr.sin_addr); //1.socket c_fd = socket(AF_INET,SOCK_STREAM,0); if(c_fd == -1){ perror("socket"); exit(-1); } //2.connect if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr_in)) == -1){ perror("connect"); exit(-1); } //3.write/read while(1){ if(fork() == 0){ while(1){ memset(&sendmessage,0,sizeof(sendmessage)); printf("input: "); gets(sendmessage); write(c_fd,sendmessage,strlen(sendmessage)); } } nread = read(c_fd,readbuf,sizeof(readbuf)); if(nread == -1 ){ perror("read"); }else { printf("get message from server:%s,message_size:%d\n",readbuf,nread); memset(&readbuf,0,sizeof(readbuf)); sleep(1); //睡眠1秒,防止服务端断开,客服端刷屏 } } //6.close close(c_fd); return 0; }
运行结果:
testServer.c
#include <stdio.h> #include <arpa/inet.h> #include <netinet/in.h> #include <stdlib.h> #include <unistd.h> #include <string.h> //#include <linux/in.h> #include <sys/types.h> #include <sys/socket.h> int main(int argc,char **argv) { int s_fd; int mark = 0; int c_fd; int c_addrlen; int nread; char readbuf[128]; char sendmessage[128] = {0}; struct sockaddr_in s_addr; struct sockaddr_in c_addr; if(argc != 3){ printf("please input right param\n"); exit(-1); } memset(&readbuf,0,sizeof(readbuf)); memset(&sendmessage,0,sizeof(sendmessage)); memset(&s_addr,0,sizeof(struct sockaddr_in)); memset(&c_addr,0,sizeof(struct sockaddr_in)); s_addr.sin_family = AF_INET; s_addr.sin_port = htons(atoi(argv[2])); inet_aton(argv[1],&s_addr.sin_addr); //1.socket s_fd = socket(AF_INET,SOCK_STREAM,0); if(s_fd == -1){ perror("socket"); exit(-1); } //2.bind if(bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in)) == -1){ perror("bind"); exit(-1); } //3.listen if(listen(s_fd,5) == -1){ perror("listen"); exit(-1); } //4.accept c_addrlen = sizeof(struct sockaddr_in); while(1){ c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&c_addrlen); if(c_fd == -1){ perror("accept"); exit(-1); } printf("I get your IP :%s\n",inet_ntoa(c_addr.sin_addr)); //5.write/read mark++; if(fork() == 0){ while(1){ sprintf(sendmessage,"No.%d client",mark); write(c_fd,sendmessage,strlen(sendmessage)); sleep(3); } } if(fork() == 0){ while(1){ nread = read(c_fd,readbuf,sizeof(readbuf)); if(nread == -1 ){ perror("read"); }else { printf("get message form client:%s,message_size:%d\n",readbuf,nread); memset(&readbuf,0,sizeof(readbuf)); sleep(1); } } } } //6.close close(s_fd); close(c_fd); return 0; }
old3_client.c
#include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> //#include <linux/in.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <stdlib.h> #include <string.h> int main(int argc,char **argv) { int c_fd; int c_addrlen; int nread; char readbuf[128]; //char *writebuf = "massage form client !"; char writebuf[128] = {0}; struct sockaddr_in c_addr; memset(&c_addr,0,sizeof( struct sockaddr_in)); c_addr.sin_family = AF_INET; c_addr.sin_port = htons(atoi(argv[2])); //端口号 字节序转换 inet_aton(argv[1],&c_addr.sin_addr);//ip地址 把字符串形式的“192.168.8.13”转为网络能识别的格式 //1.socket //int socket(int domain,int type,int protocol); c_fd= socket(AF_INET,SOCK_STREAM,0);//创建套接字 if(c_fd == -1){ perror("socket"); exit(-1); } //2.connect //int connect(int sockfd,const struct sockaddr *addr,socklen_t addrlen); if((connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr_in))) == -1){ perror("connect:"); exit(-1); } while(1){ //printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr)); //获得客户端IP地址 //3.send if(fork() == 0){ while(1){ memset(writebuf,0,sizeof(writebuf)); printf("input: "); gets(writebuf); write(c_fd,writebuf,strlen(writebuf)); if(writebuf == "q"){ printf("end!\n"); exit(0); } } } while(1){ //read memset(readbuf,0,sizeof(readbuf)); nread = read(c_fd,readbuf,128); if(readbuf == "q"){ printf("end!\n"); exit(0); } if(nread == -1){ perror("read"); }else { printf("get massage form server:%d ,%s\n",nread,readbuf); } } } return 0; }
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。