Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //server
- #include <iostream>
- #include <string>
- #include <winsock2.h>
- #pragma comment(lib, "ws2_32.lib")
- using namespace std;
- int main()
- {
- WSADATA WSAData;
- SOCKET server, client;
- SOCKADDR_IN serverAddr, clientAddr;
- WSAStartup(MAKEWORD(2, 0), &WSAData);
- server = socket(AF_INET, SOCK_STREAM, 0);
- serverAddr.sin_addr.s_addr = INADDR_ANY;
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_port = htons(5555);
- bind(server, (SOCKADDR *)&serverAddr, sizeof(serverAddr));
- listen(server, 0);
- cout << "Listening for incoming connections..." << endl;
- char buffer[1024];
- int clientAddrSize = sizeof(clientAddr);
- string responce = "";
- if ((client = accept(server, (SOCKADDR *)&clientAddr, &clientAddrSize)) != INVALID_SOCKET)
- {
- cout << "Client joined" << endl;
- while (responce != "quit")
- {
- if (recv(client, buffer, sizeof(buffer), 0) > 0)
- {
- cout << buffer << endl;
- memset(buffer, 0, sizeof(buffer));
- }
- else
- {
- responce = "quit";
- }
- //getline(cin, responce);
- //send(client, responce.c_str(), sizeof(responce), 0);
- }
- closesocket(client);
- cout << "Client disconnected." << endl;
- }
- system("pause");
- }
- //client
- #include <SDL.h>
- #include <iostream>
- #include <algorithm>
- #include <winsock2.h>
- #include <Ws2tcpip.h>
- #pragma comment(lib, "ws2_32.lib")
- using namespace std;
- int main(int arc, char ** argv)
- {
- SDL_Window* pWindow = NULL;
- SDL_Event event;
- SDL_Renderer *pRenderer = NULL;
- SDL_Texture* pTexture = NULL;
- SDL_Surface* pSprite = NULL;
- SDL_Rect dest;
- string talk = "";
- char responce[1024];
- WSADATA WSAData;
- SOCKET server;
- SOCKADDR_IN addr;
- WSAStartup(MAKEWORD(2, 0), &WSAData);
- server = socket(AF_INET, SOCK_STREAM, 0);
- InetPton(AF_INET, "192.168.1.16", &addr.sin_addr.s_addr);
- addr.sin_family = AF_INET;
- addr.sin_port = htons(5555);
- connect(server, (SOCKADDR *)&addr, sizeof(addr));
- //if (connect(server, (SOCKADDR *)&addr, sizeof(addr))>0)
- {
- bool cont = true;
- int CurrentTime = 0, seconds = 0, PassedTime = 0;
- if (SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl;
- }
- else
- {
- pWindow = SDL_CreateWindow("SDL2 Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
- }
- pRenderer = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED);
- if (pRenderer)
- {
- pSprite = SDL_LoadBMP("data/deck/background.bmp");
- if (pSprite)
- {
- pTexture = SDL_CreateTextureFromSurface(pRenderer, pSprite);
- if (pTexture)
- {
- dest = { 0, 0, 800, 600 };
- SDL_RenderCopy(pRenderer, pTexture, NULL, &dest);
- SDL_RenderPresent(pRenderer);
- }
- else
- {
- cout << "Couldn't display image : " << SDL_GetError() << endl;
- }
- }
- else
- {
- cout << "Sprite loading error : " << SDL_GetError() << endl;
- }
- }
- else
- {
- cout << "Couldn't create renderer : " << SDL_GetError() << endl;
- }
- while (cont != 0)
- {
- while (SDL_PollEvent(&event))
- {
- CurrentTime = SDL_GetTicks();
- if (CurrentTime - PassedTime > 200)
- {
- switch (event.type)
- {
- case SDL_KEYDOWN:
- if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE)
- {
- cout << "Quit" << endl;
- cont = 0;
- }
- else if (event.key.keysym.scancode == SDL_SCANCODE_1)
- {
- talk = "1";
- cout << "1" << endl;
- }
- else if (event.key.keysym.scancode == SDL_SCANCODE_2)
- {
- talk = "2";
- cout << "2" << endl;
- }
- break;
- case SDL_QUIT:
- cont = false;
- break;
- default:
- break;
- }
- PassedTime = CurrentTime;
- }
- }
- send(server, talk.c_str(), sizeof(talk), 0);
- SDL_RenderClear(pRenderer);
- SDL_RenderCopy(pRenderer, pTexture, NULL, &dest);
- SDL_RenderPresent(pRenderer);
- CurrentTime = SDL_GetTicks();
- }
- }
- //delse
- {
- cout << "Counldn't connect to server." << endl;
- system("pause");
- }
- SDL_DestroyTexture(pTexture);
- SDL_DestroyRenderer(pRenderer);
- SDL_FreeSurface(pSprite);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement