problem in receiving a data within a socket

Hello.
I am using UDP protocol to receive a data from another computer in win32 API. For receiving a data I have created the socket,but data is not received from other computer.
But data is getting received when I am sending data from the same computer.
So any other additional settings are there while creating a socket for receiving a data from another computer.
So please tell me some solution to solve my problem. Thank you in advance.
Can you post your code please. Also provide the source and destination IP addresses of the two computers.
Sorry, but I can't post my code due to my company's rules and regulations.
IP address of source computer is 192.168.10.1 and for destination it is 192.168.10.7.
And one more important thing I need to mention that I am doing socket programming in win 32 API. For other platforms like c++, my code is working properly. But only in win 32 API,I am unable to receive a data. So is there any additional setting is required in case of win32 API ??
Thanx for your reply.
Ok, well at least the computers are on the same subnet (it depends on your subnet mask) so they should be able to ping each other.

And one more important thing I need to mention that I am doing socket programming in win 32 API.
WIN32 uses WinSock, which is derived from Berkeley Sockets, like all other socket implementations. If Windows was significantly different, it wouldn't work with the rest of the internet.

For other platforms like c++, my code is working properly.
C++ is a language, not a platform. So writing it in C++ isn't a factor.

But only in win 32 API,I am unable to receive a data.
Mmm, did you initialise the WinSock with WSAStartup?

This ought to help. https://msdn.microsoft.com/en-us/library/windows/desktop/ms738545%28v=vs.85%29.aspx

If this stuff doesn't help, that's as far as I can go without seeing some code.
Last edited on
I think, best way is to post my code, otherwise our conversion will be lengthy.


Sending part-
# include <winsock2.h>
# include <stdio.h>
# include "socket.h"


#define MMI_MESSAGE_SIZE 272
double mfddata[1];
int main()
{
Socket recvS, sendS;
sendS.createSendSock(1,SPORT);
mfddata[0] = 1.0;
while(1)
{
sendS.Bytessent=sendto(sendS.sendSocket,(char *)(&mfddata),
sizeof(mfddata), 0,(struct sockaddr*)&sendS.cli_addr,sendS.socClilen);
}
return 0;
}

Receiving part-
#include <winsock2.h>
#include <windows.h>
#include "resource.h"
#include "socket.h"
HWND hWnd;

double fmcdata[1];
Socket recvS;
DWORD WINAPI Thread_socket(LPVOID lpParameter)
{
HRGBDLL hRGBDLL = 0;
WSADATA WsaDat;
int nPort=6000;
SOCKET Socket=NULL;
SOCKADDR_IN SockAddr;
int SockAddrSize = sizeof (SockAddr);
int inDataLength;

int nResult=WSAStartup(MAKEWORD(2,2),&WsaDat);
if(nResult!=0)
{
MessageBoxA(hWnd,
"Winsock initialization failed",
"Critical Error",
MB_ICONERROR);
SendMessage(hWnd,WM_DESTROY,NULL,NULL);
}

Socket=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(Socket==INVALID_SOCKET)
{
MessageBoxA(hWnd,
"Socket creation failed",
"Critical Error",
MB_ICONERROR);
SendMessage(hWnd,WM_DESTROY,NULL,NULL);
}

SockAddr.sin_port=htons(nPort);
SockAddr.sin_family=AF_INET;
SockAddr.sin_addr.s_addr=htonl(INADDR_ANY);


if(bind(Socket,(LPSOCKADDR)&SockAddr,sizeof(SockAddr))==SOCKET_ERROR)
{
MessageBoxA(hWnd,"Unable to bind socket","Error",MB_OK);
SendMessage(hWnd,WM_DESTROY,NULL,NULL);
}
while(1)
{
inDataLength=recvfrom(Socket,(char *)(&fmcdata),sizeof(fmcdata),0,(SOCKADDR *)&SockAddr, &SockAddrSize);
}
}
recvfrom line is not executing, otherwise socket binding is ok...
Thank you in advance..
The server looks ok.

The client doesn't call appear to call WSAStartup. Have you looked at the return codes from the socket calls?

To be certain the packets are leaving the client, run Wireshark and look at the UDP packets with source address matching the client.
Ok.. I will check and thank you.
One more thing since I am receiving a data from remote computer so I have put UDP socket part in WM_CREATE case of windows procedure function and recvfrom part in FD_READ case of same windows procedure function. Am I doing a right thing?
Topic archived. No new replies allowed.