Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: How to implement autoplay

Author: Robert Hyatt

Date: 07:13:52 11/11/02

Go up one level in this thread


On November 11, 2002 at 06:11:27, Gerd Isenberg wrote:

>As a kind of illiterate in internet chess protocols, sockets and WinBoard, i am
>looking for some help to implement automatic playing for ICC in IsiChess without
>using WinBoard. Using MSC++ i guess to use a CSocket or CAsyncSocket class.
>
>Are there any descriptions or code samples how to do it?
>
>Thanks in advance,
>Gerd


In the unix world, it is trivial.

The steps are:

look up the hostname (chessclub.com) using gethostbyname().  Copy the
ip address into a sockaddr structure.

You need to fill in the port number, which for ICC is 5000.  (I will attach
a simple example at the end of this).


create a socket.

Do a connect using the above socket and sockaddr structure and you will get an
open full-duplex socket that you can use to read/write data from/to the chess
server.  All you have to figure out is how to handle the data you get, parse
the style-12 chessboard description which will give you the current position
and move (you can use either or both).

The obvious question is "why not use xboard/winboard?"  That gives you already-
working code, with a nice GUI to watch the games on, zippy to parse/accept
match requests automatically, etc...

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <math.h>
/*
  client.c
  To run the client, enter: client hostname message
*/
main(int argc, char *argv[]) {

  char buf[512];
  int client_socket;
  struct sockaddr_in localAddr_Active, Remote_Address;
  struct hostent *hp;
  float x,y;
/*
  1. Create a socket.  AF_INET = internet domain socket,
                       SOCK_STREAM = stream type socket,
                       0=no protocol required.
*/
  if ((client_socket=socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("cannot create socket.\n");
    exit(1);
  }
/*
  2. fill the server's address and port.
     First we fill the address structure with Os.
     Next we look up the IP address for this host name and
     fill it in.
*/
  bzero(&Remote_Address,sizeof(Remote_Address));
  Remote_Address.sin_family=AF_INET;
  hp=gethostbyname(argv[1]);
  memcpy((unsigned char *) &Remote_Address.sin_addr,
         (unsigned char *)hp->h_addr,
         hp->h_length);
  Remote_Address.sin_port=htons(4096);
/*
  3. Connect to the Remote server.
*/
  if (connect(client_socket, (struct sockaddr *) &Remote_Address,
sizeof(Remote_Address)) < 0) {
    printf ("cannot connect to host : %s at Port : 4096\n", argv[1]);
    exit(1);
  }

  if ((write(client_socket,argv[2],strlen(argv[2])+1)) < 0) {
    printf("CLIENT: Problem with client write\n");
    exit(1);
  }

  if (read (client_socket,buf, 512) < 0) {
    printf("CLIENT: Problem with client read\n");
    exit(1);
  }
  printf("CLIENT: message from server : %s \n", buf);

  close(client_socket);
  printf("CLIENT: exit \n");
  exit(0);
}


That is a simple working example that writes a message to the
remote end and reads a single response before exiting...



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.