思维导图:
TCP服务器:
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.114.67"
int main(int argc, const char *argv[])
{
//创建流式套接字 socket
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd == -1){
ERR_MSG("socket");
return -1;
}
printf("socket success\n");
//通用地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
//绑定服务器的IP和端口 bind
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字设置为被监听状态 listen
if(listen(sfd,128) == -1){
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
//获取连接成功后的套接字
int newfd;
if((newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen)) == -1){
ERR_MSG("accept");
return -1;
}
printf("accept success\n");
char buf[128]="";
ssize_t res;
while(1){
//清空字符串
bzero(buf,sizeof(buf));
//接收数据
res = recv(newfd,buf,sizeof(buf),0);
if(-1 == res){
ERR_MSG("recv");
return -1;
}else if(0 == res){
printf("客户端关闭了\n");
break;
}
printf("收到:%s\n",buf);
//发送数据
strcat(buf,"^-^");
if(send(newfd,buf,strlen(buf),0) == -1){
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
//关闭文件描述符
close(sfd);
close(newfd);
return 0;
}
TCP客户端:
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.114.67"
int main(int argc, const char *argv[])
{
//创建报式套接字 socket
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd == -1){
ERR_MSG("socket");
return -1;
}
printf("socket success\n");
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
//连接服务器 connect
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
char buf[128]="";
ssize_t res;
while(1){
printf("请输入:");
bzero(buf,sizeof(buf));
//发送数据
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if(send(cfd,buf,strlen(buf),0) == -1){
ERR_MSG("send");
return -1;
}
printf("send success\n");
//接收数据
bzero(buf,sizeof(buf));
res=recv(cfd,buf,sizeof(buf),0);
if(res == -1){
ERR_MSG("recv");
return -1;
}else if(0 == res){
printf("服务器下线\n");
break;
}
printf("%s\n",buf);
}
//关闭文件描述符
close(cfd);
return 0;
}
UDP服务器:
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.114.67"
int main(int argc, const char *argv[])
{
//创建报式套接字 socket
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd == -1){
ERR_MSG("socket");
return -1;
}
printf("socket success\n");
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
//将套接字绑定服务器 bind
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){
ERR_MSG("bind");
return -1;
}
printf("bind asuccess\n");
char buf[128]="";
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
while(1){
bzero(buf,sizeof(buf));
//接收数据
if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen) == -1){
ERR_MSG("recvfrom");
return -1;
}
printf("收到:%s\n",buf);
//发送数据
strcat(buf,"*_*");
if(sendto(sfd,buf,strlen(buf),0,(struct sockaddr*)&cin,addrlen) == -1){
ERR_MSG("sendto");
return -1;
}
printf("send success\n");
}
//关闭文件描述符
close(sfd);
return 0;
}
UDP客户端:
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.114.67"
int main(int argc, const char *argv[])
{
//创建报式套接字 socket
int cfd=socket(AF_INET,SOCK_DGRAM,0);
if(cfd == -1){
ERR_MSG("socket");
return -1;
}
printf("socket success\n");
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
char buf[128]="";
while(1){
bzero(buf,sizeof(buf));
//发送数据
printf("请输入:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if(sendto(cfd,buf,strlen(buf),0,(struct sockaddr*)&sin,sizeof(sin)) == -1){
ERR_MSG("sendto");
return -1;
}
printf("send success\n");
//接收数据
bzero(buf,sizeof(buf));
if(recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen) == -1){
ERR_MSG("recvfrom");
return -1;
}
printf("收到:%s\n",buf);
}
//关闭文件描述符
close(cfd);
return 0;
}