-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClienteTCP.c
More file actions
132 lines (120 loc) · 3.91 KB
/
Copy pathClienteTCP.c
File metadata and controls
132 lines (120 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <arpa/inet.h>
#include "Juego.h"
void imprimir(char c[250]){
if(((strncmp(c, "+Ok. Empieza la partida.",24))==0)|| (((strncmp(c, "+Ok. Nuevo tablero.",19)))==0)){
char *token;
token=strtok(c, ".");
printf("%s",token);
token=strtok(NULL, ".");
printf("%s\n",token);
printf("|1|2|3|4|5|6|7|\n|");
token=strtok(NULL, "\n");
for(int l=0; l<(strlen(token)-1); l++){
if(token[l]=='-'){
token[l]=' ';
printf("%c", token[l]);
}
else if(token[l]==','){
token[l]='|';
printf("%c", token[l]);
}
else if(token[l]=='x'){
token[l]='x';
printf("%c", token[l]);
}
else if(token[l]=='o'){
token[l]='o';
printf("%c", token[l]);
}
else if(token[l]==';'){
token[l]='|';
printf("%c\n|", token[l]);
}
}
printf("|\n");
}else{
printf("%s\n",c);
}
}
int main(){
/*----------------------------------------------------
Descriptor del socket y buffer de datos
-----------------------------------------------------*/
int sd;
struct sockaddr_in sockname;
char buffer[250];
socklen_t len_sockname;
fd_set readfds, auxfds;
int salida;
int fin = 0;
int option;
/* --------------------------------------------------
Se abre el socket
---------------------------------------------------*/
sd = socket (AF_INET, SOCK_STREAM, 0);
if (sd == -1){
perror("No se puede abrir el socket cliente\n");
exit (1);
}
/* ------------------------------------------------------------------
Se rellenan los campos de la estructura con la IP del
servidor y el puerto del servicio que solicitamos
-------------------------------------------------------------------*/
sockname.sin_family = AF_INET;
sockname.sin_port = htons(2000);
sockname.sin_addr.s_addr = inet_addr("127.0.0.1");
/* ------------------------------------------------------------------
Se solicita la conexión con el servidor
-------------------------------------------------------------------*/
len_sockname = sizeof(sockname);
if (connect(sd, (struct sockaddr *)&sockname, len_sockname) == -1){
perror ("Error de conexión");
exit(1);
}
//Inicializamos las estructuras
FD_ZERO(&auxfds);
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(sd,&readfds);
/* ------------------------------------------------------------------
Se transmite la información
-------------------------------------------------------------------*/
do{
auxfds = readfds;
salida = select(sd+1,&auxfds,NULL,NULL,NULL);
//Tengo mensaje desde el servidor
if(FD_ISSET(sd, &auxfds)){
bzero(buffer,sizeof(buffer));
recv(sd,buffer,sizeof(buffer),0);
imprimir(buffer);
//printf("%s",buffer);
if(strcmp(buffer,"Demasiados clientes conectados\n") == 0){
fin =1;
}
if(strcmp(buffer,"Desconexión servidor\n") == 0){
fin =1;
}
}else{
//He introducido información por teclado
if(FD_ISSET(0,&auxfds)){
bzero(buffer,sizeof(buffer));
fgets(buffer,sizeof(buffer),stdin);
if(strcmp(buffer,"SALIR\n") == 0){
fin = 1;
}
send(sd,buffer,sizeof(buffer),0);
}
}
}while(fin == 0);
close(sd);
return 0;
}