使用UDP协议通过网口实现pc和dsp之间传图片

xifangdu14 2016-04-06 04:13:19
具体:对图片分组,分成多个包,pc传给dsp,dsp接收了,再传回来,问题就出在pc接收不到。我用dsp平台显示数据已经发送过来,但是,recvfrom失败。开始传送几十K的图片是没问题的,但是传送几百K,几兆的图片,接收了几个包(接收几个包也不一定的)之后,就不收了,显示接收失败,用slect()函数测试,slect并没有检测到可读的套接字。我也试图改了接收缓冲区,设置收发实现,以及recvfrom()之后sleep()了几秒,但是都不行,我也不知道怎么办了。我的客户端是用CFree5.0生成的exe。希望大神帮帮我。
#define _WIN32_WINNT 0x0601
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <types.h>
#include <errno.h>
#include <fcntl.h>
#include <stat.h>
#include <ws2tcpip.h>
#define PORT 7

#define TIMEOUT 15 /* (seconds) */

/*
* ======== main ========
*/
int main(int argc, char *argv[])
{
SOCKET s;
unsigned short port = PORT;
char *name = argv[0];
fd_set readfds;
struct timeval timeout;
time_t ts;
int nr;
int status = EXIT_SUCCESS;
int value;
struct addrinfo hints;
struct addrinfo *results = NULL;
char portString[10];
FILE* fq=NULL;
FILE* fd=NULL;
int rd;
int wd;
int rc;
int sc;
timeout.tv_sec = TIMEOUT;
timeout.tv_usec = 0;
struct stat st;
char * pbuf = NULL;
char * rbuf = NULL;
// int len = 0;

if (argc < 2)
{
fprintf(stderr, "Usage: %s [IPv4 or IPv6 addr] [port]\n", name);
status = EXIT_FAILURE;
goto QUIT;
}

if (argc > 2)
{
port = (unsigned short)strtoul(argv[2], NULL, 0);
}

/* convert the port into string format in order to pass it to getaddrinfo */
sprintf(portString,"%d", port);
printf("\nTesting UDP echo server at %s:%d\n", argv[1], port);

/* initialize sockets environment */
//socketsStartup();
WSADATA wsaData;
WSAStartup(MAKEWORD(1, 1), &wsaData);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;

/*
* getaddrinfo() fills in the results struct for us appropriately
* depending on whether the IP address is v4 or v6
*
* argv[1] = IPv4 or IPv6 address passed in from command line
* argv[2] = port number passed in from command line
*/
value = getaddrinfo(argv[1], portString, &hints, &results);
if (value != 0)
{
fprintf(stderr, "getaddrinfo failed: %d\n", value);
if (value == -2 || value == 11004)
{
fprintf(stderr, "unrecognized IP address\n");
}
status = EXIT_FAILURE;
goto QUIT;
}
printf("succeed to get the info of the host\n");

/* create socket. ai_family determined for us via getaddrinfo() call */
if ((s = socket(results->ai_family, results->ai_socktype, 0)) == INVALID_SOCKET)
{
fprintf(stderr, "%s: failed socket \n", name);
printf ( "Could not create socket : %d" , WSAGetLastError());
status = EXIT_FAILURE;
goto QUIT;
}
printf("succeed to create the socket\n");

int opt=1;
setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
//send bufffer
int nSendBuf=8*1024;// set as 32K
setsockopt(s,SOL_SOCKET,SO_SNDBUF,(const char*)&nSendBuf,sizeof(int));
// receive buffer
int nRecvBuf=8*1024;// set as 32K
setsockopt(s,SOL_SOCKET,SO_RCVBUF,(const char*)&nRecvBuf,sizeof(int));
//set timeout of sending and receiving
setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof( timeout ) );
setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof( timeout ) );

if((fq=fopen("C:/Users/Administrator/Desktop/zyc2.jpg","rb"))== NULL)
{
perror("file error");
status = EXIT_FAILURE;
goto QUIT;
}
printf("succeed to open the file\n");

// stat("C:/Users/Administrator/Desktop/zyc2.jpg",&st);/*attain the size of file*/
// len = st.st_size;

fseek (fq , 0 , SEEK_END);
int lSize;
lSize = ftell (fq);
printf("the size of file is %d\n",lSize);
rewind (fq);
//send and receive buffer
if (!(pbuf =(char *) malloc(sizeof(char)*lSize)))
{
perror("failed temp buffer allocation\n");
status = EXIT_FAILURE;
goto QUIT;

}
if (!(rbuf =(char *) malloc(sizeof(char)*lSize)))
{
perror("failed temp buffer allocation\n");
status = EXIT_FAILURE;
goto QUIT;

}

rd=fread(pbuf,1,lSize,fq);
// long temin1=ftell(fq);
// printf("terminate file:%d\n",temin1);
if(rd!=lSize)
{
perror("read error\n");
printf("return:%d",rd);
status = EXIT_FAILURE;
goto QUIT;
}
printf("read %d bytes\n",rd);

printf("sending\n");
int i_group;
int i_pakge=512;
i_group=lSize/i_pakge;
int i_leftpk=lSize-i_pakge*i_group;
int i_count=1;
int i_addr=0;
ts = time(0);
while(i_count<=i_group)
{
sc=sendto(s,&pbuf[i_addr],i_pakge,0, results->ai_addr, results->ai_addrlen);
if(sc<0)
{
fprintf(stderr, "%s: send failed \n", name);
printf ( "%d group send failed: %d" ,i_count,WSAGetLastError());
status = EXIT_FAILURE;
goto QUIT;
}
//printf("succeed to send %d group:%d bytes data\n",i_count,sc);
/* Wait for the reply */
//receive....
//printf("receiving\n");

rc=recvfrom(s,&rbuf[i_addr],i_pakge,0,results->ai_addr, &(results->ai_addrlen));
/* {
printf("Received back %d group:%d bytes\n",i_count, rc);
}
*/
Sleep(2000);
if(rc<0)
{
fprintf(stderr, "%s: recv failed \n", name);
printf ( "%d group receive failed : %d" ,i_count,WSAGetLastError());
status = EXIT_FAILURE;
// perror("rc=%d,failed in receiving\n",rc);
goto QUIT;
}

i_addr=i_addr+i_pakge;
i_count++;
}

sc=sendto(s,&pbuf[i_addr],i_leftpk,0, results->ai_addr, results->ai_addrlen);
if(sc<0)
{
fprintf(stderr, "%s: send failed \n", name);
printf ( "Could not send lefting: %d" , WSAGetLastError());
status = EXIT_FAILURE;
goto QUIT;
}
printf("left group: succeed to send %d bytes data\n",sc);
printf("left group:waitting for the reply");

rc=recvfrom(s,&rbuf[i_addr],i_leftpk,0, results->ai_addr, &(results->ai_addrlen));
if(rc<0)
{
fprintf(stderr, "%s: receive failed \n", name);
printf ( "Could not receive: %d" , WSAGetLastError());
status = EXIT_FAILURE;
goto QUIT;
}
printf("left group:succeed to reveive %d bytes data\n",rc);
printf("Exiting test. Time = %d seconds\n", (int)(time(0) - ts));

if((fd=fopen("C:/Users/Administrator/Desktop/zyc3.jpg","wb"))== NULL)
{
perror("file error");
status = EXIT_FAILURE;
goto QUIT;
}
printf("succeed to open the file\n");
if((wd=fwrite(rbuf,1,lSize,fd))!=lSize)
{
perror("write");
status = EXIT_FAILURE;
goto QUIT;
}
printf("write %d bytes\n",wd);

QUIT:

printf("\nerror:exit %d\n",status);
if (s >= 0)
{
(void)closesocket(s);
}
WSACleanup();
//socketsShutdown();
/* if(fq!=NULL)
{
fclose(fq);
}
if(fd!=NULL)
{
fclose(fd);
}
*/
fclose(fq);
fclose(fd);
fq=NULL;
fd=NULL;
free(pbuf);
free(rbuf);
return (status);
}
...全文
783 回复 打赏 收藏 转发到动态 举报
写回复
回复
切换为时间正序
请发表友善的回复…
发表回复
相关推荐

1,313

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告