udp发送daemon

#include <iostream>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <arpa/inet.h>  

using namespace std;

int main (int argc, char *argv[])
{
cout << "Hello world! 5" << endl;
int localSocket,remoteSocket;
struct sockaddr_in  localAddr,remoteAddr;
localSocket = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&localAddr, sizeof(localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_port = htons(7777);
//localAddr.sin_addr.s_addr = INADDR_ANY;
localAddr.sin_addr.s_addr = inet_addr("192.168.1.40");
cout << "socket returned : " << localSocket << endl;
int result = bind(localSocket, (struct sockaddr *)&localAddr, sizeof(localAddr));
cout << "bind returned : " << result << endl;

FILE *pFile_;
pFile_ = fopen("test.264", "r+");

if(!pFile_)
{
cout << "fopen error " << pFile_ << endl;
}
char buffer[1024];


int i = 0;
while(1)
{

//memset(buffer, 0 , 1024);
socklen_t remoteAddrLength = sizeof(remoteAddr);
//result = recvfrom(localSocket, &buffer, sizeof(buffer), 0, (struct sockaddr *)&remoteAddr, &remoteAddrLength);
//fwrite(buffer, 1024, 1, pFile_);


//printf("received UDP %d, result %d \n", i++, result);
//构建一个char[]缓冲区,包含要发送的数据,在这里可以自己定义要发送的数据。
char sendBuff[512];
memset(sendBuff, 0, sizeof(sendBuff));

int fFrameSize = fread(sendBuff, 1, sizeof(sendBuff), pFile_);
printf("fFrameSize %d, %d \n", fFrameSize, sizeof(sendBuff));

if (fFrameSize != sizeof(sendBuff))
{
fseek(pFile_, 0L, SEEK_SET);
//printf("fFrameSize %d, %d \n", fFrameSize, sizeof(sendBuff));
//assert(false);
}

int nSend = sendto(localSocket, &sendBuff, fFrameSize,0, (struct sockaddr *)&localAddr,remoteAddrLength);
printf("i %d  nSend %d \n", i++, nSend);

usleep(2000);
}
fclose(pFile_);
fflush(pFile_);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值