2 qq 27598289 qq_27598289 于 2015.07.12 13:29 提问

有关ege和udp的问题,新手求大神,

我做的udp文件传输,在dos界面下服务端可以接收到来自客户端的文件,但我加上ege界面之后,发现服务端收不到了
这个是dos界面的服务端的接收函数
int file()
{

int err = INVALID_VALUE32;
WORD ver;
WSADATA wsaData;
int result;

ver = MAKEWORD(2, 2);  


err = WSAStartup(ver, &wsaData);
if (err != 0)   
{

    return 0;

}
if ((LOBYTE(wsaData.wVersion) != 2) || (HIBYTE(wsaData.wVersion) != 2))
{ 

    WSACleanup(); 
    return 0;

}
sockSc = socket(AF_INET, SOCK_DGRAM, 0); 
SOCKADDR_IN addrSc;
addrSc.sin_addr.S_un.S_addr = htonl(INADDR_ANY);;
addrSc.sin_family = AF_INET;
addrSc.sin_port = htons(7861); 
result=bind(sockSc, (SOCKADDR*)&addrSc, sizeof(SOCKADDR)); 
DWORD ID;
HANDLE handle=CreateThread(NULL,0,recvfunc,0,0,&ID);
while(recvexi<2)
{

  Sleep(100);

}

printf("退出成功\n");
return 0;
}
char TcharToChar(const TCHAR *tchar,char *_char)
{
int ilength;
ilength = WideCharToMultiByte(CP_ACP,0,tchar,-1,NULL,0,NULL,NULL);
WideCharToMultiByte(CP_ACP,0,tchar,-1,_char,ilength,NULL,NULL);
return *_char;
}//WCHAR到char类型的转换

DWORD WINAPI recvfunc(LPVOID lpParam)
{
recvexi=1;
FILE *p;
char name[5200];
char name1[5200];
getchar();
printf("请输入你想要接收的文件路径:\n");
gets_s(name);
fopen_s(&p,name,"wb");
while(recvexi==1)
{

    int rec=recvfrom(sockSc,(char *)(&data),2048,0,(sockaddr*)&server,&lenth);
    if(rec>0)
    {
        if(data.head.id=id)
        {
            fwrite(data.buf,sizeof(char),data.bufsize,p);
            recvsize=recvsize+data.bufsize;

            filesize = data.head.size;
            datahead.id = data.head.id;
            datahead.recvsize = recvsize;
            datahead.size = filesize;
            int d = sendto(sockSc,(char*)(&datahead),
            sizeof(datahead),0,(sockaddr*)&server,lenth);
            id = id+1;
        }

         else if(data.head.id < id)
         {

             datahead.id = data.head.id;
             datahead.recvsize=recvsize;
             datahead.size = filesize;
             int d = sendto(sockSc,(char*)(&datahead),
             sizeof(datahead),0,(sockaddr*)&server,lenth);

         }
           if(filesize == recvsize)
           {
               datahead.id=data.head.id;
               datahead.recvsize=-1;
               datahead.size=filesize;
               printf("filesize=%d\n",filesize);
               printf("recvsize=%d\n",recvsize);
               printf("文件已经收到了!");
               int d=sendto(sockSc,(char*)(&datahead),
               sizeof(datahead),0,(sockaddr*)&server,lenth);
               recvexi = 2;
               system("pause");
               break;
           }

        }
    }
   fclose(p);
   recvexi = 2;
   _tsystem(TEXT("pause"));
   return 0;

}//文件接收线程的具体使用
这个是加上ege之后的服务端的文件接收函数
int file()//文件传输函数
{
post();
system("pause");
outtextxy(300,300,silePath);
int err = INVALID_VALUE32;
WORD ver;
WSADATA wsaData;
int result;

ver = MAKEWORD(2, 2);  
err = WSAStartup(ver, &wsaData);//Windows异步套接字)的启动命令

if ( 0 != err )  // 判断函数返回值
{
    return 0;
}

if ( (2 != LOBYTE(wsaData.wVersion))//是取得16进制数最低(最右边)那个字节的内容
    || 2 != (HIBYTE(wsaData.wVersion)) )//取得16进制数最高(最左边)那个字节的内容
{ 
    WSACleanup(); //中止Windows Sockets在所有线程上的操作

    return 0;
}



sockSc = socket(AF_INET, SOCK_DGRAM, 0); 
addrSc.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSc.sin_family           = AF_INET;
addrSc.sin_port             = htons(7861); 
result=bind(sockSc, (SOCKADDR*)&addrSc, sizeof(SOCKADDR)); 
DWORD ID;
HANDLE handle = CreateThread(NULL, 0, recvfunc, 0, 0, &ID);//创建文件接收线程
while( 2 > recvexi )
{
  Sleep(100);
}

printf("退出成功\n");

return 0;

}
char TcharToChar(const TCHAR *tchar,char *_char)//WCHAR到CHAR类型的转换
{
int ilength;
ilength = WideCharToMultiByte(CP_ACP, 0, tchar,-1, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, tchar, -1, _char, ilength, NULL, NULL);

return *_char;

}

DWORD WINAPI recvfunc(LPVOID lpParam)//文件接收具体线程
{
recvexi=1;
FILE *p;

p = fopen(silePath,"wb");
outtextxy(300,300,silePath);

while( 1 == recvexi )
{   

    int rec = recvfrom(sockSc, (char *)(&data), 2048, 0, (sockaddr*)&server, &lenth);//接收到消息
    if( 0 < rec )
    {

        if( data.head.id = id )
        {
            fwrite(data.buf, sizeof(char), data.bufsize, p);//二进制流格式写入文件
            recvsize = recvsize + data.bufsize;         
            filesize = data.head.size;
            datahead.id = data.head.id;
            datahead.recvsize = recvsize;
            datahead.size = filesize;
            int d = sendto(sockSc, (char*)(&datahead),
            sizeof(datahead),0,(sockaddr*)&server,lenth);
            id = id + 1;
        }

        else if( data.head.id < id)
        {
            datahead.id = data.head.id;
            datahead.recvsize=recvsize;
            datahead.size = filesize;
            int d = sendto(sockSc, (char*)(&datahead),
            sizeof(datahead), 0, (sockaddr*)&server, lenth);

         }
         if( filesize == recvsize )
         {
             datahead.id = data.head.id;
             datahead.recvsize =- 1;
             datahead.size = filesize;
             char sFile = filesize; 
             char rFile = recvsize;
             outtextxy(170, 200, "filesize = ");
             outtextxy(200, 200, sFile);
             outtextxy(170, 210, "recvsize = ");
             outtextxy(200, 210, rFile);
             outtextxy(200, 220, "文件已经收到了!\n");
             int d = sendto(sockSc, (char*)(&datahead),
             sizeof(datahead), 0, (sockaddr*)&server, lenth);
             recvexi = 2;
             break;
         }
    }
}
fclose(p);
recvexi = 2;
_tsystem(TEXT("pause"));

return 0;

}

3个回答

oyljerry
oyljerry   Ds   Rxr 2015.07.12 13:44

界面一般是在UI主线程。你接受代码不知道会不会影响。你可以多加点日志,起码看服务器有没有收到客户端发送的事件,还是客户端根本没发送过来

qq_27598289
qq_27598289   2015.07.12 14:02

客户端是发过来了的,做了测试,就是用没加界面的服务端 是可以接收到的,在没有启动符号连接器的时候,在堆栈里面显示的是你的框架不正确或者丢失,
启动符号连接器之后,堆栈显示没问题,但还是传不过去。

qq_27598289
qq_27598289   2015.07.12 14:04

而且在调用文件传输的时候,进入file函数后,就只掉用了一个ege的输入框,其他就没啥了

Csdn user default icon
上传中...
上传图片
插入图片