Click to See Complete Forum and Search --> : Visual Basic .net Socket Won't Recieve UDP Packets From C++ DLL


workinforaliving
January 3rd, 2005, 02:20 PM
I have a Windows CE dll sending UDP packets over a wireless network to a VB Program running on a PC. The VB program is not recieving the packets, though; I am using VB .net 2003. I wrote a C++ program on that same PC that recieves the packets just fine, so I know it is not a network issue. I have gone over the code for 2 days now and cannot see the problem, and there is no problem with the code for all I know (ya we've all said that before). Ive made sure that the Internet Family, Protocol type and Socket Type are matching (2, 2, 17). I've done lots of network programming in many languages, and the VB code matches up almost perfectly to the C++ code line for line, is there some special circumstance that I do not know about? Thanks for any help.

//sending Code
int sock;
char * sendBuffer;
hostent * host;
WSADATA wsaData;
sockaddr_in remote;
socklen_t remoteSize = sizeof(remote);

if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0){
return 0; //return if unable to load winsock dll
}
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
return 0; //return if unable to create socket
}
if((host = gethostbyname("192.168.1.103")) == NULL){
return 0;
}
remote.sin_family = AF_INET;
memcpy(&remote.sin_addr, host->h_addr_list[0], host->h_length);
remote.sin_port = 4765;

sendBuffer = (char *)&data; //global struct
while(true){
Sleep(1000);
globalTemp = globalTemp + 1;
sendto(sock, sendBuffer, sizeof(sendBuffer), 0, (struct sockaddr *)&remote, remoteSize);
}

//recieving code in visual basic, this does not recieve packets
Dim count As Integer = 0
Dim sock As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.IP)

Dim reciever As New IPEndPoint(IPAddress.Any, 4765)
Dim tempLocalEP As EndPoint = CType(reciever, EndPoint)
Try
sock.Bind(tempLocalEP)
Catch e As Exception
Label1.Text = "Winsock error: " & e.ToString()
End Try


Dim bytes(4096) As Byte //large enough to accept almost anything
'Label1.Text = "Socket is a " & sock.ProtocolType & " socket of type " & sock.SocketType & " of the family " & sock.AddressFamily
'Label1.Text = "Socket is bound to address " & sender.Address.ToString() & " on port " & sender.Port
Label1.Text = "Waiting for Packets..."
While True
sock.ReceiveFrom(bytes, 4096, SocketFlags.None, tempLocalEP)
count = count + 1
Label1.Text = "Received " & count & " packets."
End While

//recieving code in C, this recieves the packets just fine

int globalTemp = 0;
int sock;
char * recvBuffer;

sockaddr_in remote;
sockaddr_in local;

socklen_t rlen = sizeof(remote);
socklen_t llen = sizeof(local);

WORD wVersionRequested;
WSADATA wsaData;

wVersionRequested = MAKEWORD(2, 0); // Request WinSock v2.0
if (WSAStartup(wVersionRequested, &wsaData) != 0) { // Load WinSock DLL
cout << "Couldn't load winsock dll." << endl;
return 0;
}

sock = socket(AF_INET, SOCK_DGRAM, 0);

local.sin_family= AF_INET;
local.sin_addr.s_addr= INADDR_ANY;
local.sin_port= 4765;

recvBuffer = new char[1024];

bind(sock, (struct sockaddr *) &local, sizeof(local));

cout << "Socket is a 0 socket of type " << SOCK_DGRAM << " of the family " << AF_INET << endl;
cout << "Waiting for packets..." << endl << globalTemp << " packets recieved." << endl;

while(true){
recvfrom(sock, recvBuffer, 1024, 0, (struct sockaddr*)&remote, &rlen);
globalTemp = globalTemp + 1;
cout << globalTemp << " packets recieved. Packet contains: " << recvBuffer << endl;
}

workinforaliving
January 6th, 2005, 01:39 PM
Well we figured it out... when the destination port number is packaged in the packet being sent from the C socket, you must encode it to network format (using htons). When the packet reached the PC, it was sending it to the wrong port because it read the port number incorrectly. So in the end, this turned out to be a big/little endian format problem.