I'm developing this game with sockets, server-client connection!!
And the problem is I can't put the game work, so many errors along the program, can anyone help me?!
It´s urgent....

There is no urgent help here. All questions have the same priority: none.

If you want help you need to post your code, describe in detail the problems you are having and post any error messages you are getting.

Then if someone wants to help you they will.

Comments
"There is no urgent help here. All questions have the same priority: none." LOL

I'm developing 2 programs, with sockets: tic tac toe server and tic tac toe client. The first part was easy, but when the connection starts, so many problems. I put right here the 2 programs, then someone can make this much better!! Please?!

Client:

#! /usr/bin/perl -w

# Tic tac toe

use strict;
use Socket;

my $host = shift || 'localhost';
my $port = shift || 7890;
my $proto = getprotobyname('tcp');

my $line;
my $end = 0;

my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);

socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";

my $old_fh = select(SOCKET);
$| = 1;
select($old_fh);

connect(SOCKET, $paddr) or die "connect: $!";
print "Client connected \n";
print "Data received from server: \n";
print ("Do you wish to play tic tac toe? Please answer Y or N\n");

$Answer = <STDIN>;

chomp($Answer);

if ( $Answer eq "N" || $Answer eq "n") {
print ("Ok, goodbye!\n");
}
if ( $Answer ne "N" && $Answer ne "n" && $Answer ne "Y" && $Answer ne "y") {

print ("Give me a yes or no answer next time, please. Goodbye!\n");

}

if ( $Answer eq "Y" || $Answer eq "y") {

@Board = (" ", " ", " ", " ", " ", " ", " ", " ", " ");
@BestMoves = (5, 1, 3, 7, 9);

print ("You will be X.\n");
print ("You will be first!\n");

$Winner = 0;

    while ( $Winner != 1 )
        {
        $Answer = GetserverMove();
        $Position = $Answer - 1;

            $Board[$Position] = "X";

        DisplayBoard();

        $Winner = CheckForWinner();

        if ($Winner >=1)

        $Answer = GetclientMove();

        $Position = $Answer - 1;

        $Board[$Position] = "O";

                DisplayBoard();

        $Winner = CheckForWinner();

        if ($Winner >= 1)

    }
    if ($Winner ==1) {print ("X is a winner!!! \a\a\a\n")};

    if ($Winner ==2) {print ("O is a winner!!! \n")};

}

}

sub DisplayBoard
{
    print ("------------TOP----------------\n");

    print ("|$Board[0]|$Board[1]|$Board[2]|\n");

    print ("|$Board[3]|$Board[4]|$Board[5]|\n");

    print ("|$Board[6]|$Board[7]|$Board[8]|\n");

    print ("----------BOTTOM---------------\n");


}

sub GetserverMove
{
    $MyMove = 0;

    foreach $i (@BestMoves) {

if ( $Board[$i-1] eq " " ) {

$MyMove = $i;


}

}

if ($MyMove != 0) {

return $MyMove};

for ($i = 1; $i<=9 ; $i++) {

if ( $Board[$i-1] eq " " ) {

$MyMove = $i;

}

}

return $MyMove;

}

sub GetclientMove {


$OkMove = 0;



while ($OkMove == 0) {


print ("What is your next move? Please enter 1-9\n");

$i = <STDIN>;

chomp($i);



if ($i > 0 && $i < 10) {



if ( $Board[$i-1] ne " ") {

$OkMove = 0;

print ("Bad Move! Give me a better one!\n");

DisplayBoard();

} else {



$OkMove = 1;

$MyMove = $i;

}

} else {



$OkMove = 0;

print ("Number must be 1 through 9!\n");

}

}

return $MyMove;

}

sub CheckForWinner {



if ($Board[0] eq "X" && $Board[1] eq "X" && $Board[2] eq "X" ){return 1};

if ($Board[3] eq "X" && $Board[4] eq "X" && $Board[5] eq "X" ){return 1};

if ($Board[6] eq "X" && $Board[7] eq "X" && $Board[8] eq "X" ){return 1};

if ($Board[0] eq "X" && $Board[3] eq "X" && $Board[6] eq "X" ){return 1};

if ($Board[1] eq "X" && $Board[4] eq "X" && $Board[7] eq "X" ){return 1};

if ($Board[2] eq "X" && $Board[5] eq "X" && $Board[8] eq "X" ){return 1};

if ($Board[0] eq "X" && $Board[4] eq "X" && $Board[8] eq "X" ){return 1};

if ($Board[2] eq "X" && $Board[4] eq "X" && $Board[6] eq "X" ){return 1};



if ($Board[0] eq "O" && $Board[1] eq "O" && $Board[2] eq "O" ){return 2};

if ($Board[3] eq "O" && $Board[4] eq "O" && $Board[5] eq "O" ){return 2};

if ($Board[6] eq "O" && $Board[7] eq "O" && $Board[8] eq "O" ){return 2};

if ($Board[0] eq "O" && $Board[3] eq "O" && $Board[6] eq "O" ){return 2};

if ($Board[1] eq "O" && $Board[4] eq "O" && $Board[7] eq "O" ){return 2};

if ($Board[2] eq "O" && $Board[5] eq "O" && $Board[8] eq "O" ){return 2};

if ($Board[0] eq "O" && $Board[4] eq "O" && $Board[8] eq "O" ){return 2};

if ($Board[2] eq "O" && $Board[4] eq "O" && $Board[6] eq "O" ){return 2};

}

close SOCKET or die "close: $!";
print "\n --Client has disconnected-- \n";

Server:

#! /usr/bin/perl -w


# Tic tac toe

use strict;
use Socket;

my $port = shift || 7890;
my $proto = getprotobyname('tcp');

socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";

my $paddr = sockaddr_in($port, INADDR_ANY);

bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port \n";

my $client_addr;


while ($client_addr = accept(CLIENT, SERVER))
{
        my $old_fh = select(CLIENT);
    $| = 1;
    select($old_fh);

print "New connection established.\n";
print ("Do you wish to play first? Answer Y or N\n");

$Answer = <STDIN>;
chomp($Answer);

if ( $Answer eq "N" || $Answer eq "n")
{
    print ("Ok, goodbye!\n");
}

if ( $Answer "Y" || $Answer eq "y")
{
    @Board = (" ", " ", " ", " ", " ", " ", " ", " ", " ");
        @BestMoves = (5, 1, 3, 7, 9);

    print ("You will be X.\n");
    print ("You will be first!\n");

    $Winner = 0;

    while ( $Winner != 1 )
        {
        $Answer = GetclientMove();
        $Position = $Answer - 1;

            $Board[$Position] = "X";

        DisplayBoard();

        $Winner = CheckForWinner();

        if ($Winner >=1)

        $Answer = GetserverMove();

        $Position = $Answer - 1;

        $Board[$Position] = "O";

                DisplayBoard();

        $Winner = CheckForWinner();

        if ($Winner >= 1)

    }
    if ($Winner ==1) {print ("X is a winner!!! \a\a\a\n")};

    if ($Winner ==2) {print ("O is a winner!!! \n")};

}

}

sub DisplayBoard
{
    print ("------------TOP----------------\n");

    print ("|$Board[0]|$Board[1]|$Board[2]|\n");

    print ("|$Board[3]|$Board[4]|$Board[5]|\n");

    print ("|$Board[6]|$Board[7]|$Board[8]|\n");

    print ("----------BOTTOM---------------\n");


}

sub GetclientMove
{
    $MyMove = 0;

    foreach $i (@BestMoves) {

if ( $Board[$i-1] eq " " ) {

$MyMove = $i;


}

}

if ($MyMove != 0) {

return $MyMove};

for ($i = 1; $i<=9 ; $i++) {

if ( $Board[$i-1] eq " " ) {

$MyMove = $i;

}

}

return $MyMove;

}

sub GetserverMove {


$OkMove = 0;



while ($OkMove == 0) {


print ("What is your next move? Please enter 1-9\n");

$i = <STDIN>;

chomp($i);



if ($i > 0 && $i < 10) {



if ( $Board[$i-1] ne " ") {

$OkMove = 0;

print ("Bad Move! Give me a better one!\n");

DisplayBoard();

} else {



$OkMove = 1;

$MyMove = $i;

}

} else {



$OkMove = 0;

print ("Number must be 1 through 9!\n");

}

}

return $MyMove;

}

sub CheckForWinner {



if ($Board[0] eq "X" && $Board[1] eq "X" && $Board[2] eq "X" ){return 1};

if ($Board[3] eq "X" && $Board[4] eq "X" && $Board[5] eq "X" ){return 1};

if ($Board[6] eq "X" && $Board[7] eq "X" && $Board[8] eq "X" ){return 1};

if ($Board[0] eq "X" && $Board[3] eq "X" && $Board[6] eq "X" ){return 1};

if ($Board[1] eq "X" && $Board[4] eq "X" && $Board[7] eq "X" ){return 1};

if ($Board[2] eq "X" && $Board[5] eq "X" && $Board[8] eq "X" ){return 1};

if ($Board[0] eq "X" && $Board[4] eq "X" && $Board[8] eq "X" ){return 1};

if ($Board[2] eq "X" && $Board[4] eq "X" && $Board[6] eq "X" ){return 1};



if ($Board[0] eq "O" && $Board[1] eq "O" && $Board[2] eq "O" ){return 2};

if ($Board[3] eq "O" && $Board[4] eq "O" && $Board[5] eq "O" ){return 2};

if ($Board[6] eq "O" && $Board[7] eq "O" && $Board[8] eq "O" ){return 2};

if ($Board[0] eq "O" && $Board[3] eq "O" && $Board[6] eq "O" ){return 2};

if ($Board[1] eq "O" && $Board[4] eq "O" && $Board[7] eq "O" ){return 2};

if ($Board[2] eq "O" && $Board[5] eq "O" && $Board[8] eq "O" ){return 2};

if ($Board[0] eq "O" && $Board[4] eq "O" && $Board[8] eq "O" ){return 2};

if ($Board[2] eq "O" && $Board[4] eq "O" && $Board[6] eq "O" ){return 2};

}


close CLIENT;
print "--END--";
exit;

Edited 3 Years Ago by mike_2000_17: Fixed formatting

the game must do this:
-the client connects;
-then the server go and wait for the call;
-the client waits for the server's moves,
-the program prints the game (board, positions);
-server plays, while the client waits for his time;
-the game prints the board, positions, moves;
-then the client plays while server waits for his time;

this will go to this cycle until someone decides to finish the game!!

so, anyone has the answer for my problem?!

...
And the problem is I can't put the game work, so many errors along the program, can anyone help me?!
It´s urgent....

you have posted your code, what errors you are getting? post that too... it would be easy to tackle problem.

my bad, but can u help me?!

I will try and help you with specific problems but so far you have not provided that information.

This article has been dead for over six months. Start a new discussion instead.