Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //**********************************************
- //* client_main.cpp
- //**********************************************
- #include <iostream>
- #include "Client.h"
- #include <string>
- //CLIENrt
- using namespace std;
- int main()
- {
- Client c("localhost");
- char bufor[DEFAULT_BUFLEN];
- string stuff = "";
- do {
- cout << "send stuff: ";
- cin >> stuff;
- memcpy(bufor, stuff.c_str(), stuff.size() + 1); // overflow
- c.sen(bufor);
- memcpy(bufor, "deadbeefdeadbeef", strlen("deadbeefdeadbeef") + 1);
- int btz = c.rec(bufor);
- cout << "\n[receiving " << btz << "bytes.] : " << bufor << endl;
- } while (stuff != "Q");
- //cout << "Listening for client ... " <<endl;
- //cout << "Ret Code: " << s.acc() << endl;
- cout << "KONIETZ";
- system("pause");
- return 0;
- }
- //**********************************************
- //* Client.h
- //**********************************************
- #pragma once
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <iostream>
- #pragma comment (lib, "Ws2_32.lib")
- #pragma comment (lib, "Mswsock.lib")
- #pragma comment (lib, "AdvApi32.lib")
- #define DEFAULT_BUFLEN 512
- #define DEFAULT_PORT "6666"
- using namespace std;
- class Client
- {
- protected:
- WSADATA wsaData;
- SOCKET ConnectSocket;
- int iResult;
- public:
- Client(const char * addr);
- ~Client(void);
- int sen(char * sendbuf);
- int rec(char * recvbuf);
- };
- //**********************************************
- //* Client.cpp
- //**********************************************
- #include "Client.h"
- Client::Client(const char * addr)
- {
- iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
- if (iResult != 0) {
- cout << "wsastartup failed" << endl;
- }
- struct addrinfo *result = NULL,
- *ptr = NULL,
- hints;
- ZeroMemory( &hints, sizeof(hints) );
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- iResult = getaddrinfo(addr, DEFAULT_PORT, &hints, &result);
- if ( iResult != 0 ) {
- WSACleanup();
- cout << "getaddrinfo failed" << endl;
- }
- // Attempt to connect to an address until one succeeds
- for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
- // Create a SOCKET for connecting to server
- ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
- ptr->ai_protocol);
- if (ConnectSocket == INVALID_SOCKET) {
- WSACleanup();
- cout << "connect socket create failed" << endl;
- }
- // Connect to server.
- iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- closesocket(ConnectSocket);
- ConnectSocket = INVALID_SOCKET;
- continue;
- }
- break;
- }
- freeaddrinfo(result);
- if (ConnectSocket == INVALID_SOCKET) {
- WSACleanup();
- cout << "connect failed" << endl;
- }
- }
- Client::~Client(void)
- {
- // shutdown the connection since no more data will be sent
- iResult = shutdown(ConnectSocket, SD_SEND);
- if (iResult == SOCKET_ERROR) {
- cout << "shutdown failed with error: "<< WSAGetLastError()<< endl;
- closesocket(ConnectSocket);
- WSACleanup();
- }
- }
- int Client::sen(char * sendbuf)
- {
- // Send an initial buffer
- iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf) + 1, 0 );
- if (iResult == SOCKET_ERROR) {
- cout << "send failed with error: " <<WSAGetLastError() << endl;
- closesocket(ConnectSocket);
- WSACleanup();
- return -1;
- }
- cout << "Bytes Sent: " << iResult;
- return 0;
- }
- int Client::rec(char * recvbuf)
- {
- return recv(ConnectSocket, recvbuf, DEFAULT_BUFLEN, 0);
- return 0;
- }
- //**********************************************
- //* server_main.cpp
- //**********************************************
- #include <iostream>
- #include "Server.h"
- //SERVER
- using namespace std;
- int main()
- {
- Server s;
- s.run();
- system("pause");
- return 0;
- }
- //**********************************************
- //* Server.h
- //**********************************************
- #pragma once
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <iostream>
- #pragma comment (lib, "Ws2_32.lib")
- #pragma comment (lib, "Mswsock.lib")
- #pragma comment (lib, "AdvApi32.lib")
- #define DEFAULT_BUFLEN 512
- #define DEFAULT_PORT "6666"
- using namespace std;
- class Server
- {
- protected:
- WSADATA wsaData;
- int iResult;
- SOCKET ListenSocket;
- SOCKET ClientSocket;
- struct addrinfo *result;
- public:
- Server(void);
- ~Server(void);
- int acc(void); // accept
- int echo();
- void shtd(); // shutdown
- void run();
- };
- //**********************************************
- //* Server.cpp
- //**********************************************
- #include "Server.h"
- Server::Server(void)
- {
- ListenSocket = INVALID_SOCKET;
- ClientSocket = INVALID_SOCKET;
- result = NULL;
- struct addrinfo hints;
- // Initialize Winsock
- iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
- if (iResult != 0) {
- cout << "wsa startup failed" <<endl;
- }
- ZeroMemory(&hints, sizeof(hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_flags = AI_PASSIVE;
- // Resolve the server address and port
- iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
- if ( iResult != 0 ) {
- WSACleanup();
- cout << "getaddr info failed" <<endl;
- }
- // Create a SOCKET for connecting to server
- ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
- if (ListenSocket == INVALID_SOCKET) {
- freeaddrinfo(result);
- WSACleanup();
- cout << "listen socket create failed" <<endl;
- }
- // Setup the TCP listening socket
- iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- freeaddrinfo(result);
- closesocket(ListenSocket);
- WSACleanup();
- cout << "bind failed" <<endl;
- }
- freeaddrinfo(result);
- }
- Server::~Server(void)
- {
- closesocket(ListenSocket);
- closesocket(ClientSocket);
- WSACleanup();
- }
- int Server::acc(void)
- {
- iResult = listen(ListenSocket, SOMAXCONN);
- if (iResult == SOCKET_ERROR) {
- closesocket(ListenSocket);
- WSACleanup();
- return -5;
- }
- // Accept a client socket
- ClientSocket = accept(ListenSocket, NULL, NULL);
- if (ClientSocket == INVALID_SOCKET) {
- closesocket(ListenSocket);
- WSACleanup();
- return -6;
- }
- return 0;
- }
- void Server::run()
- {
- int echoRet = 1;
- do {
- cout << "Listening for client ... " <<endl;
- cout << "Client connect ret code: " << acc() << endl;
- do {
- echoRet = echo();
- cout << "Echoing " << echoRet << "bytes"<<endl;
- } while(echoRet > 0);
- } while(echoRet >= 0);
- cout << "shutting down";
- shtd();
- }
- int Server::echo() // 0 - rozlaczenie klienta -1 - shutdown >0 - ilosc przeslanych bajtow
- {
- int iSendResult;
- char recvbuf[DEFAULT_BUFLEN];
- iResult = recv(ClientSocket, recvbuf, DEFAULT_BUFLEN, 0);
- if (iResult > 0) {
- //cout << "Bytes received: %d\n"<<iResult;
- if (strcmp(recvbuf, "shutdown") == 0) {
- return -1;
- }
- // Echo the buffer back to the sender
- iSendResult = send( ClientSocket, recvbuf, iResult, 0 );
- if (iSendResult == SOCKET_ERROR) {
- return 0;
- }
- return iResult;
- }
- else if (iResult == 0)
- return 0;
- else {
- return 0;
- }
- }
- void Server::shtd()
- {
- iResult = shutdown(ClientSocket, SD_SEND);
- if (iResult == SOCKET_ERROR) {
- cout << "shutdown failed with error: " << WSAGetLastError() << endl;
- closesocket(ClientSocket);
- WSACleanup();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement