赞
踩
TCP模型图
Server.c
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <unistd.h>
-
- #define ERR_MSG(msg)do{\
- fprintf(stderr,"line:__%d__",__LINE__);\
- perror(msg);\
- }while(0)
-
- #define PORT 9999 //1024~49151
- #define IP "192.168.8.57"//ifconfig查看
-
- int main(int argc, const char *argv[])
- {
- //创建流式套接字 --> TCP
- int sfd = socket(AF_INET,SOCK_STREAM,0);
- if(sfd < 0){
- ERR_MSG("socket");
- return -1;
- }
- printf("sfd = %d\n",sfd);
-
- //允许端口快速被复用
- int reuse = 1;
- if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0){
- ERR_MSG("setsockopt");
- return -1;
- }
- printf("setsockopt success\n");
-
-
- //填充地址信息结构体,真实的地址信息结构体在man 7 ip
- //AF_INET --> man 7 ip
- struct sockaddr_in sin;
- sin.sin_family = AF_INET; //必须填AF_INET;
- sin.sin_port = htons(PORT); //1024~49151,网络字节序
- sin.sin_addr.s_addr = inet_addr(IP); //本机IP,ifconfig查看
-
- //功能:将IP地址和端口号绑定到指定套接字中;
- if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) < 0){
- ERR_MSG("bind");
- return -1;
- }
- printf("bind success __%d__\n",__LINE__);
-
- //功能:将套接字设置为被动监听状态,只负责监听是否有客户端连接成功;
- if(listen(sfd,10) < 0){
- ERR_MSG("listen");
- return -1;
- }
- printf("listen success\n");
-
- struct sockaddr_in cin; //存储客户端的地址信息
- socklen_t addrlen = sizeof(cin);
- int newfd = 0;
-
- //功能:阻塞函数,阻塞等待客户端连接成功
- //当客户端连接成功后,会从已完成连接的队列头中获取一个客户端信息,
- //并生成一个新的文件描述符;
- //注意:新生成的文件描述符才是用于通信的文件描述符;
- newfd = accept(sfd,(struct sockaddr *)&cin,&addrlen);
- if(newfd < 0){
- ERR_MSG("accept");
- return -1;
- }
- printf("[%s | %d] newfd = %d\n",
- inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
-
- char buf[128] = "";
- ssize_t res = 0;
- while(1){
- //接收
- bzero(buf,sizeof(buf));
- res = recv(newfd,buf,sizeof(buf),0);
- if(res < 0){
- ERR_MSG("recv");
- return -1;
- }
- else if(0 == res){
- printf("[%s | %d] newfd = %d 客户端断开连接\n",
- inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
- break;
- }
- printf("[%s | %d] newfd = %d : %s\n",
- inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
-
- //发送--->谁发给我数据,我就发还给谁
- strcat(buf,"*_*");
- if(send(newfd,buf,sizeof(buf),0) < 0){
- ERR_MSG("send");
- return -1;
- }
- printf("send success\n");
- }
-
- close(newfd);
- close(sfd);
- return 0;
- }
Client.c
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <unistd.h>
-
- #define ERR_MSG(msg)do{\
- fprintf(stderr,"line:__%d__",__LINE__);\
- perror(msg);\
- }while(0)
-
- #define PORT 9999 //服务器的端口
- #define IP "192.168.8.57"//服务器绑定的IP
-
- #define CLI_PORT 6666 //客户端需要绑定的端口号
- #define CLI_IP "192.168.8.57"//本机IP,ifconfig
-
- int main(int argc, const char *argv[])
- {
- //创建流式套接字
- int cfd = socket(AF_INET,SOCK_STREAM,0);
- if(cfd < 0){
- ERR_MSG("socket");
- return -1;
- }
-
- //绑定客户端自身的地址信息结构体--->非必须绑定,可以选择不绑
- //如果选择不绑定,则操作系统会默认绑定一个端口号给这个客户端
-
- ///
- /***************以下代码可以不写,不绑定*****************/
- //填充要连接的服务器的IP和端口
- struct sockaddr_in cin;
- cin.sin_family = AF_INET;
- cin.sin_port = htons(CLI_PORT); //客户端的port
- cin.sin_addr.s_addr = inet_addr(CLI_IP); //本机IP,ifconfig
-
- if(bind(cfd,(struct sockaddr *)&cin,sizeof(cin)) < 0){
- ERR_MSG("bind");
- return -1;
- }
- printf("client bind success\n");
- ///
-
-
- //填充要连接的服务器的IP和端口,根据地址族指定
- struct sockaddr_in sin;
- sin.sin_family = AF_INET; //必须填AF_INET;
- sin.sin_port = htons(CLI_PORT); //服务器绑定的端口号
- sin.sin_addr.s_addr = inet_addr(CLI_IP); //服务器绑定的IP地址
-
- //连接服务器
- if(connect(cfd,(struct sockaddr *)&sin,sizeof(sin)) < 0){
- ERR_MSG("connect");
- return -1;
- }
- printf("connect server success\n");
-
- char buf[128];
- ssize_t res = 0;
- while(1){
- bzero(buf,sizeof(buf));
- //发送数据给服务器,服务器收多少,发送多少字节
- printf("请输入>>>");
- fgets(buf,sizeof(buf),stdin);
- buf[strlen(buf)-1] = 0;
-
- if(send(cfd,buf,sizeof(buf),0) < 0){
- ERR_MSG("send");
- return -1;
- }
- printf("send success\n");
-
- //接收服务器返回的数据
- bzero(buf,sizeof(buf));
- res = recv(cfd,buf,sizeof(buf),0);
- if(res < 0){
- ERR_MSG("recv");
- return -1;
- }
- else if(0 == res){
- printf("服务器端关闭\n");
- break;
- }
- printf(":%s\n",buf);
- }
-
- //关闭文件描述符
- close(cfd);
- return 0;
- }
UDP模型图
Server.c
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <linux/input.h>
-
- #define ERR_MSG(msg)do{\
- fprintf(stderr,"line:__%d__",__LINE__);\
- perror(msg);\
- }while(0)
-
- #define PORT 9999 //1024~49151
- #define IP "192.168.8.57"//ifconfig查看
-
- int main(int argc, const char *argv[])
- {
- //创建报式套接字 socket
- int sfd = socket(AF_INET,SOCK_DGRAM,0);
- if(sfd < 0){
- ERR_MSG("socket");
- return -1;
- }
-
- //服务器自身的地址信息结构体,真实的地址信息结构体根据AF_INET查找
- //man 7 IP
- struct sockaddr_in sin;
- sin.sin_family = AF_INET;//必须填AF_INET
- sin.sin_port = htons(PORT);//端口号,1024~49151
- sin.sin_addr.s_addr = inet_addr(IP);//本机IP,ifconfig查找
-
- //绑定服务器的地址信息结构体 bind
- if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) < 0){
- ERR_MSG("bind");
- return -1;
- }
- printf("server bind success\n");
-
- char buf[128] = "";
- ssize_t res = 0;
-
- struct sockaddr_in cin; //存储发送方的地址信息
- socklen_t addrlen = sizeof(cin);
-
- while(1){
- bzero(buf,sizeof(buf));
- //接收客户端发送过来的数据
- res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&cin,&addrlen);
- if(res < 0){
- ERR_MSG("recvfrom");
- return -1;
- }
- printf("[%s:%d] :%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
-
- //将数据发送给客户端--->谁发给我,我发还给谁
- strcat(buf,"*_*");
- if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr *)&cin,sizeof(cin)) < 0){
- ERR_MSG("sendto");
- return -1;
- }
- printf("sendto success\n");
- }
-
- //关闭套接字文件描述符
- close(sfd);
- return 0;
- }
Client.c
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <linux/input.h>
-
- #define ERR_MSG(msg)do{\
- fprintf(stderr,"line:__%d__",__LINE__);\
- perror(msg);\
- }while(0)
-
- #define PORT 9999 //1024~49151
- #define IP "192.168.8.57"//ifconfig查找
-
- int main(int argc, const char *argv[])
- {
- //创建报式套接字 socket
- int sfd = socket(AF_INET,SOCK_DGRAM,0);
- if(sfd < 0){
- ERR_MSG("socket");
- return -1;
- }
-
- //绑定服务器的地址信息结构体 bind
- //---->非必须绑定,若不绑定,则由操作系统自定绑定
- //客户端自身的地址信息结构体,真实的地址信息结构体根据AF_INET查找
- //man 7 IP
-
- //服务器的地址信息结构体,供给下面的sendto函数使用
- struct sockaddr_in sin;
- sin.sin_family = AF_INET;//必须填AF_INET
- sin.sin_port = htons(PORT);//端口号,1024~49151
- sin.sin_addr.s_addr = inet_addr(IP);//本机IP,ifconfig查找
-
- char buf[128] = "";
- ssize_t res = 0;
-
- struct sockaddr_in rcvaddr; //存储发送方的地址信息
- socklen_t addrlen = sizeof(rcvaddr);
-
- while(1){
- //将数据发送给服务器
- bzero(buf,sizeof(buf));
- printf("请输入>>>");
- fgets(buf,sizeof(buf),stdin);
- buf[strlen(buf)-1] = 0;
-
- //要发送给服务器,所以地址信息结构体需要填服务器的
- if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,sizeof(sin)) < 0){
- ERR_MSG("sendto");
- return -1;
- }
- printf("sendto success\n");
-
- bzero(buf,sizeof(buf));
- //接收服务器发送过来的数据
- res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&rcvaddr,&addrlen);
- if(res < 0){
- ERR_MSG("recvfrom");
- return -1;
- }
- printf("[%s:%d] :%s\n",inet_ntoa(rcvaddr.sin_addr),ntohs(rcvaddr.sin_port),buf);
-
- }
-
- //关闭套接字文件描述符
- close(sfd);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。