BleepingComputer.com: WINSOCK

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

WINSOCK using 'sendto' in WINSOCK

#1 User is offline   chx101 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 31
  • Joined: 20-February 09
  • Gender:Male
  • Location:uSa

  Posted 11 April 2009 - 11:10 AM

Hie guys...
i ran into some C++ WINSOCK programming codes and i thought i should give it a try . i wanted to make a chat program but first i had to try it on my own before i went looking for help and i came across this at msdn




#include <stdio.h>
#include "winsock2.h"

int main() {
  
  WSADATA wsaData;
  SOCKET SendSocket;
  sockaddr_in RecvAddr;
  int Port = 27015;
  char SendBuf[1024];
  int BufLen = 1024;

  //---------------------------------------------
  // Initialize Winsock
  WSAStartup(MAKEWORD(2,2), &wsaData);

  //---------------------------------------------
  // Create a socket for sending data
  SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

  //---------------------------------------------
  // Set up the RecvAddr structure with the IP address of
  // the receiver (in this example case "123.456.789.1")
  // and the specified port number.
  RecvAddr.sin_family = AF_INET;
  RecvAddr.sin_port = htons(Port);
  RecvAddr.sin_addr.s_addr = inet_addr("123.456.789.1");

  //---------------------------------------------
  // Send a datagram to the receiver
  printf("Sending a datagram to the receiver...\n");
  sendto(SendSocket, 
	SendBuf, 
	BufLen, 
	0, 
	(SOCKADDR *) &RecvAddr, 
	sizeof(RecvAddr));

  //---------------------------------------------
  // When the application is finished sending, close the socket.
  printf("Finished sending. Closing socket.\n");
  closesocket(SendSocket);

  //---------------------------------------------
  // Clean up and quit.
  printf("Exiting.\n");
  WSACleanup();
  return 0;
}


am using Dev-C++ 4.9.9.2 compiler, when i compile it i get these errors

  [Linker error] undefined reference to `WSAStartup@8' 
  [Linker error] undefined reference to `socket@12' 
  [Linker error] undefined reference to `htons@4' 
  [Linker error] undefined reference to `inet_addr@4' 
  [Linker error] undefined reference to `sendto@24' 
  [Linker error] undefined reference to `closesocket@4' 
  [Linker error] undefined reference to `WSACleanup@0' 
  ld returned 1 exit status 
 C:\Dev-Cpp\New Folder (2)\Makefile.win [Build Error]  [Project2.exe] Error 1


any help will b greatly appreciated...thx :thumbsup:
CHX101

#2 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 11 April 2009 - 11:15 AM

My gues is that you don't have the Winsock header file in a place where the compiler can find it.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#3 User is offline   chx101 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 31
  • Joined: 20-February 09
  • Gender:Male
  • Location:uSa

Posted 11 April 2009 - 03:48 PM

i tried changing
#include "winsock2.h"

to
#include <winsock2.h>

and
#include <winsock.h>


but still wont work
CHX101

#4 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 11 April 2009 - 03:54 PM

Have you physically downloaded the library and placed it where the compiler can find it? If the library does not exist on your system, you can change the include all you want, but it will not find it. Do you know for sure that the winsock2 header file exists on your system? :thumbsup:
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#5 User is offline   chx101 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 31
  • Joined: 20-February 09
  • Gender:Male
  • Location:uSa

Posted 11 April 2009 - 04:06 PM

yes there's both of them winsock.h and winsock2.h ...its driving me :thumbsup:
CHX101

#6 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 11 April 2009 - 04:20 PM

The next thing that I can think of is that sometimes when adding non-standard libraries, one has to add some switches to the compile instruction. Where is the tutorial that you are using?
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#7 User is offline   chx101 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 31
  • Joined: 20-February 09
  • Gender:Male
  • Location:uSa

Posted 11 April 2009 - 04:28 PM

winsock sendto function

you know what?
am thinking that this may be a MS Visual C++ code... :thumbsup:

oh 1 more thing they used Ws2_32.lib and Ws2_32.dll. so i guess thats why it wont work, but do you think Dev-Cpp would be able to compile if i had the library and DLL file? coz i could look it up and give it a try

This post has been edited by chx101: 11 April 2009 - 04:34 PM

CHX101

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users