T课上实现通信代码总结:
程序代码:
TCPSER.c
#include<myhead.h>
#define SER_IP "192.168.244.140" //服务器IP
#define SER_PORT 9999 //服务器端口号
int main(int argc, const char *argv[])
{//1.创建用于监听的套接字int sfd = -1;sfd = socket(AF_INET , SOCK_STREAM , 0);//参数1:表示使用的是ipv4的通信//参数2:表示使用tcp通信类型//参数3:表示前面已经特定了通信协议if(sfd == -1){perror("socket error");return -1;}printf("sfd = %d\n",sfd);//2.绑定ip地址和端口号//2.1填充地址信息结构体struct sockaddr_in sin;sin.sin_family = AF_INET; //地址族sin.sin_port = htons(SER_PORT);//端口号sin.sin_addr.s_addr = inet_addr(SER_IP); //IP地址//2.2绑定if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){perror("bind error");return -1;}printf("bind success\n");//3.启动监听,允许客户端连接if(listen(sfd,128) == -1){perror("listen error\n");return -1;}printf("listen success\n");//4.当有客户端发来连接请求后,创建新的用于通信的套接字//如果不想接收客户端地址信息结构体,则无需传入参数2和参数3//如果想要获取客户端信息结构体,则需要传入相关参数struct sockaddr_in cin; //用于接收客户端地址信息结构体socklen_t socklen = sizeof(cin);int newfd = accept(sfd,(struct sockaddr*)&cin,&socklen);if(newfd == -1){perror("accept error");return -1;}printf("newfd = %d 您有新的客户已经上线\n",newfd);printf("客户端IP:%s ,端口号为:%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));//5.通信套接字与客户端进行数据收发char rbuf[128] = "";while(1){//清空容器bzero(rbuf,sizeof(rbuf));//从套接字中读取数据int res = recv(newfd,rbuf,sizeof(rbuf),0);if(res == 0){printf("客户端已经下线\n");break;}printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);//将收到的信息加上其他字符传回去strcat(rbuf,"*-*");send(newfd,rbuf,strlen(rbuf),0);}//6.关闭套接字close(newfd);close(sfd);return 0;
}
TCPCLI.c
#include<myhead.h>
#define SER_IP "192.168.244.140" //服务器ip
#define SER_PORT 9999 //服务器端口号
int main(int argc, const char *argv[])
{//1.创建用于通信的套接字文件描述符int cfd = socket(AF_INET,SOCK_STREAM,0);if(cfd == -1){perror("socket error");return -1;}printf("cfd = %d\n",cfd);//2.绑定(非必须)//3.连接服务器//3.1填充要连接的服务器地址信息结构体struct sockaddr_in sin;sin.sin_family = AF_INET; //地址族sin.sin_port = htons(SER_PORT); //端口号sin.sin_addr.s_addr = inet_addr(SER_IP); //ip地址//3.2连接服务器if(connect(cfd ,(struct sockaddr*)&sin,sizeof(sin)) == -1){perror("connect error");return -1;}printf("connect success\n");//4.数据收发char wbuf[128] ="";while(1){//清空数据bzero(wbuf,sizeof(wbuf));printf("请输入》》");fgets(wbuf,sizeof(wbuf),stdin); //从终端输入wbuf[strlen(wbuf)-1] = 0;//将数据发送给服务器send(cfd , wbuf , strlen(wbuf) , 0);printf("发送成功\n");//判断发送的数据if(strcmp(wbuf,"quit") == 0){break;}//接受服务器发来的信息//清空数据bzero(wbuf,sizeof(wbuf));recv(cfd , wbuf , sizeof(wbuf), 0);printf("收到信息为:%s\n",wbuf);}//5.关闭套接字close(cfd);return 0;
}
UDPSER.c
#include<myhead.h>
#define SER_IP "192.168.244.140" //服务器IP
#define SER_PORT 9999 //服务器端口号
int main(int argc, const char *argv[])
{//创建用于通信的套接字int sfd = socket(AF_INET,SOCK_DGRAM,0);if(sfd == -1){perror("socket error");return -1;}printf("sfd = %d\n",sfd);//绑定IP地址和端口号//填充地址信息结构体struct sockaddr_in sin;sin.sin_family = AF_INET; //地址族sin.sin_port = htons(SER_PORT); //端口号sin.sin_addr.s_addr = inet_addr(SER_IP); //IP地址//绑定if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){perror("bind error");return -1;}printf("bind success\n");//收发数据char rbuf[128] = "";//定义容器接受对端地址信息结构体struct sockaddr_in cin;socklen_t socklen = sizeof(cin);while(1){//清空数组bzero(rbuf , sizeof(rbuf));//接收数据recvfrom(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&cin,&socklen);printf("收到信息为:%s\n",rbuf);//将消息加“*_*”返回strcat(rbuf,"*_*");if(sendto(sfd,rbuf,strlen(rbuf),0,(struct sockaddr*)&cin,socklen) == -1){perror("sendto error");return -1;}}//关闭套接字close(sfd);return 0;
}
UDPCLI.c
#include<myhead.h>
#define SER_IP "192.168.244.140" //服务器IP
#define SER_PORT 9999 //服务器端口号
int main(int argc, const char *argv[])
{//创建用于通信的套接字int cfd = socket(AF_INET,SOCK_DGRAM,0);if(cfd == -1){perror("socket error");return -1;}printf("cfd = %d\n",cfd);//绑定IP地址和端口号(非必要)//收发数据char wbuf[128] = "";//定义容器,记录服务器地址信息结构体struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(SER_PORT);sin.sin_addr.s_addr = inet_addr(SER_IP);while(1){//清空数组bzero(wbuf , sizeof(wbuf));//从终端获取数据printf("请输入:");fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1] = 0;//将数据发送给服务器sendto(cfd,wbuf,sizeof(wbuf),0,(struct sockaddr*)&sin,sizeof(sin));printf("发送成功\n");//接收服务器回复的消息bzero(wbuf , sizeof(wbuf));recvfrom(cfd,wbuf,sizeof(wbuf), 0 ,NULL,NULL);printf("收到的信息为:%s\n",wbuf);}//关闭套接字close(cfd);return 0;
}
运行结果:
课程总结: