思维导图
代码练习
TCP实现通信
服务器端代码
#include <myhead.h>
#define SER_IP "192.168.152.135"
#define SER_PORT 8910
int main(int argc, const char *argv[])
{
//1创建用于监听的套接字
int sfd = -1;
sfd =socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1)
{
perror("socket error");
return -1;
}
printf("sfd = %d\n",sfd);
//2绑定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);
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");
return -1;
}
printf("listen success\n");
//4当有客户端发来连接请求后创建新的用于通信的套接字
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 =read(newfd,rbuf,sizeof(rbuf));
if(res==0)
{
printf("客户端已经下线\n");
break;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);
strcat(rbuf," O_o");
write(newfd,rbuf,strlen(rbuf));
}
//6关闭套接字
close(newfd);
close(sfd);
return 0;
}
客户端代码
#include <myhead.h>
#define SER_IP "192.168.152.135"
#define SER_PORT 8910
int main(int argc, const char *argv[])
{
//1创建用于通信的套接字文件描述符
int cfd = -1;
cfd =socket(AF_INET,SOCK_STREAM,0);
if(cfd==-1)
{
perror("socket error");
return -1;
}
printf("cfd = %d\n",cfd);
//2连接服务器
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(SER_PORT);
sin.sin_addr.s_addr = inet_addr(SER_IP);
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("connect error");
return -1;
}
printf("connect success\n");
//数据收发
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);
}
close(cfd);
return 0;
}
效果图
UDP实现通信
服务器端代码
#include <myhead.h>
#define SER_IP "192.168.152.135"
#define SER_port 8910
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1)
{
perror("socket error");
return -1;
}
printf("socket success\n");
//绑定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);
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," O_o");
if(sendto(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&cin,socklen)==-1)
{
perror("sendto error");
return -1;
}
}
close(sfd);
return 0;
}
客户端代码
#include <myhead.h>
#define SER_IP "192.168.152.135"
#define SER_PORT 8910
int main(int argc, const char *argv[])
{
int cfd=socket(AF_INET,SOCK_DGRAM,0);
if(cfd==-1)
{
perror("socket error");
return -1;
}
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;
}
效果图