Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // sieciowe.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- //#include <winsock.h>
- #include <iostream>
- #include <WinSock2.h>
- #include <ws2tcpip.h>
- using namespace std;
- #define MYPORT 3490
- int _tmain(int argc, _TCHAR* argv[])
- {
- /*0. Inicjalizacja*/
- WSADATA wsaData;
- int iResult;
- iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != 0) {
- printf("WSAStartup failed: %d\n", iResult);
- return 1;
- }
- // 1. Deklarujemy addinfo ktore zawiera sockaddr i inicjalizuje te wartosci
- struct addrinfo *result = NULL,
- *ptr = NULL,
- hints;
- ZeroMemory(&hints, sizeof(hints));
- // Cos jak wczesniej funkcja socket, tam dawalem takie argumenty
- // czyli to sa te sockaddr adresy gniazda
- hints.ai_family = AF_UNSPEC; // !!! Tut daje AF_UNSPEC
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- #define DEFAULT_PORT "27015"
- char* ip = "78.11.253.152";
- //char* port = "55555";
- // adres/nazwa strony, potem port http to domyslnie 80,
- // potem wskaznik na strukture ktora pokazuje jakie typy gniazda klient wspiera
- // wskzanik strukture ktora bedzie zawierac odp hosta.
- iResult = getaddrinfo("78.11.253.152", DEFAULT_PORT, &hints, &result);
- if (iResult != 0){
- cout << "Getaddrinfo blad: " << iResult << endl;
- WSACleanup();
- }
- //3. Tworzymy socket ConnectSocket
- SOCKET ConnectSocket = INVALID_SOCKET;
- /*Laczymy ptr na pierwszy adres zwrocony przez getaddrinfo*/
- ptr = result;
- // tworzymy gniazdo do laczenia sie z serverem
- ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
- if (ConnectSocket == INVALID_SOCKET){
- cout << "socket() poszlo zle\n";
- freeaddrinfo(result);
- WSACleanup();
- return 1;
- }
- // laczymy sie z serverem
- iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
- while (iResult == SOCKET_ERROR){
- closesocket(ConnectSocket);
- ConnectSocket = INVALID_SOCKET;
- cout << "Blad ustanowienia polaczenia\n";
- ptr = ptr->ai_next;
- iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
- // naprawde powinnismy sprobowac nastepny adres zwrocony przez getaddrinfo
- // freeaddrinfo(result);
- // WSACleanup();
- }
- // transmisja danych
- #define DEFAULT_BUFLEN 512
- int recvbuflen = DEFAULT_BUFLEN;
- char *sendbuf = "this is a test";
- char recvbuf[DEFAULT_BUFLEN];
- iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
- if (iResult == SOCKET_ERROR){
- cout << "mamy blad: " << WSAGetLastError() << endl;
- closesocket(ConnectSocket);
- WSACleanup();
- return 1;
- }
- cout << "Il wyslanych bajtow: " << iResult << endl;
- // Zamykanie polaczenia na wysylanie skoro nie mamy zamiar niczego wiecej wyslac
- // klient wciaz moze uzywac ConnectSock do odbierania danych
- iResult = shutdown(ConnectSocket, SD_SEND);
- if (iResult == SOCKET_ERROR) {
- printf("shutdown failed: %d\n", WSAGetLastError());
- closesocket(ConnectSocket);
- WSACleanup();
- return 1;
- }
- //odbieraj dane az server nie zamknie polaczenia
- do {
- iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
- if (iResult > 0)
- printf("Bytes received: %d\n", iResult);
- else if (iResult == 0)
- printf("Connection closed\n");
- else
- printf("recv failed: %d\n", WSAGetLastError());
- } while (iResult > 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement