一、UDP通信
本地通信:
a.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
typedef struct sockaddr* SP;
int main()
{
// 创建socket
int sockfd = socket(AF_LOCAL,SOCK_DGRAM,0);
if(0 > sockfd)
{
perror("socket");
return -1;
}
// 准备地址
struct sockaddr_un addr,src_addr;
addr.sun_family = AF_LOCAL;
strcpy(addr.sun_path,"socketA");
// 绑定会自动创建socket文件
remove(addr.sun_path);
if(bind(sockfd,(SP)&addr,sizeof(addr)))
{
perror("bind");
return -1;
}
for(;;)
{
char buf[256] = {};
socklen_t len;
// 接收
recvfrom(sockfd,buf,sizeof(buf),0,(SP)&src_addr,&len);
printf("recv:%s from:%s\n",buf,src_addr.sun_path);
if(!strcmp("quit",buf))
{
printf("通信结束!\n");
break;
}
// 发送
strcat(buf,"!");
sendto(sockfd,buf,strlen(buf)+1,0,(SP)&src_addr,len);
}
// 关闭
close(sockfd);
// 删除
remove(addr.sun_path);
}
b.c
。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
typedef struct sockaddr* SP;
int main()
{
// 创建socket对象
int sockfd = socket(AF_LOCAL,SOCK_DGRAM,0);
if(0 > sockfd)
{
perror("socket");
return -1;
}
// 准备绑定地址
struct sockaddr_un addr,dest_addr;
addr.sun_family = AF_LOCAL;
strcpy(addr.sun_path,"socketB");
dest_addr.sun_family = AF_LOCAL;
strcpy(dest_addr.sun_path,"socketA");
// 绑定
remove(addr.sun_path);
if(bind(sockfd,(SP)&addr,sizeof(addr)))
{
perror("bind");
return -1;
}
for(;;)
{
char buf[256] = {};
printf(">");
gets(buf);
// 发送
sendto(sockfd,buf,strlen(buf)+1,0,(SP)&dest_addr,sizeof(dest_addr));
if(!strcmp("quit",buf))
{
printf("通信结束!\n");
break;
}
socklen_t len;
// 接收
recvfrom(sockfd,buf,sizeof(buf),0,(SP)&addr,&len);
printf("recv:%s\n",buf);
}
// 关闭
close(sockfd);
// 删除
remove(addr.sun_path);
}
网络通信:
server.c
。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef struct sockaddr* SP;
int main()
{
int sockfd = socket(AF_INET,SOCK_DGRAM,0);
if(0 > sockfd)
{
perror("socket");
return -1;
}
struct sockaddr_in addr,src_addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(端口号);
addr.sin_addr.s_addr = inet_addr("ip");
if(bind(sockfd,(SP)&addr,sizeof(addr)))
{
perror("bind");
return -1;
}
for(;;)
{
char buf[256] = {};
socklen_t len;
recvfrom(sockfd,buf,sizeof(buf),0,(SP)&src_addr,&len);
printf("recv:%s\n",buf);
if(!strcmp("quit",buf))
{
printf("通信结束!\n");
break;
}
sendto(sockfd,"hehe",5,0,(SP)&src_addr,len);
}
close(sockfd);
}
client.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef struct sockaddr* SP;
int main()
{
int sockfd = socket(AF_INET,SOCK_DGRAM,0);
if(0 > sockfd)
{
perror("socket");
return -1;
}
// 目标地址
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(端口号);
addr.sin_addr.s_addr = inet_addr("目标ip");
for(;;)
{
char buf[256] = {};
printf(">");
gets(buf);
sendto(sockfd,buf,strlen(buf)+1,0,(SP)&addr,sizeof(addr));
if(!strcmp("quit",buf))
{
printf("通信结束!\n");
break;
}
socklen_t len;
recvfrom(sockfd,buf,sizeof(buf),0,(SP)&addr,&len);
printf("%s\n",buf);
}
close(sockfd);
}
二、TCP通信
server.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef struct sockaddr* SP;
void* run(void* arg)
{
// 备份客户端描述符
int clifd = *(int*)arg;
for(;;)
{
char buf[256] = {};
int ret = read(clifd,buf,sizeof(buf));
if(0 > ret)
{
printf("客户端异常结束!\n");
break;
}
printf("recv:%s\n",buf);
if(!strcmp("quit",buf))
{
printf("通信结束!\n");
break;
}
write(clifd,"xixi",5);
}
close(clifd);
}
int main()
{
// 创建socket
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(0 > sockfd)
{
perror("sockfd");
return -1;
}
// 准备地址
struct sockaddr_in addr,cliaddr;
addr.sin_family = AF_INET;
addr.sin_port = htons(端口号);
addr.sin_addr.s_addr = inet_addr("ip");
// 绑定
if(bind(sockfd,(SP)&addr,sizeof(addr)))
{
perror("bind");
return -1;
}
// 监听
listen(sockfd,10);
// 等待连接
for(;;)
{
socklen_t len;
// 等待客户端连接并获取描述符
int clifd = accept(sockfd,(SP)&cliaddr,&len);
// 创建线程为客户端服务
pthread_t tid;
pthread_create(&tid,NULL,run,&clifd);
usleep(500);
}
}
client.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef struct sockaddr* SP;
int main()
{
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(0 > sockfd)
{
perror("socket");
return -1;
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(端口号);
addr.sin_addr.s_addr = inet_addr("目标ip");
if(connect(sockfd,(SP)&addr,sizeof(addr)))
{
perror("connect");
return -1;
}
for(;;)
{
char buf[256] = {};
printf(">");
gets(buf);
write(sockfd,buf,strlen(buf)+1);
if(!strcmp("quit",buf))
{
printf("通信结束!\n");
break;
}
read(sockfd,buf,sizeof(buf));
printf("server:%s\n",buf);
}
close(sockfd);
}