add client and server

This commit is contained in:
Josh Holtrop 2012-04-22 18:25:56 -04:00
parent aaf6b5b709
commit 390eea5249
3 changed files with 61 additions and 2 deletions

View File

@ -7,9 +7,9 @@ CXX := $(PREFIX)-g++
LD := $(PREFIX)-ld LD := $(PREFIX)-ld
CPPFLAGS := -I$(SFML_DIR)/include CPPFLAGS := -I$(SFML_DIR)/include
CXXFLAGS := -mwindows CXXFLAGS := -mwindows
LDFLAGS := -L$(SFML_DIR)/lib -lsfml-system -lsfml-window -lsfml-graphics -lopengl32 -lglu32 -static-libstdc++ -static-libgcc -mwindows LDFLAGS := -L$(SFML_DIR)/lib -lsfml-system -lsfml-window -lsfml-graphics -lsfml-network -lopengl32 -lglu32 -static-libstdc++ -static-libgcc -mwindows
all: clock window events opengl graphics text dlls all: clock window events opengl graphics text client server dlls
%: %.cpp %: %.cpp
$(CXX) -o $@ $(CPPFLAGS) $(CXXFLAGS) $^ $(LDFLAGS) $(CXX) -o $@ $(CPPFLAGS) $(CXXFLAGS) $^ $(LDFLAGS)

29
client.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <iostream>
#include <SFML/network.hpp>
#define PORT 57219
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "Usage: client <server_name>" << std::endl;
return 1;
}
sf::IPAddress server(argv[1]);
if (!server.IsValid())
{
std::cerr << "Host not found" << std::endl;
return 1;
}
sf::SocketUDP socket;
char msg[] = "Hello from client!";
if (socket.Send(msg, sizeof(msg), server, PORT) != sf::Socket::Done)
{
std::cerr << "Error sending data" << std::endl;
return 1;
}
socket.Close();
return 0;
}

30
server.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <iostream>
#include <SFML/network.hpp>
#define PORT 57219
int main(int argc, char *argv[])
{
sf::SocketUDP socket;
char buffer[1000];
sf::IPAddress sender;
unsigned short port;
size_t received;
if (!socket.Bind(PORT))
{
std::cerr << "Error binding to port " << PORT << std::endl;
return 1;
}
if (socket.Receive(buffer, sizeof(buffer), received, sender, port)
!= sf::Socket::Done)
{
std::cerr << "Error receiving data" << std::endl;
return 1;
}
buffer[received] = '\0';
std::cout << "Received " << received << " bytes from " <<
sender << ':' << port << ": \"" << buffer << '"' << std::endl;
socket.Close();
return 0;
}