1、TCP服务器端
#include
#define PORT 9999
#define IP "192.168.125.213"
int main(int argc, const char *argv[])
{
//创建套接字
int sfd = -1;
if((sfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("sock error");
return -1;
}
//绑定服务端地址端口号
//填充服务器地址端口号
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(IP);
sin.sin_port = htons(PORT);
if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) == -1)
{
perror("bind error");
return -1;
}
printf("bind success _%d_%s_%s\n",__LINE__,__FILE__,__func__);
//设置监听
if(listen(sfd,128) == -1)
{
perror("listen error");
return -1;
}
printf("listen success _%d_%s_%s\n",__LINE__,__FILE__,__func__);
//定义用于接收客户端地址信息结构体变量
struct sockaddr_in cin;
socklen_t socklen = sizeof(cin);
//接收客户端链接请求,创建新文件描述符用于通信
int newfd = -1;
if((newfd = accept(sfd,(struct sockaddr*)&cin,&socklen)) == -1)
{
perror("accept newfd error");
return -1;
}
printf("[%s:%d] 连接成功,newfd = %d\n",inet_ntoa(cin.sin_addr)\
,ntohs(cin.sin_port),newfd);
//收发消息
//定义搬运工
char rbuf[128] = "";
while(1)
{
bzero(buf,sizeof(buf));
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,sizeof(rbuf),0);
}
//关闭套接字
close(sfd);
close(newfd);
return 0;
}
2、TCP客户端
#include
#define SERPORT 9999
#define SERIP "192.168.125.213"
#define CLIPORT 8888
#define CLIIP "192.168.125.213"
int main(int argc, const char *argv[])
{
//创建套接字
int cfd = -1;
if((cfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket error");
return -1;
}
//端口号快速重用
int reuse =1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) == -1)
{
perror("setsockopt error");
return -1;
}
//可以绑定也可以不绑定
//填充地址信息结构体
struct sockaddr_in cin;
cin.sin_addr.s_addr = inet_addr(CLIIP);
cin.sin_port = htons(CLIPORT);
cin.sin_family = AF_INET;
//绑定
if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin)) == -1)
{
perror("bind error");
return -1;
}
//连接服务器
//填充服务器地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(SERIP);
sin.sin_port = htons(SERPORT);
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));
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1] = 0;
//发送
send(cfd,wbuf,sizeof(wbuf),0);
//接收
bzero(wbuf,sizeof(wbuf));
int res = recv(cfd,wbuf,sizeof(wbuf),0);
if(res == 0)
{
printf("服务器已下线\n");
break;
}
printf("[%s:%d]:%s\n",SERIP,SERPORT,wbuf);
}
//关闭套接字
close(cfd);
return 0;
}
3、UDP服务器端
#include
#define PORT 9999
#define IP "192.168.125.213"
int main(int argc, const char *argv[])
{
//创建套接字
int sfd = -1;
if((sfd = socket(AF_INET,SOCK_DGARM,0)) == -1)
{
perror("socket");
return -1;
}
//端口号快速重用
int reuse =1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSESDDR,&REUSE,sizeof(reuse))==-1)
{
perror("set");
return -1;
}
//绑定地址端口
//填充地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) == -1)
{
perror("bind");
return -1;
}
//收发
//定义接收客户端地址结构体
struct sockaddr_in cin;
socklen_t socklen = sizeof(cin);
while(1)
{
bzero(buf,sizeof(buf));
recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&socklen);
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
//加一个笑脸发回去
sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*),sizeof(cin));
printf("发送成功\n");
}
//关闭
close(sfd);
return 0;
}
4、UDP客户端
#include
#define PORT 9999
#define IP "192.168.125.213"
int main(int argc, const char *argv[])
{
//创建客户端套接字
int cfd = -1;
if((cfd = socket(AF_INET,SOCK_DGARM,0)) == -1)
{
perror("socket");
return -1;
}
//绑定地址信息
//填充服务器地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(IP);
sin.sin_port = htons(PORT);
//收发信息
char buf[128] = "";
while(1)
{
bzero(buf,sizeof(buf));
fgets(cfd,buf,sizeof(buf));
buf[strlen(buf) - 1] = 0;
sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin));
printf("发送成功\n");
//接收服务器发送的消息
recvfrom(cfd,buf,sizeof(buf),0,NULL,NULL);
printf("buf = %s\n",buf);
}
//关闭
close(cfd);
return 0;
}
5、思维导图