Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ARS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
danredo
ARS
Commits
4840d0f0
Commit
4840d0f0
authored
2 months ago
by
danredo
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
b32696a2
Branches
main
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
arst.c
+126
-0
126 additions, 0 deletions
arst.c
with
126 additions
and
0 deletions
arst.c
0 → 100644
+
126
−
0
View file @
4840d0f0
/**
* Practica Tema 6: DAYTIME TCP
*
* Mata, David
*
*
*/
#include
<stdio.h>
#include
<string.h>
#include
<stdlib.h>
#include
<netdb.h>
#include
<arpa/inet.h>
#include
<sys/types.h>
#include
<sys/socket.h>
#include
<netinet/ip.h>
// Define sockaddr_in
#include
<errno.h>
// Para usar errno y perror()
#include
<unistd.h>
int
main
(
int
argc
,
char
*
argv
[])
{
struct
servent
*
servicio
;
// Estructura para guardar el puerto
struct
sockaddr_in
direcccion
;
// Estructura para guardar la ip del servidor
int
puerto
;
int
numero_socket
;
char
mensaje
[
30
]
=
"¿Que dia es hoy?"
;
char
respuesta
[
100
];
// Buffer para almacenar la respuesta del servidor
struct
sockaddr_in
myaddr
;
// Estructura para guardar la direccion
myaddr
.
sin_family
=
AF_INET
;
// Sockets IPv4
myaddr
.
sin_port
=
0
;
// Si cliente, no indicar puerto.
myaddr
.
sin_addr
.
s_addr
=
INADDR_ANY
;
// Comprobamos que el numero de argumentos sea correcto
if
(
argc
<
2
||
argc
>
5
)
{
fprintf
(
stderr
,
"Error en el numero de argumentos"
);
exit
(
EXIT_FAILURE
);
}
// Transforma la direccion IP en numero entero de 32bits
if
(
inet_aton
(
argv
[
1
],
&
direcccion
.
sin_addr
)
==
0
)
{
fprintf
(
stderr
,
"Error al transfromar la direccion IP"
);
exit
(
EXIT_FAILURE
);
}
// Comprueba si se pasa el numero de puerto con la opcion -p
if
(
argc
==
4
&&
strcmp
(
argv
[
2
],
"-p"
)
==
0
)
{
puerto
=
htons
(
atoi
(
argv
[
3
]));
// Convierte el puerto de una cadena a un entero
}
else
{
// Si no introduce puerto lo obtiene del servicio daytime(13 UDP)
servicio
=
getservbyname
(
"daytime"
,
"tcp"
);
// Comprobamos que no hay ningun error
if
(
servicio
==
NULL
)
{
fprintf
(
stderr
,
"No se consigue encontrar el servicio"
);
exit
(
EXIT_FAILURE
);
}
puerto
=
servicio
->
s_port
;
// Asociamos el puerto a la estructura del servidor
}
// Creamos la variable sockect
numero_socket
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
// Comprobamos que no hay ningun error
if
(
numero_socket
<
0
)
{
perror
(
"Error al crear el socket"
);
exit
(
EXIT_FAILURE
);
}
// Enlazamos el socket con nuestra direccion
int
sock_bind
=
bind
(
numero_socket
,
(
struct
sockaddr
*
)
&
myaddr
,
sizeof
(
myaddr
));
// Comprobamos que no hay ningun error
if
(
sock_bind
<
0
)
{
perror
(
"Error en la funcion bind"
);
exit
(
EXIT_FAILURE
);
}
direcccion
.
sin_family
=
AF_INET
;
// Asociamos la familia(Sockets IPv4)
direcccion
.
sin_port
=
puerto
;
// Devuelve el puerto en formato red
// Establece la conexion con el servidor a traves de un sockect
int
sockect_connect
=
connect
(
numero_socket
,
(
struct
sockaddr
*
)
&
direcccion
,
sizeof
(
direcccion
));
// Comprobamos que no hay ningun error
if
(
sockect_connect
<
0
)
{
perror
(
"Error en la funcion connect"
);
exit
(
EXIT_FAILURE
);
}
// Enviamos el mensaje al servidor a traves del sockect TCP
int
sock_send
=
send
(
numero_socket
,
mensaje
,
sizeof
(
mensaje
),
0
);
// Comprobamos que no hay ningun error
if
(
sock_send
<
0
)
{
perror
(
"Error en la funcion send"
);
exit
(
EXIT_FAILURE
);
}
// Recibimos el mensaje del servidor a traves del sockect UDP
int
sockect_recv
=
recv
(
numero_socket
,
respuesta
,
sizeof
(
respuesta
),
0
);
// Comprobamos que no hay ningun error
if
(
sockect_recv
<
0
)
{
perror
(
"Error en la funcion recv"
);
exit
(
EXIT_FAILURE
);
}
// Imprimimos el mensaje del servidor
printf
(
"%s
\n
"
,
respuesta
);
// Notifica de que se va a cerrar la conexion
shutdown
(
numero_socket
,
SHUT_RDWR
);
// Cerramos el sockect
close
(
numero_socket
);
exit
(
0
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment