TCP服务器端代码实现
#include <myhead.h>
#define SER_PORT 6666
#define SER_IP "192.168.254.129"
int main(int argc,const char *argv[])
{
//1.创建套接字文件
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1)
{
perror("socket error");
return -1;
}
printf("socket success,sfd = %d\n",sfd);
//将端口号快速重用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
}
printf("端口号快速重用成功\n");
//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 addrlen = sizeof(cin);
int newfd = accept(sfd,(struct sockaddr *)&cin,&addrlen);
if(newfd==-1)
{
perror("accept error");
return -1;
}
printf("[%s:%d]已成功连接一个客户端\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
//数据收发
char buf[128]="";
while(1)
{
memset(buf,0,sizeof(buf));
int res = recv(newfd,buf,sizeof(buf),0);
if(res==-1)
{
perror("read error");
return -1;
}
else if(res==0)
{
printf("客户端已经下线\n");
close(newfd);
break;
}
printf("[%s:%d] : %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
strcat(buf,"*_*");
if(send(newfd,buf,strlen(buf),0)==-1)
{
perror("发送失败");
return -1;
}
printf("发送成功\n");
}
close(sfd);
return 0;
}
TCP客户端实现
#include <myhead.h>
#define SER_PORT 6666
#define SER_IP "192.168.254.129"
#define CLI_PORT 8888
#define CLI_IP "192.168.254.129"
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.绑定IP地址和端口号
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(CLI_PORT);
cin.sin_addr.s_addr = inet_addr(CLI_IP);
if(bind(cfd,(struct sockaddr *)&cin,sizeof(cin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
//3.连接到服务器
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("连接服务器成功\n");
//4.数据收发
char buf[128]="";
while(1)
{
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
send(cfd,buf,strlen(buf),0);
printf("发送成功\n");
memset(buf,0,sizeof(buf));
recv(cfd,buf,sizeof(buf),0);
printf("收到服务器消息为:%s\n",buf);
}
//5.关闭套接字
close(cfd);
return 0;
}
UDP服务器端实现
#include <myhead.h>
#define SER_PORT 9999
#define SER_IP "192.168.254.129"
int main(int argc,const char *argv[])
{
//1.创建用于通信的服务器套接字文件描述符
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd==-1)
{
perror("socket error");
return -1;
}
//将端口号快速重用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
{
perror("reuse error");
return -1;
}
printf("端口号快速重用成功\n");
//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.数据收发
char buf[128]="";
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
while(1)
{
memset(buf,0,sizeof(buf));
recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&cin,&addrlen);
printf("收到的消息为:%s\n",buf);
strcat(buf,"*_*");
if(sendto(sfd,buf,strlen(buf),0,(struct sockaddr *)&cin,sizeof(cin))==-1)
{
perror("发送error");
return -1;
}
printf("发送成功\n");
}
close(sfd);
return 0;
}
UDP客户端实现
#include <myhead.h>
#define SER_PORT 9999
#define SER_IP "192.168.254.129"
#define CLI_PORT 5555
#define CLI_IP "192.168.254.129"
int main(int argc,const char *argv[])
{
//1.创建用于通信的服务器套接字文件描述符
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd==-1)
{
perror("socket error");
return -1;
}
//将端口号快速重用
int reuse = 1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
{
perror("reuse error");
return -1;
}
printf("端口号快速重用成功\n");
//2.为套接字绑定ip地址和端口号
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(CLI_PORT);
cin.sin_addr.s_addr = inet_addr(CLI_IP);
if(bind(cfd,(struct sockaddr *)&cin,sizeof(cin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
//3.数据收发
char buf[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)
{
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(sendto(cfd,buf,strlen(buf),0,(struct sockaddr *)&sin,sizeof(sin))==-1)
{
perror("发送error");
return -1;
}
printf("发送成功\n");
memset(buf,0,sizeof(buf));
recvfrom(cfd,buf,sizeof(buf),0,NULL,NULL);
printf("收到服务器消息为:%s\n",buf);
}
close(cfd);
return 0;
}
思维导图
![](https://i-blog.csdnimg.cn/direct/a5127867ad5d4a119e5fb9e96787153e.png)
![](https://i-blog.csdnimg.cn/direct/7b8d25167b104fcab95e32d28f410eea.png)