Using socket programming in games
Socket programming is one of the earliest mechanisms for transferring data between end-to-end connections. Even now, if you are comfortable writing socket programming, it is a much better option for a relatively small game than using third party solutions, as they add a lot of extra space.
Getting ready
For this recipe, you will need a Windows machine and an installed version of Visual Studio.
How to do it…
In this recipe, we will find out how easy it is to write sockets:
struct sockaddr_in
{
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
int PASCAL connect(SOCKET,const struct sockaddr*,int);
target.sin_family = AF_INET; // address family Internet
target.sin_port = htons (PortNo); //Port to connect on
target.sin_addr.s_addr = inet_addr (IPAddress); //Target IP
s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket
if (s == INVALID_SOCKET)
{
...