Entries Tagged 'sockets' ↓

Sdl basic chess game networking – GameDev.net

Posted 01 July 2011 – 05:45 PM

hello, so i made a small 2d chess game everything works fine but network i relay don't know how to make it work.so at firs i thougt about sending the hole chessboard array over network but that whatnot such good idea due to size. so i tried sending the coordinates and the dest coordinates, but i kept getting wrong numbers. i hope you can give me a start at sending data over sockets.so my code is written for the sdl_net library im using the tcp socket because its more "safe" i heard. the code is used as both server and client the player can press start server or join game. so here's my code network.cpp#include "stdafx.h"#include "network.h" //————————————————————————————————-Network::Network(){ initialized = false;}Network::~Network(){ initialized = false;}//————————————————————————————————-bool Network::init(char *host){ if(host == NULL) { server = true; } else { server = false; } if (SDLNet_Init() < 0) { fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError()); return false; } /* Resolving the host using NULL make network interface to listen */ if (SDLNet_ResolveHost(&ip, host, Port) < 0) { fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError()); return false; } /* Open a connection with the IP provided (listen on the host's port) */ if (!(sd = SDLNet_TCP_Open(&ip))) { fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError()); return false; } sockset = SDLNet_AllocSocketSet(4); SDLNet_TCP_AddSocket(sockset,sd); initialized = true; return true;}//————————————————————————————————-void Network::exit(){ SDLNet_TCP_Close(sd); if (server) { SDLNet_TCP_Close(csd); } SDLNet_FreeSocketSet(sockset); SDLNet_Quit(); initialized = false;}//————————————————————————————————-bool Network::waitforconection(){ if ((csd = SDLNet_TCP_Accept(sd))) { /* now we can communicate with the client using csd socket * sd will remain opened waiting other connections */ /* Get the remote address */ if ((remoteIP = SDLNet_TCP_GetPeerAddress(csd))) { /* Print the address, converting in the host format */ printf("Host connected: %x %d\n", SDLNet_Read32(&remoteIP->host), SDLNet_Read16(&remoteIP->port)); return true; } else fprintf(stderr, "SDLNet_TCP_GetPeerAddress: %s\n", SDLNet_GetError()); } return false;}//————————————————————————————————-bool Network::send(const char * buf){ len = strlen(buf) + 1; if (server) { if (SDLNet_TCP_Send(csd, (void *)buf, len) < len) { fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError()); return false; } }else { if (SDLNet_TCP_Send(sd, (void *)buffer, len) < len) { fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError()); return false; } } return true;}//————————————————————————————————-bool Network::getactivety(){ /* Wait for events */ SDLNet_CheckSockets(sockset, 0); /* Check for new connections */ if (server) { if ( SDLNet_SocketReady(csd)) if (SDLNet_TCP_Recv(csd, buffer, 512) > 0) return true; }else { if ( SDLNet_SocketReady(sd)) if (SDLNet_TCP_Recv(sd, buffer, 512) > 0) return true; } return false;}//————————————————————————————————-bool Network::waitforactivety(){ /* Wait for events */ SDLNet_CheckSockets(sockset, ~0); /* Check for new connections */ if (server) { if ( SDLNet_SocketReady(csd)) if (SDLNet_TCP_Recv(csd, buffer, 512) > 0) return true; }else { if ( SDLNet_SocketReady(sd)) if (SDLNet_TCP_Recv(sd, buffer, 512) > 0) return true; } return false;}//———————————————————————————————— network.h#ifndef network_h__#define network_h__#include <sdl.h>#include <SDL_net.h> #ifndef Max_iplength #define Max_iplength 15#endif #ifndef Port #define Port 2000#endif class Network{public: Network(); virtual ~Network(); bool init(char *host = NULL); void exit(); bool waitforconection(); bool send(const char *); bool getactivety(); bool waitforactivety(); bool initialized; bool server; char buffer[512];private: TCPsocket sd, csd; /* Socket descriptor, Client socket descriptor */ IPaddress ip, *remoteIP; SDLNet_SocketSet sockset; int len;}; #endif // network_h__ game_network.cppvoid CGame::Network_Events(){ if (network->getactivety()) { if (strcmp(network->buffer, "move") == 0) network_recivemove(); else if (strcmp(network->buffer, "next player") == 0) next_player(); else if (strcmp(network->buffer, "quit") == 0) { quit();screens->signtext = "opponent quited";screens->DisplaySign(); } }} void CGame::network_sendmove(int x, int y , int desty,int destx){ char c[10]; network->send("sending move"); itoa(x,c,10); network->send(c); itoa(y,c,10); network->send(c); itoa(destx,c,10); network->send(c); itoa(desty,c,10); network->send(c);} void CGame::network_recivemove(){ network->waitforactivety(); int x,y,dx,dy; network->waitforactivety(); x =(int) network->buffer; network->waitforactivety(); y =(int) network->buffer; network->waitforactivety(); dx =(int) network->buffer; network->waitforactivety(); dy =(int) network->buffer; pices->Move(x,y,dx,dy,Chessboard); } thanks in advice /alex oh if you find spelling mistakes please don't point em out.

Anyone else experience quirks with Berkeley Socket API?

Hello, not really sure if this will lead to something, but I just want to have a small discussion about the Berkley Sockets API. recently i’ve been working on TCP Client/Server architectures, and while these lovely Sockets do the things they’re supposed to do, I often find myself struggling with the API or inner workings of the Sockets. An example is when I establish a connection on a local port to a remote port. when the connection is established, there’s a point in time where I need to either re-establish the connection or connect to a different system from the same port. More often then not I have to wait a long time for the socket component to be fully destroyed by the system before I can rebind the socket on the port. Event when the application is terminated. I’m aware this is rather a Kernel/OS issue and not really the Berkley Socket API fault, but there’s more of these little things that strike me when working with it. Anyone else have these quircks with the API?

Socket Wrenches

May 2, 2011

  • Precision machined S-2 Alloy steel bits
  • Drop Forged, Chrome Vanadium Steel sockets
  • Heat Treated, chrome plated for durability
  • Knurled finish on sockets for easy turning.
  • Use for Tamper-Proof Star Fasteners in cars, trucks, appliances, etc.

Product Description

Professional Quality HEAVY DUTY tool set!

These bits will allow you to open those “tamper proof” and “security” Torx/Star fasteners.

Precision machined S-2 Alloy steel bits Drop Forged, Chrome Vanadium Steel sockets. Heat Treated, chrome plated for durability. Knurled finish on sockets for easy turning. use for Tamper-Proof Star Fasteners in cars, trucks, appliances, etc.

6 pc 1/4″ Drive start Bit Sockets… Best Offer Today! >>

Read more >>