2023/12/15 work
2023-12-18 06:13:54
1.? tcp 客户端、服务端通讯
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>
#define SERVICE_PORT 9994
#define SERVICE_IP "192.168.0.106"
int main(int argc, const char *argv[])
{
//服务端
//1. 创建socket
int serviceId;
if((serviceId = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("service socket error");
return -1;
}
//2. 绑定
struct sockaddr_in serviceAddr;
serviceAddr.sin_family = AF_INET;
serviceAddr.sin_port = htons(SERVICE_PORT);
serviceAddr.sin_addr.s_addr = inet_addr(SERVICE_IP);
if(bind(serviceId,(struct sockaddr*)&serviceAddr,sizeof(serviceAddr)) == -1){
perror("service bind error");
return -1;
}
//3.监听
if(listen(serviceId,128) ==-1){
perror("service listen error");
return -1;
}
printf("service success %d\n",serviceId);
//4. 接收连接请求,接收客户端ip
int newServiceId;
struct sockaddr_in cliAddr;
int cliAddrLen = sizeof(cliAddr);
if((newServiceId = accept(serviceId,(struct sockaddr*)&cliAddr
,&cliAddrLen)) == -1){
perror("service accept error");
return -1;
}
printf("[%s:%d]\n",inet_ntoa(cliAddr.sin_addr),ntohs(cliAddr.sin_port));
//5. 循环接收消息
char sbuf[120] ="";
while(1){
bzero(sbuf,sizeof(sbuf));
//读取
int len = recv(newServiceId,sbuf,sizeof(sbuf),0);
printf("[%s:%d ] 客户端发送的消息 %s \n",inet_ntoa(cliAddr.sin_addr),ntohs(cliAddr.sin_port),sbuf);
if(len == 0){
printf("client break\n");
break;
}
if(strcmp(sbuf,"tc") ==0){
printf("service break\n");
break;
}
//拼接个笑脸发送给客户端
strcat(sbuf,"*_*");
send(newServiceId,sbuf,sizeof(sbuf),0);
}
//关闭文件描述符
close(newServiceId);
close(serviceId);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>
#define SERVICE_PORT 9994
#define SERVICE_IP "192.168.0.106"
int main(int argc, const char *argv[])
{
//客户端
//1.创建socket
int clientId;
if((clientId = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("client socket error");
return -1;
}
//2. 客户端不绑定
//3. 客户端连接服务器端
struct sockaddr_in cliAddr;
cliAddr.sin_family = AF_INET;
cliAddr.sin_port = htons(SERVICE_PORT);
cliAddr.sin_addr.s_addr = inet_addr(SERVICE_IP);
if(connect(clientId,(struct sockaddr*)&cliAddr,sizeof(cliAddr)) == -1){
perror("client connect error");
return -1;
}
printf("client socket success\n");
//4.发送消息给客户端及接收服务器端消息
char cbuf[120] ="";
while(1){
bzero(cbuf,sizeof(cbuf));
fgets(cbuf,sizeof(cbuf),stdin);
//处理\n
cbuf[strlen(cbuf) -1] = 0;
//发送消息
send(clientId,cbuf,sizeof(cbuf),0);
bzero(cbuf,sizeof(cbuf));
//接收到的消息
int len = recv(clientId,cbuf,sizeof(cbuf),0);
printf("[%s:%d] 服务器发送的消息 %s\n",SERVICE_IP,SERVICE_PORT,cbuf);
if(len == 0 ){
printf("service break\n");
break;
}
if(strcmp(cbuf,"tc") == 0){
printf("client break");
break;
}
}
//关闭socket流描述符
close(clientId);
return 0;
}
2.udp 客户端 服务器端通讯
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>
#define SERVICE_PORT 8889
#define SERVICE_IP "192.168.0.106"
int main(int argc, const char *argv[])
{
//udp服务端
//1.创建socket
int udpService;
if((udpService = socket(AF_INET,SOCK_DGRAM,0)) == -1){
perror("udpService socket error");
return -1;
}
//绑定
struct sockaddr_in udpAddr;
udpAddr.sin_family = AF_INET;
udpAddr.sin_port = htons(SERVICE_PORT);
//点分十进制 ip 转化成 网络套接字
udpAddr.sin_addr.s_addr = inet_addr(SERVICE_IP);
if(bind(udpService,(struct sockaddr*)&udpAddr,sizeof(udpAddr)) == -1){
perror("udpService bind error");
return -1;
}
//服务端获取到客户端 ip 及端口
struct sockaddr_in udpCliAddr;
int udpCliAddrLen = sizeof(udpCliAddr);
printf("udpService success\n");
//接收/发送消息
char ubuf[120] ="";
while(1){
bzero(ubuf,sizeof(ubuf));
int len = recvfrom(udpService,ubuf,sizeof(ubuf),0,
(struct sockaddr*)&udpCliAddr,&udpCliAddrLen);
printf("[%s:%d] 客户端发送的消息 %s\n",inet_ntoa(udpCliAddr.sin_addr),
ntohs(udpCliAddr.sin_port),ubuf);
if(strcmp(ubuf,"tc") == 0){
printf("udpService break\n");
break;
}
//发送
strcat(ubuf,"*_*");
sendto(udpService,ubuf,sizeof(ubuf),0,(struct sockaddr*)&udpCliAddr,
udpCliAddrLen);
}
close(udpService);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>
#define SERVICE_PORT 8889
#define SERVICE_IP "192.168.0.106"
int main(int argc, const char *argv[])
{
//udp客户端
//1. 创建socket
int udpClient;
if((udpClient = socket(AF_INET,SOCK_DGRAM,0)) == -1){
perror("udpClient socket error");
return -1;
}
struct sockaddr_in udpService;
udpService.sin_family = AF_INET;
udpService.sin_port = htons(SERVICE_PORT);
udpService.sin_addr.s_addr = inet_addr(SERVICE_IP);
//int udpLen = sizeof(udpService);
printf("udpClient success\n");
char ucbuf[120] ="";
while(1){
bzero(ucbuf,sizeof(ucbuf));
printf("发送消息 ");
fgets(ucbuf,sizeof(ucbuf),stdin);
//处理\n
ucbuf[strlen(ucbuf)-1] = 0;
//发送
sendto(udpClient,ucbuf,sizeof(ucbuf),0,(struct sockaddr*)&udpService,sizeof(udpService));
bzero(ucbuf,sizeof(ucbuf));
//接收
int len = recvfrom(udpClient,ucbuf,sizeof(ucbuf),0,NULL,NULL);
printf("[%s:%d] 服务端回复的消息 %s\n",SERVICE_IP,SERVICE_PORT,ucbuf);
if(strcmp(ucbuf,"tc") == 0){
printf("udpClient break\n");
break;
}
}
close(udpClient);
return 0;
}
3. 思维导图
文章来源:https://blog.csdn.net/qinww2009/article/details/135048894
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!