参考链接
注意事项
- 代码很low,主要看封装的Send函数所体现的切片思想即可
server代码
//udp服务端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <ctype.h>
#include <string>
#include <iostream>
#define SLICE_SIZE 5
bool Send(int sock_fd, char* buffer, int length)
{
int once; //一次发送的返回值
int total = 0; //目前发送了多少数据
int thislen = 0; //本次要发送的数据长度
do
{
if (length - total > SLICE_SIZE)
thislen = SLICE_SIZE;
else
thislen = length - total;
once = send(sock_fd, buffer + total, thislen, 0);
if (once <= 0) //如果once < 0则表示发送失败,用break跳出循环,返回FALSE
{
break;
}
total += once;
} while (total < length); //如果total < length说明数据还未完全发送,继续循环
if (total == length) //当total == length时,说明已完全发送,返回TRUE
return true;
return false;
}
int main(int argc, char const *argv[])
{
//1.创建socket
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd<0){
perror("socket error");
return -1;
}
//绑定
struct sockaddr_in serv;
struct sockaddr_in client;
bzero(&serv,sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_port = htons(7777);
serv.sin_addr.s_addr = htonl(INADDR_ANY);
bind(cfd,(struct sockaddr *)&serv,sizeof(serv));
//3.循环读取读取客户端消息和给客户端回复消息
int i;
int n;
socklen_t len;
char buf[1024];
std::string merge;
while(1){
//4.读取数据
memset(buf,0x00,sizeof(buf));
len = sizeof(client);
n = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&client,&len);
merge += buf;
printf("serv端口号=[%d]:本次发送字节数=[%d],buf=[%s]\n",ntohs(client.sin_port),n,buf);
// printf("serv端口号=[%d]:本次发送字节数=[%d],bufer=[%s]\n",ntohs(serv.sin_port),once,tmp);
std::cout << "merge= " << merge << ";" << "merge size is " << merge.size() << std::endl;
//5.给客户端回复消息
sendto(cfd,buf,n,0,(struct sockaddr*)&client,len);
}
//关闭套接字
close(cfd);
return 0;
}
client代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <ctype.h>
#define SLICE_SIZE 5
char* buffer = {"Helloworld\n"};
char* tmp[1024] ={0};
struct sockaddr_in serv;
bool Send(int sock_fd, char* buffer, int length)
{
int once; //一次发送的返回值
int total = 0; //目前发送了多少数据
int thislen = 0; //本次要发送的数据长度
do
{
if (length - total > SLICE_SIZE)
thislen = SLICE_SIZE;
else
thislen = length - total;
once = send(sock_fd, buffer + total, thislen, 0);
// printf("serv端口号=[%d]:本次发送字节数=[%d],bufer=[%s]\n",ntohs(serv.sin_port),once,tmp);
// sendto(cfd,buf,n,0,(struct sockaddr*)&serv,sizeof(serv));
if (once <= 0) //如果once < 0则表示发送失败,用break跳出循环,返回FALSE
{
break;
}
total += once;
} while (total < length); //如果total < length说明数据还未完全发送,继续循环
if (total == length) //当total == length时,说明已完全发送,返回TRUE
return true;
return false;
}
int main(){
//1.创建socket
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd<0){
perror("socket error");
return -1;
}
int n ;
serv.sin_family = AF_INET;
serv.sin_port = htons(7777);
inet_pton(AF_INET,"127.0.0.1",&serv.sin_addr.s_addr);
struct sockaddr_in client;
client.sin_family = AF_INET;
client.sin_port = htons(3333);
bind(cfd, (struct sockaddr*)&client, sizeof(client));
connect(cfd, (struct sockaddr*)&serv, sizeof(serv));
char buf[1024] = {'\0'};
// while(1){
//读取标准输入数据
// memset(buf,0x00,sizeof(buf));
// n = read(STDIN_FILENO,buf, sizeof(buf));
// buf[n-1] = 0x00;
// n -= 1;
Send(cfd,buffer, strlen(buffer));
//发送数据
// sendto(cfd,buf,n,0,(struct sockaddr*)&serv,sizeof(serv));
//读取数据
memset(buf,0x00,sizeof(buf));
n = recvfrom(cfd,buf,sizeof(buf),0,NULL,NULL);
// printf("[%d]:n=[%d],buf=[%s]\n",ntohs(serv.sin_port),n,buf);
// }
//关闭套接字
close(cfd);
return 0;
}