int main(int argc, char* argv[])
{
Data_t data;
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2,2);
if(WSAStartup(sockVersion, &wsaData) != 0)
{
return 0;
}
SOCKET serSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(serSocket == INVALID_SOCKET)
{
printf("socket error !");
return 0;
}
sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(75);
serAddr.sin_addr.S_un.S_addr = INADDR_ANY;
if(bind(serSocket, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
printf("bind error !");
closesocket(serSocket);
return 0;
}
sockaddr_in remoteAddr;
int nAddrLen = sizeof(remoteAddr);
printf("Listening ....\n");
while (true)
{
char recvData[500];
char sendData[1000];
char *buf[50];
int allByte=0;
int line=0;
char *start;
char *end;
int i, count;
int ret = recvfrom(serSocket, recvData,500, 0, (sockaddr *)&remoteAddr, &nAddrLen);
if (ret > 0)
{
recvData[ret] = 0x00;
printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
//printf("%s\n",recvData);
start = lskip(rstrip(recvData));
for(i=0; i<50; i++){
end = find_char_or_comment(start, '/');
if ( *end == '/' && *(++end)) {
*(--end) = '\0';
buf[i] = rstrip(start);
start = lskip(end + 1);
}
else{
buf[i] = rstrip(start);
break;
}
}
printf(" %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n",
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
end = find_char_or_comment(buf[0], '=');
buf[0] = lskip(end + 1);
end = find_char_or_comment(buf[1], '=');
buf[1] = lskip(end + 1);
if (!strcmp(buf[0], "ini")){
FILE *fp;
if(!strcmp(buf[1], "hi3520d")){
fp = fopen("E:\\uboot_env_3520D_256m-hf.ini","rb");
}
else if(!strcmp(buf[1], "hi3521a")){
fp = fopen("E:\\uboot_env_3521A_256m-hf.ini","rb");
}
if(!fp) {
printf("open error!");
return 1;
}
else
printf("文件已经打开,等待传输...\n");
while(!feof(fp))
{
memset(sendData, 0, 1000);
int numreads=fread(sendData, 1, 1000, fp);
int sByte = sendto(serSocket, sendData, numreads, 0, (sockaddr*)&remoteAddr, sizeof(remoteAddr));
allByte+=sByte;
line++;
if(SOCKET_ERROR==sByte)
{
printf("sendto()Failed:%d\n",WSAGetLastError());
closesocket(serSocket);
WSACleanup();
return -1;
}
}
printf("传输完毕,总共字节=%d,次数=%d\n",allByte,line);
}
else if(buf[0] == "xml"){
}
else if(buf[0] == "json"){
}
}
// char * sendData = "一个来自服务端的UDP数据包\n";
//sendto(serSocket, sendData, strlen(sendData), 0, (sockaddr *)&remoteAddr, nAddrLen);
}
closesocket(serSocket);
WSACleanup();
return 0;
}
|