Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

WINSOCK


  • Please log in to reply
6 replies to this topic

#1 chx101

chx101

  • Members
  • 32 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:uSa
  • Local time:07:06 PM

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:

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:06 PM

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.

#3 chx101

chx101
  • Topic Starter

  • Members
  • 32 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:uSa
  • Local time:07:06 PM

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

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:06 PM

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:

#5 chx101

chx101
  • Topic Starter

  • Members
  • 32 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:uSa
  • Local time:07:06 PM

Posted 11 April 2009 - 04:06 PM

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

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:06 PM

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?

#7 chx101

chx101
  • Topic Starter

  • Members
  • 32 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:uSa
  • Local time:07:06 PM

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

Edited by chx101, 11 April 2009 - 04:34 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users