winsock help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 98
Reputation: Nemoticchigga is an unknown quantity at this point 
Solved Threads: 2
Nemoticchigga Nemoticchigga is offline Offline
Junior Poster in Training

winsock help

 
0
  #1
Aug 6th, 2008
I am trying to run a simple program to read ethernet using UDP. Here is the code, but it always receives -1 for 'error', but I did it by the book. Do you guys see the problem? Thanks.

  1. //enum specifying the current status of the comm port.
  2. enum e_PortStatus
  3. {
  4. PORT_BUSY, //port is currently in use
  5. PORT_READY, //port is setup and ready for communication
  6. PORT_ERROR //an error has occured somewhere
  7. };
  8.  
  9. e_PortStatus myPortStatus;
  10.  
  11. //methods
  12. void InitializeDevice(); //method which sets up this class, called at startup
  13. void SetupPort(); //method which readies the port for communications
  14. void SendCommandToDevice(); //method which passes in parameters defining a message to be sent to the device on the port
  15.  
  16. static struct sockaddr_in mySocketAddress; //used to keep track of the connector's address
  17. static struct sockaddr_in theirSocketAddress; //used to keep track of the connector's address
  18. static int socket_fd; //Socket File Descriptor
  19. void StartDevice(); //Initializes the WINSOCK API
  20. void CloseDevice(); //Close the WINSOCK API
  21.  
  22.  
  23. void InitializeDevice()
  24. {
  25. SetupPort();
  26. }
  27.  
  28. void SetupPort()
  29. {
  30. int yes = 1;
  31.  
  32. StartDevice();
  33. if (myPortStatus != PORT_READY)
  34. CloseDevice();
  35.  
  36. // create a UDP-based socket
  37. socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
  38. if (socket_fd == -1)
  39. {
  40. myPortStatus = PORT_ERROR;
  41. cout << "CANNOT CREATE SOCKET" << endl;
  42. CloseDevice();
  43. }
  44.  
  45. if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (char* )&yes, sizeof(int)) == -1)
  46. {
  47. cout << "setsockopt" << endl;
  48. CloseDevice();
  49. }
  50.  
  51. // set up the local address
  52. mySocketAddress.sin_family = AF_INET;
  53. mySocketAddress.sin_port = htons(2010); //My_Port_Number
  54. mySocketAddress.sin_addr.s_addr = htonl(INADDR_ANY); //automatically fills in the IP address of the machine the process is running on
  55.  
  56. // bind in socket to local address (sockaddr)
  57. if (bind(socket_fd, (sockaddr *) &mySocketAddress, sizeof (mySocketAddress)) == -1)
  58. {
  59. myPortStatus = PORT_ERROR;
  60. cout << "CANNOT BIND SOCKET" << endl;
  61. int error = WSAGetLastError();
  62. CloseDevice();
  63. }
  64. }
  65.  
  66. char* ReceiveCommandFromDevice()
  67. {
  68. char* messageBuf = NULL;
  69. int size_received;
  70. int dwtheirSocketAddressSize;
  71.  
  72. /*Start of broadcast
  73. //Only use if you want to broadcast to every address on the network
  74. //So change theirSocketAddress.sin_addr.s_addr to INADDR_BROADCAST and uncomment
  75. if (setsockopt(socket_fd, SOL_SOCKET, SO_BROADCAST, (char* )&broadcast, sizeof(broadcast)) == -1)
  76. {
  77.  
  78. myPortStatus = PORT_ERROR;
  79. perror("setsockopt (SO_BROADCAST)");
  80. CloseDevice();
  81.  
  82. }
  83. //end of broadcast
  84.  
  85. */
  86.  
  87. //make sure Port is set to Ready Mode
  88. if ( myPortStatus == PORT_READY)
  89. {
  90. myPortStatus = PORT_BUSY;
  91.  
  92. size_received = recvfrom(socket_fd, messageBuf, sizeof(messageBuf), MSG_WAITALL, (SOCKADDR*) &theirSocketAddress, &dwtheirSocketAddressSize);
  93.  
  94. //check for error
  95. if (size_received == -1)
  96. {
  97. myPortStatus = PORT_ERROR;
  98. //cout << "CANNOT RECEIVE FROM PORT" << endl;
  99. CloseDevice();
  100. messageBuf = 'FAILURE';
  101. }
  102. else
  103. myPortStatus = PORT_READY;
  104. }
  105.  
  106. return messageBuf;
  107. }
  108.  
  109.  
  110. void CloseDevice()
  111. {
  112. closesocket(socket_fd);
  113. WSACleanup();
  114. }
  115.  
  116. void StartDevice()
  117. {
  118. WSADATA wsaData;
  119. int iResult;
  120.  
  121. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  122.  
  123. if (iResult != 0)
  124. {
  125. myPortStatus = PORT_ERROR;
  126. cout << "WSAStartup failed" << endl;
  127. CloseDevice();
  128. }
  129. else
  130. myPortStatus = PORT_READY;
  131. }
  132.  
  133.  
  134. int _tmain(int argc, _TCHAR* argv[])
  135. {
  136. InitializeDevice();
  137.  
  138. char* message;
  139.  
  140. while(1)
  141. {
  142. message = ReceiveCommandFromDevice();
  143.  
  144. cout << "Received: " << message << endl << endl;
  145.  
  146. Sleep(2000);
  147. }
  148.  
  149. return 0;
  150. }
Last edited by Nemoticchigga; Aug 6th, 2008 at 4:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: winsock help

 
0
  #2
Aug 6th, 2008
Probably because your use of messagebuf is broken in so many ways.

> size_received = recvfrom(socket_fd, messageBuf, sizeof(messageBuf),
messageBuf is NULL (1) and the sizeof is only the size of the pointer (2).
Since UDP is an "all-or-nothing" deal, if you can't fit the whole message into the space provided, then you don't get any of it. So any UDP packets larger than the typical 4-bytes for a pointer are just going to be dropped.

> messageBuf = 'FAILURE';
Please tell me your compiler told you this was bad?
Or do you just ignore all warnings and plough on until it crashes?

> int error = WSAGetLastError();
1. Add this to the call which fails.
2. Print it, so you know more.

Probably some other stuff, but that's enough for you to think about fixing.

And please don't just make messageBuf a char array and attempt to return it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC