Advertisement
Guest User

Untitled

a guest
Apr 6th, 2015
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. // sieciowe.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. //#include <winsock.h>
  6. #include <iostream>
  7. #include <WinSock2.h>
  8. #include <ws2tcpip.h>
  9.  
  10. using namespace std;
  11.  
  12. #define MYPORT 3490
  13.  
  14.  
  15. int _tmain(int argc, _TCHAR* argv[])
  16. {
  17.     /*0. Inicjalizacja*/
  18.     WSADATA wsaData;
  19.     int iResult;
  20.     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  21.     if (iResult != 0) {
  22.         printf("WSAStartup failed: %d\n", iResult);
  23.         return 1;
  24.     }
  25.    
  26.     // 1. Deklarujemy addinfo ktore zawiera sockaddr i inicjalizuje te wartosci
  27.     struct addrinfo *result = NULL,
  28.         *ptr = NULL,
  29.         hints;
  30.     ZeroMemory(&hints, sizeof(hints));
  31.  
  32.     // Cos jak wczesniej funkcja socket, tam dawalem takie argumenty
  33.     // czyli to sa te sockaddr adresy gniazda
  34.     hints.ai_family = AF_UNSPEC; // !!! Tut daje AF_UNSPEC
  35.     hints.ai_socktype = SOCK_STREAM;
  36.     hints.ai_protocol = IPPROTO_TCP;
  37.  
  38. #define DEFAULT_PORT "27015"
  39.  
  40.     char* ip = "78.11.253.152";
  41.     //char* port = "55555";
  42.     // adres/nazwa strony, potem port http to domyslnie 80,
  43.     // potem wskaznik na strukture ktora pokazuje jakie typy gniazda klient wspiera
  44.     // wskzanik strukture ktora bedzie zawierac odp hosta.
  45.     iResult = getaddrinfo("78.11.253.152", DEFAULT_PORT, &hints, &result);
  46.     if (iResult != 0){
  47.         cout << "Getaddrinfo blad: " << iResult << endl;
  48.         WSACleanup();
  49.     }
  50.  
  51.     //3. Tworzymy socket ConnectSocket
  52.     SOCKET ConnectSocket = INVALID_SOCKET;
  53.  
  54.     /*Laczymy ptr na pierwszy adres zwrocony przez getaddrinfo*/
  55.     ptr = result;
  56.  
  57.     // tworzymy gniazdo do laczenia sie z serverem
  58.     ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
  59.     if (ConnectSocket == INVALID_SOCKET){
  60.         cout << "socket() poszlo zle\n";
  61.         freeaddrinfo(result);
  62.         WSACleanup();
  63.         return 1;
  64.     }
  65.  
  66.     // laczymy sie z serverem
  67.     iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  68.     while (iResult == SOCKET_ERROR){
  69.         closesocket(ConnectSocket);
  70.         ConnectSocket = INVALID_SOCKET;
  71.         cout << "Blad ustanowienia polaczenia\n";
  72.         ptr = ptr->ai_next;
  73.         iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  74.         // naprawde powinnismy sprobowac nastepny adres zwrocony przez getaddrinfo
  75.         // freeaddrinfo(result);
  76.         // WSACleanup();
  77.        
  78.     }
  79.  
  80.     // transmisja danych
  81. #define DEFAULT_BUFLEN 512
  82.  
  83.     int recvbuflen = DEFAULT_BUFLEN;
  84.     char *sendbuf = "this is a test";
  85.     char recvbuf[DEFAULT_BUFLEN];
  86.  
  87.     iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
  88.     if (iResult == SOCKET_ERROR){
  89.         cout << "mamy blad: " << WSAGetLastError() << endl;
  90.         closesocket(ConnectSocket);
  91.         WSACleanup();
  92.         return 1;
  93.     }
  94.  
  95.     cout << "Il wyslanych bajtow: " << iResult << endl;
  96.  
  97.     // Zamykanie polaczenia na wysylanie skoro nie mamy zamiar niczego wiecej wyslac
  98.     // klient wciaz moze uzywac ConnectSock do odbierania danych
  99.  
  100.     iResult = shutdown(ConnectSocket, SD_SEND);
  101.  
  102.     if (iResult == SOCKET_ERROR) {
  103.         printf("shutdown failed: %d\n", WSAGetLastError());
  104.         closesocket(ConnectSocket);
  105.         WSACleanup();
  106.         return 1;
  107.     }
  108.  
  109.     //odbieraj dane az server nie zamknie polaczenia
  110.     do {
  111.         iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
  112.         if (iResult > 0)
  113.             printf("Bytes received: %d\n", iResult);
  114.         else if (iResult == 0)
  115.             printf("Connection closed\n");
  116.         else
  117.             printf("recv failed: %d\n", WSAGetLastError());
  118.     } while (iResult > 0);
  119.  
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement