赞
踩
1.基于TCP通信服务器
程序代码:
- 1 #include<myhead.h>
- 2 #define SER_IP "192.168.126.121"//服务器IP
- 3 #define SER_PORT 8888//服务器端口号
- 4 int main(int argc, const char *argv[])
- 5 {
- 6 //1.创建用于监听的套接字
- 7 int sfd=-1;
- 8 sfd=socket(AF_INET,SOCK_STREAM,0);
- 9 //参数1:IPv4的通信
- 10 //参数2:使用TCP通信类型
- 11 //参数3:参数2以确定通信类型,0
- 12 if(sfd==-1)
- 13 {
- 14 perror("socket error");
- 15 return -1;
- 16 }
- 17 printf("sfd=%d\n",sfd);//3 返回文件描述符,最小分配原则
- 18 //2.绑定IP地址和端口号
- 19 //2.1填充地址信息结构体
- 20 struct sockaddr_in sin;
- 21 sin.sin_family=AF_INET;//地址族
- 22 sin.sin_port=htons(SER_PORT);//要2字节无符号整数端口号(网络字节序)
- 23 //将主机字节序转换为网络字节序
- 24 sin.sin_addr.s_addr=inet_addr(SER_IP);//IP地址(sin的sin_addr的s_addr)
- 25 //要IP地址的网络字节序,将点分十进制数据转换为4字节无符号整数的网络字节序
- 26 //2.2绑定
- 27 if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))==-1)
- 28 //要绑定的套接字文件描述符,通用地址信号结构体,结构体大小
- 29 {
- 30 perror("bind error");
- 31 return -1;
- 32 }
- 33 printf("bind success\n");
- 34 //3.启动监听,允许客户端连接
- 35 if(listen(sfd,128)==-1)
- 36 {
- 37 perror("listen error");
- 38 return -1;
- 39 }
- 40 printf("listen success\n");
- 41 //4.客户端发来连接请求后,创建新的用于通信的套接字
- 42 //不想接收客户端地址信息结构体,则无需传参2,参3
- 43 //想要获取客户端地址信息结构体,要传入相关参数
- 44 struct sockaddr_in cin;//用于接收客户端地址信息结构体
- 45 socklen_t socklen=sizeof(cin);//用于接收客户端地址信息大小
- 46 int newfd=accept(sfd,(struct sockaddr*)&cin,&socklen);
- 47 //阻塞等待客户端请求,客户端发来连接请求后,创建新的套接字,返回套接字文件描述符
- 48 //参数1:服务器套接字文件描述符
- 49 //参数2:通用地址信号结构体,接收最新连接的客户端地址信息
- 50 //参数3:客户端套接字大小,地址传递
- 51 if(newfd==-1)//成功返回套接字文件描述符,失败-1
- 52 {
- 53 perror("accept error");
- 54 return -1;
- 55 }
- 56 printf("newfd=%d您有新的客户已经上线\n",newfd);
- 57 printf("客户端IP:%s,端口号:%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
- 58 //将IP地址的网络字节序(4字节无符号整数)转换为点分十进制字符串(IP地址的网络字节序)
- 59 //将端口的网络字节序(2字节无符号整数)转换为主机字节序(端口的网络字节序)
- 60 //5.通信套接字与客户端进行数据收发
- 61 char rbuf[128]="";
- 62 while(1)
- 63 {
- 64 //清空容器
- 65 bzero(rbuf,sizeof(rbuf));
- 66 //从套接字中读取数据
- 67 int res=read(newfd,rbuf,sizeof(rbuf));
- 68 if(res==0)
- 69 {
- 70 printf("客户端已下线\n");
- 71 break;
- 72 }
- 73 printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);
- 74 //将收到的消息加上其他字符回过去
- 75 strcat(rbuf,"*_*");
- 76 write(newfd,rbuf,strlen(rbuf));
- 77 }
- 78 //6.关闭套接字
- 79 close(newfd);
- 80 close(sfd);
- 81 return 0;
- 82 }
运行结果:
2. 基于TCP通信客户端
程序代码:
- 1 #include<myhead.h>
- 2 #define SER_IP "192.168.126.121"//服务器IP
- 3 #define SER_PORT 8888//服务器端口号
- 4 int main(int argc, const char *argv[])
- 5 {
- 6 //1.创建用于通信的套接字文件描述符
- 7 int cfd=socket(AF_INET,SOCK_STREAM,0);
- 8 //使用IPv4通信,使用TCP通信类型,已确定通信类型0
- 9 if(cfd==-1)
- 10 {
- 11 perror("socket error");
- 12 return -1;
- 13 }
- 14 printf("cfd=%d\n",cfd);//返回文件描述符,最小分配3
- 15 //2.绑定(不写系统默认绑定)
- 16 //3.连接服务器
- 17 //3.1填充要连接的服务器地址信息结构体
- 18 struct sockaddr_in sin;
- 19 sin.sin_family=AF_INET;//地址族
- 20 sin.sin_port=htons(SER_PORT);//端口号
- 21 sin.sin_addr.s_addr=inet_addr(SER_IP);//IP地址
- 22 //3.2连接服务器
- 23 if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
- 24 {
- 25 perror("connect error");
- 26 return -1;
- 27 }
- 28 printf("connect success\n");
- 29 //4数据收发
- 30 char wbuf[128]="";
- 31 while(1)
- 32 {
- 33 //清空数据
- 34 bzero(wbuf,sizeof(wbuf));
- 35 printf("请输入>>>");
- 36 fgets(wbuf,sizeof(wbuf),stdin);//终端输入
- 37 wbuf[strlen(wbuf)-1]=0;
- 38 //将数据发送给服务器
- 39 send(cfd,wbuf,strlen(wbuf),0);
- 40 printf("发送成功\n");
- 41 if(strcmp(wbuf,"quit")==0)
- 42 break;
- 43 //接收服务器发来的消息
- 44 //清空数据
- 45 bzero(wbuf,sizeof(wbuf));
- 46 recv(cfd,wbuf,sizeof(wbuf),0);
- 47 printf("收到消息为;%s\n",wbuf);
- 48 }
- 49 //5.关闭套接字
- 50 close(cfd);
- 51 return 0;
- 52 }
- ~
运行结果:
3.基于UDP通信服务器
程序代码:
- 1 #include<myhead.h>
- 2 #define SER_IP "192.168.126.121"//服务器IP
- 3 #define SER_PORT 8888//服务器端口号
- 4 int main(int argc, const char *argv[])
- 5 {
- 6 //1.创建用于通信的套接字
- 7 int sfd=socket(AF_INET,SOCK_DGRAM,0);
- 8 if(sfd==-1)
- 9 {
- 10 perror("socket error");
- 11 return -1;
- 12 }
- 13 printf("sfd=%d\n",sfd);
- 14 //2.绑定IP地址和端口号
- 15 //2.1填充地址信息结构体
- 16 struct sockaddr_in sin;
- 17 sin.sin_family=AF_INET;//地址族
- 18 sin.sin_port=htons(SER_PORT);//端口号
- 19 sin.sin_addr.s_addr=inet_addr(SER_IP);//IP地址
- 20 //2.2绑定
- 21 if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
- 22 {
- 23 perror("bind error");
- 24 return -1;
- 25 }
- 26 printf("bind success\n");
- 27 //3.收发数据
- 28 char rbuf[128]="";
- 29 //定义容器接收对端地址信息结构体
- 30 struct sockaddr_in cin;
- 31 socklen_t socklen=sizeof(cin);
- 32 while(1)
- 33 {
- 34 //清空数组
- 35 bzero(rbuf,sizeof(rbuf));
- 36 //接收数据
- 37 recvfrom(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&cin,&socklen);
- 38 printf("收到消息为:%s\n",rbuf);
- 39 //将消息加*_*回过去
- 40 strcat(rbuf,"*_*");
- 41 if(sendto(sfd,rbuf,strlen(rbuf),0,(struct sockaddr*)&cin,socklen)=
- 42 {
- 43 perror("sendto error");
- 44 return -1;
- 45 }
- 46 }
- 47 //4.关闭套接字
- 48 close(sfd);
- 49 return 0;
- 50 }
运行结果:
4.基于UDP的客户端
程序代码:
- 1 #include<myhead.h>
- 2 #define SER_IP "192.168.126.121"//IP地址
- 3 #define SER_PORT 8888//服务器端口
- 4 int main(int argc, const char *argv[])
- 5 {
- 6 //1.创建用于通信的套接字
- 7 int cfd=socket(AF_INET,SOCK_DGRAM,0);
- 8 if(cfd==-1)
- 9 {
- 10 perror("socket error");
- 11 return -1;
- 12 }
- 13 printf("cfd=%d\n",cfd);
- 14 //2.绑定IP地址和端口号
- 15 //非必要
- 16 //3.收发数据
- 17 char wbuf[128]="";
- 18 //定义容器,记录服务器的地址信息结构体
- 19 struct sockaddr_in sin;
- 20 sin.sin_family=AF_INET;
- 21 sin.sin_port=htons(SER_PORT);
- 22 sin.sin_addr.s_addr=inet_addr(SER_IP);
- 23 while(1)
- 24 {
- 25 //清空数组
- 26 bzero(wbuf,sizeof(wbuf));
- 27 //从终端获取数据
- 28 printf("请输入>>>");
- 29 fgets(wbuf,sizeof(wbuf),stdin);
- 30 wbuf[strlen(wbuf)-1]=0;
- 31 //将数据发送给服务器
- 32 sendto(cfd,wbuf,sizeof(wbuf),0,(struct sockaddr*)&sin,sizeof(sin));
- 33 printf("发送成功\n");
- 34 //接收服务器回复的消息
- 35 bzero(wbuf,sizeof(wbuf));
- 36 recvfrom(cfd,wbuf,sizeof(wbuf),0,NULL,NULL);
- 37 printf("收到消息:%s\n",wbuf);
- 38 }
- 39 //关闭套接字
- 40 close(cfd);
- 41 return 0;
- 42 }
运行结果:
流程图:
面试:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。