Week 13, Update 01
This commit is contained in:
parent
feb247e6d9
commit
c6ce1eea2e
|
@ -18,7 +18,7 @@
|
|||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
# Importando funções
|
||||
# Importando bibliotecas
|
||||
import os, random, json
|
||||
|
||||
# Armazenando caminho completo do diretório desse programa para
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
# Importando funções
|
||||
# Importando bibliotecas
|
||||
import os, json, requests
|
||||
|
||||
# Armazenando caminho completo do diretório desse programa para
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
# Importando funções
|
||||
# Importando biblioteca
|
||||
import os
|
||||
|
||||
# Armazenando caminho completo do diretório desse programa para
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
# Importando funções
|
||||
# Importando bibliotecas
|
||||
import os, socket
|
||||
|
||||
# Armazenando caminho completo do diretório desse programa para
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
# Importando funções
|
||||
# Importando bibliotecas
|
||||
import os, socket
|
||||
|
||||
# Armazenando caminho completo do diretório desse programa para
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
import socket
|
||||
from socket_constants import *
|
||||
|
||||
# Criando o socket TDP
|
||||
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
# Ligando o socket a porta
|
||||
tcp_socket.connect((HOST_SERVER, SOCKET_PORT))
|
||||
|
||||
while True:
|
||||
# Recebendo echo do servidor
|
||||
dado_recebido = tcp_socket.recv(BUFFER_SIZE)
|
||||
mensagem_recebida = dado_recebido.decode(CODE_PAGE)
|
||||
print(f'Mensagem Recebida: {mensagem_recebida}')
|
||||
|
||||
# Devolvendo uma mensagem (echo) ao cliente
|
||||
mensagem_retorno = 'Devolvendo mensagem: ' + mensagem_recebida
|
||||
tcp_socket.send(mensagem_retorno.encode(CODE_PAGE))
|
||||
|
||||
# Fechando o socket
|
||||
tcp_socket.close()
|
|
@ -1,32 +0,0 @@
|
|||
import socket
|
||||
from socket_constants import *
|
||||
|
||||
# Criando o socket TCP
|
||||
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
# Ligando o socket a porta
|
||||
tcp_socket.bind((HOST_SERVER, SOCKET_PORT))
|
||||
|
||||
# Máximo de conexões enfileiradas
|
||||
tcp_socket.listen(MAX_LISTEN)
|
||||
|
||||
while True:
|
||||
# Aceita a conexão com o cliente
|
||||
conexao, cliente = tcp_socket.accept()
|
||||
print('Conectado por: ', cliente)
|
||||
while True:
|
||||
mensagem = input('Digite a mensagem: ')
|
||||
if not mensagem:
|
||||
continue
|
||||
# Convertendo a mensagem digitada de string para bytes
|
||||
mensagem = mensagem.encode(CODE_PAGE)
|
||||
# Enviando a mensagem ao cliente
|
||||
conexao.send(mensagem)
|
||||
|
||||
mensagem = conexao.recv(BUFFER_SIZE)
|
||||
if not mensagem:
|
||||
break
|
||||
print(cliente, mensagem.decode(CODE_PAGE))
|
||||
|
||||
print('Finalizando Conexão do Cliente ', cliente)
|
||||
conexao.close()
|
|
@ -0,0 +1,52 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ifrn/2023.2/programacao_para_redes/projeto/cliente.py
|
||||
# Copyright (C) 2023 Sidney Pedro
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
# Importando bibliotecas
|
||||
from constantes import *
|
||||
import socket
|
||||
|
||||
# Criando socket TCP
|
||||
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
# Conectando socket
|
||||
status_conexao = server_sock.connect_ex(SOCKET_SERVIDOR)
|
||||
if (status_conexao != 0):
|
||||
print("Erro: não foi possível se conectar ao servidor!")
|
||||
exit()
|
||||
print("Conexão estabelecida com o servidor!")
|
||||
|
||||
while (True):
|
||||
# Recebendo e apresentando mensagem do servidor
|
||||
try:
|
||||
mensagem_recebida = server_sock.recv(TAMANHO_BUFFER).decode(CHARSET)
|
||||
except:
|
||||
print()
|
||||
break
|
||||
if (len(mensagem_recebida) == 0):
|
||||
print("Conexão perdida com o servidor!")
|
||||
break
|
||||
print(f"Mensagem recebida: {mensagem_recebida}")
|
||||
|
||||
# Devolvendo mensagem ao servidor
|
||||
mensagem_retorno = ("Devolvendo mensagem: " + mensagem_recebida).encode(CHARSET)
|
||||
server_sock.send(mensagem_retorno)
|
||||
|
||||
# Finalizando socket
|
||||
server_sock.close()
|
|
@ -0,0 +1,26 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ifrn/2023.2/programacao_para_redes/projeto/constantes.py
|
||||
# Copyright (C) 2023 Sidney Pedro
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
# Armazenando informações do servidor, de socket e codificação de
|
||||
# caracteres
|
||||
SOCKET_SERVIDOR = ("localhost", 50000)
|
||||
CONEXOES_MAXIMAS = 1
|
||||
TAMANHO_BUFFER = 512
|
||||
CHARSET = "utf-8"
|
|
@ -0,0 +1,69 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ifrn/2023.2/programacao_para_redes/projeto/servidor.py
|
||||
# Copyright (C) 2023 Sidney Pedro
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
# Importando bibliotecas
|
||||
from constantes import *
|
||||
import socket
|
||||
|
||||
# Criando socket TCP e desabilitando TIME_WAIT
|
||||
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
|
||||
# Habilitando socket
|
||||
server_sock.bind(SOCKET_SERVIDOR)
|
||||
|
||||
# Escutando até o máximo de conexões simultaneas permitidas
|
||||
server_sock.listen(CONEXOES_MAXIMAS)
|
||||
|
||||
parar = False
|
||||
while (not parar):
|
||||
# Recebendo conexão do cliente e o apresentando
|
||||
try:
|
||||
client_sock, SOCKET_CLIENTE = server_sock.accept()
|
||||
except:
|
||||
print()
|
||||
break
|
||||
print(f"Cliente {SOCKET_CLIENTE} conectado!")
|
||||
|
||||
while (True):
|
||||
# Obtendo mensagem do usuário
|
||||
try:
|
||||
mensagem = input("Digite uma mensagem: ").encode(CHARSET)
|
||||
except:
|
||||
parar = True
|
||||
print()
|
||||
break
|
||||
if (len(mensagem) == 0):
|
||||
continue
|
||||
|
||||
# Enviando mensagem obtida ao cliente
|
||||
client_sock.send(mensagem)
|
||||
|
||||
# Recebendo mensagem do cliente
|
||||
mensagem_recebida = client_sock.recv(TAMANHO_BUFFER).decode(CHARSET)
|
||||
if (len(mensagem_recebida) == 0):
|
||||
print(f"Cliente {SOCKET_CLIENTE} desconectado!")
|
||||
break
|
||||
|
||||
print(f"{SOCKET_CLIENTE}> {mensagem_recebida}")
|
||||
|
||||
client_sock.close()
|
||||
|
||||
server_sock.close()
|
|
@ -1,6 +0,0 @@
|
|||
HOST_SERVER = 'localhost' # Definindo o IP do servidor
|
||||
SOCKET_PORT = 50000 # Definindo a porta
|
||||
BUFFER_SIZE = 512 # Definindo o tamanho do buffer
|
||||
CODE_PAGE = 'utf-8' # Definindo a página de códigos de caracteres-
|
||||
|
||||
MAX_LISTEN = 1 # Definindo o máximo de conexões enfileiradas
|
Loading…
Reference in New Issue