Skip to content

Instantly share code, notes, and snippets.

/Client.cpp Secret

Created October 29, 2015 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/508a862ff47f41e1805f to your computer and use it in GitHub Desktop.
Save anonymous/508a862ff47f41e1805f to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <math.h>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
using namespace std;
class cTcp{
public:
void connectTo(string IP, int PORT, int disconnectTime){
sf::Socket::Status status = socket.connect(IP, PORT, sf::seconds(disconnectTime));
if (status != sf::Socket::Done)
{
cout << "Connection failed!" << endl;
}
else{
cout << "Connected!" << endl;
}
}
void mySend(sf::Packet packet){
sf::Socket::Status status = socket.send(packet);
while (status == sf::Socket::Partial){
status = socket.send(packet);
}
}
sf::TcpSocket socket;
private:
}myTcp;
int main(int argc, char** argv)
{
sf::ContextSettings Settings;
Settings.depthBits = 24; // Request a 24 bits depth buffer
Settings.stencilBits = 8; // Request a 8 bits stencil buffer
Settings.antialiasingLevel = 0; // Request 2 levels of antialiasing
// Set Screen Size
sf::Vector2f screenSize;
screenSize.x = 1000;
screenSize.y = 600;
//Create the Window
sf::RenderWindow App(sf::VideoMode(screenSize.x, screenSize.y, 32), "Network Client", sf::Style::Titlebar, Settings);
//Frame Limit
App.setFramerateLimit(0);
//Vertical sync
App.setVerticalSyncEnabled(false);
myTcp.connectTo("192.168.1.6", 27000, 6);
myTcp.socket.setBlocking(false);
string message = "";
while (App.isOpen())
{
cout << endl << "enter message: ";
cin >> message;
//Send message to server
sf::Packet packet;
packet.clear();
packet << message;
myTcp.mySend(packet);
//Update positions and draw
App.clear();
App.display();
}
return 0;
}//End of main
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <sstream>
#include <vector>
#include <math.h>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
using namespace std;
int main(int argc, char** argv)
{
sf::TcpListener listener;
listener.setBlocking(false);
// bind the listener to a port
if (listener.listen(27000) != sf::Socket::Done){
cout << "error listening on that port" << endl;
}
cout << "Waiting for connections..." << endl << endl;
string message = "";
while (1){
sf::TcpSocket *client = new sf::TcpSocket;
client->setBlocking(false);
// accept a new connection
if (listener.accept(*client) == sf::Socket::Done){
// A new client just connected!
cout << client->getRemoteAddress() << " connected" << endl;
}
else{
delete client;
}
//Receive packets
sf::Packet packet;
packet.clear();
sf::Socket::Status status = client->receive(packet);
//Receive again if not completed
while (status == sf::Socket::Partial){
status = client->receive(packet);
}
if (status == sf::Socket::Done){
packet >> message;
cout << message << endl;
}
//Check if we disconnected and then delete player
if (status == sf::Socket::Disconnected){
cout << client->getRemoteAddress() << " disconnected" << endl;
delete client;
}
}
return 0;
}//End of main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment