WSADATA wsaData; //struktura WSADATA pre pracu s Winsock
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); //zakladna inicializacia
if (iResult != 0) //kontrola, ci nestala chyba
{
printf(“WSAStartup failed : % d\n”, iResult);
return 1;
}
struct addrinfo* result = NULL, * ptr = NULL; //struktura pre pracu s adresami
struct addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP; //pracujeme s protokolom TCP/IP
// Resolve the server address and port
iResult = getaddrinfo(“tu ma byt IP adresa servera”, “tu ma byt port”, &hints, &result);
if (iResult != 0) //kontrola, ci nenastala chyba
{
printf(“getaddrinfo failed : % d\n”, iResult);
WSACleanup();
return 1;
}
else
printf(“getaddrinfo didn’t fail…\n”);
//vytvorenie socketu a pripojenie sa
SOCKET ConnectSocket = INVALID_SOCKET;
// Attempt to connect to the first address returned by
// the call to getaddrinfo
ptr = result;
// Create a SOCKET for connecting to server => pokus o vytvorenie socketu
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) //kontrola, ci nenastala chyba
{
printf(“Error at socket() : % ld\n”, WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
else
printf(“Error at socket DIDN’T occur…\n”);
// Connect to server. => pokus o pripojenie sa na server
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) //kontrola, ci nenastala chyba
printf(“Not connected to server…\n”);
else
printf(“Connected to server!\n”);
if (iResult == SOCKET_ERROR) //osetrenie chyboveho stavu
{
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
WSACleanup();
return 1;
}
Sleep(250);
//posielanie
char sendbuf[/*treba zavolit vhodnu velkost, napr. 4096*/]; //buffer (v zasade retazec), kam sa budu ukladat data, ktore chcete posielat
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR)
{
printf(“send failed : % d\n”, WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf(“Bytes Sent : % ld\n”, iResult); //vypisanie poctu odoslanych dat
//prijimanie
#define DEFAULT_BUFLEN 4096 //makro, kde definujeme velkost prijimacieho buffera
int recvbuflen = DEFAULT_BUFLEN;
char recvbuf[DEFAULT_BUFLEN];
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); //funkcia na príjimanie
if (iResult > 0)
printf(“Bytes received : % d\n”, iResult); //prisli validne data, vypis poctu
else if (iResult == 0)
printf(“Connection closed\n”); //v tomto pripade server ukoncil komunikaciu
else
printf(“recv failed with error : % d\n”, WSAGetLastError()); //ina chyba
/ zavretie socketu
closesocket(ConnectSocket);
WSACleanup();
Upravili by ste tento program tak, aby sa to pripojilo na server a potom ma nechal zadat ID adresu, ktorú overi a nasledne my normalne ten server odpovie?
Are you looking for informatika tutoring? Find the right informatika tutor for online or in-person tutoring near you.
We access the information stored on your device for this website to function properly. This includes, for example, cookies or local browser cache. We use this to store the data necessary for the functioning of the website, data used for analytical purposes, or data stored by third parties.
If this information is essential for the operation of this website, we store it automatically. For everything else, we need your consent, which you can choose to give below. Your consent is valid for 12 months. Should you refuse, we will ask you for consent again in 6 months, but you are free to change your mind at any time. For more information, please see our GDPR and Terms of Use.