Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Pindrought
- Date: 11/13/2015
- This is the solution for the client that you should have at the end of tutorial 1.
- */
- #include "pch.h"
- #include <iostream>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string>
- #pragma comment(lib,"ws2_32.lib")
- #pragma warning(disable:4996)
- std::string IP_ADDR;
- int PORT = 8080;
- int main()
- {
- std::cout << "Podaj adres IP serwera: " << std::endl;
- std::cin >> IP_ADDR;
- std::cout << "Podaj port: " << std::endl;
- std::cin >> PORT;
- //Winsock Startup
- WSAData wsaData;
- WORD DllVersion = MAKEWORD(2, 1);
- if (WSAStartup(DllVersion, &wsaData) != 0) //If WSAStartup returns anything other than 0, then that means an error has occured in the WinSock Startup.
- {
- MessageBoxA(NULL, "Winsock startup failed", "Error", MB_OK | MB_ICONERROR);
- exit(1);
- }
- SOCKADDR_IN addr; //Address to be binded to our Connection socket
- int sizeofaddr = sizeof(addr); //Need sizeofaddr for the connect function
- addr.sin_addr.s_addr = inet_addr(IP_ADDR.c_str()); //Address = localhost (this pc)
- addr.sin_port = htons(PORT); //Port = 8080
- addr.sin_family = AF_INET; //IPv4 Socket
- SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL); //Set Connection socket
- if (connect(Connection, (SOCKADDR*)&addr, sizeofaddr) != 0) //If we are unable to connect...
- {
- MessageBoxA(NULL, "Failed to Connect", "Error", MB_OK | MB_ICONERROR);
- return 0; //Failed to Connect
- }
- std::cout << "Connected!" << std::endl;
- char buff[256];
- recv(Connection, buff, sizeof(buff), NULL); //Receive Message of the Day buffer into MOTD array
- std::cout << "Init Message form server:" << buff << std::endl;
- std::cout << "Podaj liczby L1 i L2\n";
- std::cin >> buff; //getting L1
- send(Connection, buff, sizeof(buff), NULL); //sending L1
- std::cin >> buff; //getting L2
- send(Connection, buff, sizeof(buff), NULL); //sending L2
- recv(Connection, buff, sizeof(buff), NULL); //waiting for result
- std::cout << "Wynik: " << buff << std::endl;
- Sleep(2000);
- closesocket(Connection);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement