18,356
社区成员
发帖
与我相关
我的任务
分享
recvfrom:
struct sockaddr_in add2;
int iLen2 =sizeof(add2);
// ZeroMemory(&add2,iLen2);
add2.sin_family=AF_INET;
add2.sin_port=htons(8080);
add2.sin_addr.s_addr = inet_addr("127.0.0.1");
struct sockaddr_in add;
int iLen =sizeof(add);
add.sin_family=AF_INET;
add.sin_port=htons(60000); ///server的监听端口
add.sin_addr.s_addr = inet_addr("222.35.136.224");
SOCKET hSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (hSocket == INVALID_SOCKET)
{
AfxMessageBox(_T("socked failed"));
WSACleanup();
return 0;
}
if(bind(hSocket, (struct sockaddr*)&add2, iLen2))
{
CString str;
str.Format(_T("recieve error: %d\n"), WSAGetLastError());
AfxMessageBox(str);
return 0;
}
char recvBuf[1024] = {0};
int dwRecv = 0;
while(1)
{
dwRecv = recvfrom(hSocket, recvBuf , 1024, 0, (struct sockaddr*)&add, &iLen);
if(dwRecv == SOCKET_ERROR)
{
CString str;
str.Format(_T("recieve error: %d\n"), WSAGetLastError());
AfxMessageBox(str);
break;
return 0;
}
sendto:
struct sockaddr_in add;
int iLen =sizeof(add);
ZeroMemory(&add,iLen);
add.sin_family=AF_INET;
add.sin_port=htons(60000); ///server的监听端口
add.sin_addr.s_addr = inet_addr("222.35.136.224");
SOCKET hSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (hSocket == INVALID_SOCKET)
{
AfxMessageBox(_T("socked failed"));
WSACleanup();
dlg->m_bSuccess =0;
dlg->UpdateList();
return dlg->m_bSuccess;
}
DWORD dwSend2=0,dwTemp2=0;
char* fBuf = fileBuff;
DWORD fSize = dlg->m_dwFileSize;
DWORD dTemp = 0;
// int iPack = 0;
while(fSize)
{
dwSend2 = sendto(hSocket, fBuf+dwTemp2, min(fSize,1000), 0, (struct sockaddr*)&add, iLen); //send
printf("send data to server:-----start-----\r\n%s\r\n------end------\r\n", fBuf+dwTemp2);
if(dwSend2 == SOCKET_ERROR)
{
CString str;
str.Format(_T("Error on send file %d\n"), WSAGetLastError());
AfxMessageBox(str);
dlg->m_bSuccess =0;
dlg->UpdateList();
return dlg->m_bSuccess;
}