Len tak surfujem po nete a najdem tento zdrojak :
/*
trojan - klasicky UDP klient - Windows verze
2006 (L) cZokL, mr [dot] czokl [at] gmail [dot] com
ad komenty - u kazdy fce z knihovny winsock najdete zbezny popis a
odkaz na dokumentaci + nejake to info i u jinych veci
*/
#include <iostream>
#include <windows.h>
#include <string>
#include <cstdlib>
using namespace std ;
/*
BUFSIZE velikost BUFSIZE musi byt u serveru i klienta stejna
aby nedcochazelo ke ztratam, u tehle kodu je zbytecny delat
header file pac by mel tak tri radky proto se tohle define
objevuje v obou kodech.
*/
#define BUFSIZE 1024
inline void usage() {
cout << "Usage: klient <hostname> <port>" ;
}
int main(int argc, char *argv[]) {
/*
prebrani vstupnich parametru
*/
if ( argc != 3 ) {
cerr << "chybny pocet argumentu" << endl ;
usage() ;
exit(-1) ;
}
string hostname = argv[1] ;
/* atoi() prevadi retezec na int cislo */
int port = atoi(argv[2]) ;
if ( port < 1 || port > 65536 ) {
cerr << "port neni v rozsahu 1-65536" << endl ;
usage() ;
exit(-1) ;
}
/*
WSAStartup - incializace knihovny winsock - tuto fci je nutne volat pred pouzitim fci z knihovny
vstupni "MAKEWORD(2,2)" urcuje verzi pozadovane knihovny, misto toho lze take napsat WINSOCK_VERSION
vystupni "wsadata" obsahuje info o knihovne
viz. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/wsastartup_2.asp
*/
WSAData wsadata ;
if( WSAStartup(MAKEWORD(2,2), &wsadata) != 0 ) {
cerr << "Nepodarilo se inicializovat knihovnu winsock" ;
exit(-1) ;
}
/*
Struktura hostent se naplni po zavolani fci gethostby*
informacema jako je ip adresa nebo nazev hosta
my pomoci toho prevedeme hostname na ip adresu
viz. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/hostent_2.asp
*/
hostent *H = gethostbyname(hostname.c_str()) ;
if ( H == NULL ) {
cerr << "hostname neexistuje" << endl ;
usage() ;
exit(-1) ;
}
string ip = inet_ntoa(*(in_addr *)H->h_addr_list[0]) ;
/*
struktura SOCKADDR_IN urcuje rodinu protokolu (AF_INET pro IPV4),
IP adresu a port, se kterym bude socket pracovat
viz. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_sockaddr_in_structure.asp
htons() prevadi int hodnotu na network byte order - prevod je nutny kuli little endian vs big endian
viz. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/htons_2.asp
inet_addr() prevadi const *char na network byte order ze stejneho duvodu
viz. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/inet_addr_2.asp
a inet_ntoa() dela presne obracenou vec
viz. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/inet_ntoa_2.asp
*/
SOCKADDR_IN service ;
service.sin_family = AF_INET ;
service.sin_addr.s_addr = inet_addr(ip.c_str()) ;
service.sin_port = htons(port) ;
/*
definice typu SOCKET vypada asi takto : #define SOCKET int - imho zbytecnost
fce socket vytvari socket descriptor(popisovac) - podobny jako prace se soubory,
pomoci neho cteme a zapisujeme z/do socketu
AF_INET - urcuje rodinu protokolu ipv4,
konst. SOCK_DGRAM urcuje ze budeme pouzivat UDP protokol
treti parametr nechame nulovy
viz. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/socket_2.asp
*/
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0) ;
if( sock == INVALID_SOCKET) {
cerr << "Nepodarilo se vytvorit socket descriptor " ;
/*
Funkce WSACleanup() deinicializuje knihovnu winsock
*/
WSACleanup() ;
exit(-1) ;
}
/*
sento odesila packet, vlastne zapise data na socket aby si je mohla druha strana
vyzvednout,
prebira soket deskrtiptor, data, ktera se maji poslat a pak urceni kam ze se maji odeslat
viz. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/sendto_2.asp
nejdrive precteme z inputu nejaky komend ktery pak server u sebe provede
*/
char buffer[BUFSIZE] ;
cin.getline(buffer, BUFSIZE) ;
if( sendto(sock, buffer, strlen(buffer) , 0, (sockaddr*) &service, sizeof(service) ) == SOCKET_ERROR ) {
cerr << "Chyba pri odesilani dat" ;
/*
fce closetsocket() uzavira socket
*/
closesocket(sock) ;
WSACleanup() ;
exit(-1) ;
}
/*
recvfrom prijima data ze socketu, parametry sou prakticky stejne jako u sendto()
viz. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/recvfrom_2.asp
*/
int bytes ;
int iSize = sizeof(service) ;
if ( (bytes = recvfrom(sock, buffer, BUFSIZE, 0, (sockaddr*) &service, &iSize)) == SOCKET_ERROR) {
cerr << "data nelze prijmout" << endl ;
closesocket(sock) ;
WSACleanup() ;
exit(-1);
}
/*
dulezita vec - fce recvfrom() vraci pocet prijatych bitu,
a protoze buffer je zpocatku zaplnenej nesmyslama a potom
prikazem, ktery odesilame (viz. radek cin.getline(buffer, BUFSIZE))
tak nastavime konec retezce za prijate bity aby se vypsalo jenom
to co opravdu prislo
*/
buffer[bytes] = '\0' ;
cout << buffer << endl ;
closesocket(sock) ;
WSACleanup() ;
system("pause") ;
return 0 ;
}
pri komplimacii mi komplimator hlasi :
------ Build started: Project: trojan, Configuration: Debug Win32 ------
Compiling...
trojak.cpp
Linking...
trojak.obj : error LNK2019: unresolved external symbol _recvfrom@24 referenced in function _main
trojak.obj : error LNK2019: unresolved external symbol _closesocket@4 referenced in function _main
trojak.obj : error LNK2019: unresolved external symbol _sendto@24 referenced in function _main
trojak.obj : error LNK2019: unresolved external symbol _WSACleanup@0 referenced in function _main
trojak.obj : error LNK2019: unresolved external symbol _socket@12 referenced in function _main
trojak.obj : error LNK2019: unresolved external symbol _htons@4 referenced in function _main
trojak.obj : error LNK2019: unresolved external symbol _inet_addr@4 referenced in function _main
trojak.obj : error LNK2019: unresolved external symbol _inet_ntoa@4 referenced in function _main
trojak.obj : error LNK2019: unresolved external symbol _gethostbyname@4 referenced in function _main
trojak.obj : error LNK2019: unresolved external symbol _WSAStartup@8 referenced in function _main
C:\C++\trojan\Debug\trojan.exe : fatal error LNK1120: 10 unresolved externals
Build log was saved at "file://c:\C++\trojan\trojan\Debug\BuildLog.htm"
trojan - 11 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Mozete mi prosim povedat kde je chyba ? Je chyba u mna alebo ?
Pouzivam MS v C++ pro
Za odpoved vopred dik ...