UDP and C++.
- From: "Sunny Mahajan" <sunnyvardhan@xxxxxxxxxxx>
- Date: Thu, 4 Aug 2005 22:09:08 -0400
Hi,
I have an xPC system (Host PC and Target PC) setup.
I am writing a C++ program to run on Host PC of xPC to acquire data
from Target machine.
The target machine has a UDP block at output. It sends 32
'doubles'(Matlab data type) every 4 ms. So, I should be collecting
packets of size = 256 bytes at the host machine.
However, when I run my C++ code, I get ERROR# 10040 explained on the
following MSDN URL:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/recv_2.asp>
Is the data transferred as 'Double' or is it ASCII?
Here is my C++ code:
#include <stdio.h>
#include <winsock2.h>
int main()
{
printf("Initialize Winsock \n");
//----------------------
// Initialize Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
printf("Error at WSAStartup()\n");
printf("Create a SOCKET for connecting to server \n");
//----------------------
// Create a SOCKET for connecting to server
SOCKET ConnectSocket;
ConnectSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (ConnectSocket == INVALID_SOCKET)
{
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return 0;
}
printf("The sockaddr_in structure specifies the address family
\n");
//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "192.168.1.102" );
clientService.sin_port = htons(25000);
//clientService.sin_addr.s_addr = htonl(INADDR_ANY);
//bind socket to specific ip, port, etc.
bind(ConnectSocket, (SOCKADDR *) &clientService,
sizeof(clientService));
printf("Connect to server.\n");
//----------------------
// Connect to server.
if ( connect( ConnectSocket, (SOCKADDR*) &clientService,
sizeof(clientService) ) == SOCKET_ERROR)
{
printf( "Failed to connect.\n" );
WSACleanup();
return 0;
}
printf("Declare and initialize variables \n");
//------------------------------------------------------------------
// Declare and initialize variables.
int bytesRecv = 0; //SOCKET_ERROR;
char recvbuf[256] = "";
//------------------------------------------------------------------
// receive data.
printf("receiving bytes... \n");
int count = 0;
while( bytesRecv != SOCKET_ERROR )
{
printf("Loop#: %i \n",count);
bytesRecv = recv( ConnectSocket, recvbuf, 32, 0 );
printf("Error in recieving data: %ld\n", WSAGetLastError());
printf( "**Bytes Recieved: %ld\n", bytesRecv );
printf(recvbuf);
if (bytesRecv == 0 || WSAGetLastError() == WSAECONNRESET)
{
printf( "Connection Closed.\n");
break;
}
printf("\n Bytes Recv: %ld\n", bytesRecv );
}
WSACleanup();
return 0;
}
Thanks in advance.
.
- Prev by Date: i have a question about parameter transfer when i use uimenu callback
- Next by Date: sell hand painted oil paintings and frames from china
- Previous by thread: i have a question about parameter transfer when i use uimenu callback
- Next by thread: sell hand painted oil paintings and frames from china
- Index(es):
Relevant Pages
|