Witam. Mam pewien problem przy tradycyjnej grze w okręty.
Stworzyłem klienta i server. Komunikują się za pomocą socketów, jednak mam problem, gry gracz probuje wprowadzic swoje statki. Stwrzylem do tego dwie funkcje, jedna z nich zadaje statek pionowo druga poziomo do tablicy 10x10. Jednak przy wysylaniu tego do serwera cos sie dzieje nie tak jak powinno.
Koda servera:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
void drukujPlansze(int v[][ 10 ] )
{
/* numeracja kolumn */
printf("\t X\n\n");
printf(" 0 1 2 3 4 5 6 7 8 9\n\n");
int i; /* wiersze */
int j; /* kolumny */
/* numeracja werszy */
for ( i = 0; i <= 9; i++ )
{
if( i < 4)
{
printf(" %d ", i);
}
if( i > 4 )
{
printf(" %d ", i);
}
if(i == 4)
{
printf(" 4 ");
}
/* wypisywanie * * * wartosci * * * kolumn * */
for ( j = 0; j <= 9; j++ )
{
printf( "%d ", v[ i ][ j ] );
}
printf( "\n" ); /* nowa linia */
if( i == 3 )
{
printf("Y");
}
} /* koniec wypisujacej petli for */
} /* koniec funkcji wypisujacej plansze */
int main(int argc, char *argv[])
{
int fd;
int port;
int temp1;
int temp2;
if (argc<2)
{
port = 9876;
}
else if (argc==2)
{
port = atoi(argv[1]);
}
else
{
printf("ERROR! zla ilosc parametrow");
exit(-1);
}
fd = socket(AF_INET, SOCK_STREAM,0);
if (fd < 0)
{
printf("ERROR! przy tworzeniu socket\n");
exit (1);
}
struct sockaddr_in serwer;
serwer.sin_family = AF_INET;
serwer.sin_port = htons(port);
serwer.sin_addr.s_addr= INADDR_ANY;
if (bind(fd, (struct sockaddr *)&serwer, sizeof(serwer)) < 0)
{
printf("ERROR! przy bind\n");
exit(1);
}
if (listen(fd,5) < 0)
{
printf("ERROR! przy listen");
exit(1);
}
struct sockaddr_in usr1;
socklen_t usr1len=sizeof(struct sockaddr_in);
struct sockaddr_in usr2;
socklen_t usr2len=sizeof(struct sockaddr_in);
while(1)
{
int user1;
int user2;
user1=accept(fd, (struct sockaddr *) &usr1, &usr1len);
if (user1 < 0)
{
printf("ERROR! bald accept user1\n");
exit(1);
}
user2=accept(fd, (struct sockaddr *) &usr2, &usr2len);
if (user2 < 0)
{
printf("ERROR! bald accept user2\n");
exit(1);
}
int pid2=fork();
char buf[512];
if (pid2==0)
{
int user1map[ 10 ][ 10 ] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
int user2map[ 10 ][ 10 ] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
// wczytywanie nickow dwoch graczy
temp1 = read(user1, buf, 1000);
printf("Nick gracza 1 %s\n", buf);
bzero(buf, 512);
temp1 = write(user1, buf, 1000);
temp2 = read(user2, buf, 1000);
printf("Nick gracza 2 %s\n", buf);
bzero(buf, 512);
temp2 = write(user2, buf, 1000);
// koniec wczytywania nickow graczy
//wczytywanie pozycji statkow obu graczy
temp1 = read(user1, user1map, 1000);
printf(" ========================\n");
printf(" == PLANSZA U1 ==\n");
printf(" ========================\n");
drukujPlansze(user1map);
printf("\n");
temp1 = write(user1, user1map, 1000);
temp2 = read(user2, user2map, 1000);
printf(" ========================\n");
printf(" == PLANSZA U2 ==\n");
printf(" ========================\n");
drukujPlansze(user2map);
printf("\n");
temp2 = write(user2, user2map, 1000);
// koniec wczytywania pozycji statkow graczy
//doddatkowa petla do wiadomosci, ktore maja byc przerobione na atakowanie
while(1)
{
temp1 = read(user1, buf, 1000);
printf("Wiadomosc od 1 %s\n", buf);
bzero(buf, 512);
temp1 = write(user1, buf, 1000);
temp2 = read(user2, buf, 1000);
printf("Wiadomosc od 2 %s\n", buf);
bzero(buf, 512);
temp2 = write(user2, buf, 1000);
}
// koniec doddatkowej petli do wiadomosci, ktore maja byc przerobione na atakowanie
close(fd);
return 0;
}
else
{
waitpid(pid2, NULL, 0);
sleep(1);
}
}
return 0;
}
Ponizej kod klienta, czyli gracza:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
/* ###### FUNKCJA DRUKUJACA PLANSZE ###### */
void drukujPlansze(int v[][ 10 ] )
{
/* numeracja kolumn */
printf("\t X\n\n");
printf(" 0 1 2 3 4 5 6 7 8 9\n\n");
int i; /* wiersze */
int j; /* kolumny */
/* numeracja werszy */
for ( i = 0; i <= 9; i++ )
{
if( i < 4)
{
printf(" %d ", i);
}
if( i > 4 )
{
printf(" %d ", i);
}
if(i == 4)
{
printf(" 4 ");
}
/* wypisywani * wartosci * kolumn * */
for ( j = 0; j <= 9; j++ )
{
printf( "%d ", v[ i ][ j ] );
}
printf( "\n" ); /* nowa linia */
if( i == 3 )
{
printf("Y");
}
} /* koniec wypisujacej petli for */
}
/*########### KONIEC FUNKCJI DRUKUJACEJ PLANSZE ###### */
/*########### FUNKCJA DO POZIOMEGO ZADAWANIA STATKOW ####### */
void zadawaniePoziome(int p1map[ 10 ][ 10 ], int n)
{
int x=0;
int y=0;
int i=0;
printf("Podaj wsoplrzedna X: ");
scanf("%d",&x);
printf("Podaj wsoplrzedna Y: ");
scanf("%d", &y);
for ( i = x; i<x + n; i++)
{
p1map[y][i]= n;
}
}
/* ########## KONIEC FUNKCJI DO POZIOMEGO ZADAWANIA STATKLOW ######## */
/*########### FUNKCJA DO PIONOWEGO ZADAWANIA STATKOW ####### */
void zadawaniePionowe(int p1map[ 10 ][ 10 ], int n)
{
int x=0;
int y=0;
int i=0;
printf("Podaj wsoplrzedna X twojego %d masztowca: ", n);
scanf("%d",&x);
printf("Podaj wsoplrzedna Y twojego %d masztowca: ", n);
scanf("%d", &y);
for ( i = y; i<y + n; i++)
{
p1map[i][x]= 4;
}
}
/* ########## KONIEC FUNKCJI DO POZIOMEGO ZADAWANIA STATKLOW ######## */
int main(int argc, char *argv[])
{
int user1map[ 10 ][ 10 ] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
int port;
int s;
int n;
char buffer[256];
if (argc < 2 )
{
printf("ERROR! zla ilosc parametrow");
return 0;
}
if (argc < 3)
{
port = 9876;
}
else
{
port = atoi(argv[2]);
}
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
{
printf("ERROR! blad socket\n");
exit(1);
}
struct sockaddr_in serwer;
serwer.sin_family = AF_INET;
serwer.sin_port = htons(port);
serwer.sin_addr.s_addr= INADDR_ANY;
int con;
con = connect(s, (struct sockaddr *) &serwer, sizeof(serwer));
if (con < 0)
{
printf("ERROR! blad connect\n");
exit(1);
}
/*Wczytywanie nicku gracza i wysylanie go na serwer*/
printf(" Podaj swoj nick: ");
scanf("%s", buffer);
n = write(s,buffer,strlen(buffer));
if (n < 0)
printf("ERROR writing to socket");
bzero(buffer,256);
n = read(s,buffer,strlen(buffer));
/* Koniec wczytywania nicku gracza i wysylania go na serwer*/
int a;
/* zadawanie czteromasztowca */
for (a=0; a < 1; a++)
{
char c;
system("clear");
printf(" ========================\n");
printf(" == PLANSZA ==\n");
printf(" ========================\n");
drukujPlansze(user1map);
printf("\n");
printf("Czteromasztowiec: poziomo? (t/n): ");
scanf("%c", &c);
if (c='t')
zadawaniePoziome(user1map,4);
if (c='t')
zadawaniePionowe(user1map,4);
}
/* zadawanie trojmasztowca */
for (a=0; a < 2; a++)
{
char c;
system("clear");
printf(" ========================\n");
printf(" == PLANSZA ==\n");
printf(" ========================\n");
drukujPlansze(user1map);
printf("\n");
/* zadawanie czteromasztowca */
printf("Trojmasztowiec: poziomo? (t/n): ");
scanf("%c", &c);
if (c=='t')
zadawaniePoziome(user1map,3);
if (c=='t')
zadawaniePionowe(user1map,3);
}
/* zadawanie dwumasztowca */
for (a=0; a < 3; a++)
{
char c;
system("clear");
printf(" ========================\n");
printf(" == PLANSZA ==\n");
printf(" ========================\n");
drukujPlansze(user1map);
printf("\n");
/* zadawanie czteromasztowca */
printf("Dwumasztowiec: poziomo? (t/n): ");
scanf("%c", &c);
if (c=='t')
zadawaniePoziome(user1map,2);
if (c=='t')
zadawaniePionowe(user1map,2);
}
/* zadawanie jednoomasztowca */
for (a=0; a < 4; a++)
{
char c;
system("clear");
printf(" ========================\n");
printf(" == PLANSZA ==\n");
printf(" ========================\n");
drukujPlansze(user1map);
printf("\n");
printf("Jednomasztowiec: poziomo? (t/n): ");
scanf("%c", &c);
if (c=='t')
zadawaniePoziome(user1map,1);
if (c=='t')
zadawaniePionowe(user1map,1);
}
//koniec zadawania statkow
//wysylanie tablicy z zadanymi statkami na serwer
n = write(s,user1map, 1000);
if (n < 0)
printf("ERROR writing to socket");
n = read(s,user1map, 1000);
if (n < 0)
printf("ERROR reading from socket");
//koniec wysylania tablicy
//doddatkowa petla do wiadomosci, ktore maja byc przerobione na atakowanie
while (1)
{
n = write(s,buffer, 1000);
if (n < 0)
printf("ERROR writing to socket");
bzero(buffer,256);
n = read(s,buffer, 1000);
if (n < 0)
printf("ERROR reading from socket");
}
// koniec doddatkowej petli do wiadomosci, ktore maja byc przerobione na atakowanie
return 0;
}
Chce aby sie laczylo 2 graczy, ktorzy beda mogli swobodnie grac miedzy saba.