-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e8d34a4
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import socket | ||
|
||
class TCP_Client: | ||
|
||
def __init__(self, nombreDelServer, puertoDelServer): | ||
self.ip = nombreDelServer | ||
self.puerto = puertoDelServer | ||
|
||
def iniciar(self): | ||
|
||
clienteSocketTCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
clienteSocketTCP.connect((self.ip, self.puerto)) | ||
mensaje_request = " soy un cliente" | ||
clienteSocketTCP.send(mensaje_request.encode()) | ||
mensajeRecivido = clienteSocketTCP.recv(1024) | ||
print(mensajeRecivido.decode()) | ||
clienteSocketTCP.close() | ||
|
||
class MainTCP_Client: | ||
c = TCP_Client("127.0.0.1", 5030) | ||
c.iniciar() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import socket | ||
|
||
class TCP_Server: | ||
|
||
def __init__(self, ip, puerto): | ||
self.ipServer = ip | ||
self.puertoServer = puerto | ||
|
||
def inicio(self): | ||
#AF_INET: hace referencia a IPv4, | ||
#SOCK_STREAM: protocolo nos da una comunicación fiable de direcciones en un flujo de datos TCP | ||
serverSocketTCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
#blind: metodo para designacion de IP y Puerto para el socket | ||
serverSocketTCP.bind((self.ipServer, self.puertoServer)) | ||
# Esperando a un posible cliente | ||
print("***** Servidor TCP iniciado... ****") | ||
print("esperando a clientes ...") | ||
# Para mantener el servidor tcp abierto | ||
while True: | ||
# Esperanda un posible cliente | ||
serverSocketTCP.listen(1) | ||
#coneccionSocket : objeto cliente | ||
# direccionIP: direccion IP | ||
# serverSocketTCP.accept(): El método accept() espera una conexión. Cuando llega, este acepta la conexión | ||
coneccionSocket, direccionIP = serverSocketTCP.accept() | ||
print(f"cliente conectado desde la IP : {direccionIP} ") | ||
mensaje_response = "conectado al servidor TCP..." | ||
# envía una respuesta (texto u otros datos) al cliente en el standar estandar "utf-8" | ||
coneccionSocket.send(mensaje_response.encode('utf-8')) | ||
coneccionSocket.close() | ||
|
||
class MainTCP_Server: | ||
s = TCP_Server("127.0.0.1", 5030) | ||
s.inicio() | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import socket | ||
|
||
class UDP_Client: | ||
|
||
paqueteRecibido = "" | ||
paqueteAEnviar = "" | ||
|
||
def __init__(self, nombreDelServer, puertoDelServer): | ||
self.ip = nombreDelServer | ||
self.puerto = puertoDelServer | ||
|
||
def iniciar(self): | ||
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
clientSocket.sendto(("soy el cliente").encode(), (self.ip, self.puerto )) | ||
mensajeRecivido, serverAddress = clientSocket.recvfrom(1024) | ||
print(mensajeRecivido.decode()) | ||
clientSocket.close() | ||
|
||
class MainUDP_Client: | ||
c = UDP_Client("127.0.0.1", 5012) | ||
c.iniciar() | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import socket | ||
|
||
class UDP_Server: | ||
paqueteRecibido = "" | ||
paqueteAEnviar = "" | ||
|
||
def __init__(self, ip, puerto): | ||
self.ipServer = ip | ||
self.puertoServer = puerto | ||
|
||
def inicio(self): | ||
|
||
serverSocketUDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
|
||
serverSocketUDP.bind((self.ipServer, self.puertoServer)) | ||
|
||
print ("El servidor esta esperando para recibir...") | ||
|
||
while True: | ||
|
||
mensaje, direccionIP = serverSocketUDP.recvfrom(1024) | ||
paqueteRecibido = mensaje; | ||
print(f"cliente conectado desde la IP : {direccionIP} ") | ||
paqueteAEnviar = "conectado al servidor" | ||
serverSocketUDP.sendto(paqueteAEnviar.encode(), direccionIP) | ||
|
||
def fin(): | ||
clientsocket.close() | ||
serverSocketUDP.close() | ||
|
||
|
||
class MainUDP_Server: | ||
s = UDP_Server("127.0.0.1", 5012) | ||
s.inicio() | ||
s.fin() | ||
|
||
|
||
|
||
|
||
|
||
|