Python Forum
Simple send and recive string Server/Client
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple send and recive string Server/Client
#1
Hi there, i'm new so sorry if I miss a rule, I read it but i'm not sure if get them all
And sorry for my english i'm learning this Big Grin

Mi problem is next: In this moment I have a simple multi thread server and a client that send and recieve a string

The questions: What occur if the client close the program? The listening of client from server is still running? If not close, how can I close the connection if the client show no activity during some time of inactivity?

There is my code:

Server:

import socket
import threading

host = "127.0.0.1"
port = 27501
maxClients = 1000

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("Socket Created")
sock.bind((host, port))
print ("Socket bind complete")
sock.listen(maxClients)
print ("Socket now listening")

def action(*args):
    conn = args[0]
    addr = args[1]
    try:
        print('Connection with: ', format(addr))
        while True:
            data = conn.recv(4096)
            if data:
                print('Recieved: ', format(data.decode('utf-8')))
                answer = "SERVER: You send > "+data.decode('utf-8')
                conn.send(answer.encode("utf-8"))
                break
    finally:
        doSomeThing = 1

while True:
    conn, addr = sock.accept()
    threading.Thread(target=action, args=(conn, addr)).start()
Client:

import socket
import sys

server_address = ('localhost', 27501)

while True:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(server_address)
    msg = input("Send data > ")   
    try:
             
        print ("Send: ",msg)
        sock.sendall(msg.encode("utf-8"))
         
        amount_received = 0
        amount_expected = len(msg)
             
        while amount_received < amount_expected:
            data = sock.recv(4096)
            amount_received += len(data)
            print ("Answer\n-----> ", data.decode("utf-8"))
         
    finally:
        doSomeThing = 1
        
    opc = input("[ENTER] other msg\n[Some value] Close\n")
    if (opc != ""):
        break
That is all, I hope you can help
Bye Folks

Sorry this is real code, the last close the connection before the action

Server:

import socket
import threading
import datetime

host = "127.0.0.1"
port = 27501
maxClients = 1000


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("Socket Created")
sock.bind((host, port))
print ("Socket bind complete")
sock.listen(maxClients)
print ("Socket now listening")

def action(*args):
    conn = args[0]
    addr = args[1]
    try:
        print('Connection with: ', format(addr))
        while True:
            data = conn.recv(4096)
            if data:
                print('Recieved: ', format(data.decode('utf-8')))
                answer = "SERVER: You send > "+data.decode('utf-8')
                conn.send(answer.encode("utf-8"))
    finally:
        return 1

while True:
    conn, addr = sock.accept()
    threading.Thread(target=action, args=(conn, addr)).start()
Client:
import socket
import threading
import datetime

host = "127.0.0.1"
port = 27501
maxClients = 1000


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("Socket Created")
sock.bind((host, port))
print ("Socket bind complete")
sock.listen(maxClients)
print ("Socket now listening")

def action(*args):
    conn = args[0]
    addr = args[1]
    try:
        print('Connection with: ', format(addr))
        while True:
            data = conn.recv(4096)
            if data:
                print('Recieved: ', format(data.decode('utf-8')))
                answer = "SERVER: You send > "+data.decode('utf-8')
                conn.send(answer.encode("utf-8"))
    finally:
        return 1

while True:
    conn, addr = sock.accept()
    threading.Thread(target=action, args=(conn, addr)).start()
Reply
#2
One way you could try is to send one last message to the server saying that the client is now logged off. if you are running a console application catch the keyboard interrupt.
The best way to clean up in this scenario would be to have a timer running on the server that closes everything after some amount of time.
from time import time
start = time()
# some communication stuff
end = time()
if end - start >= time_threshold:
      # close server or reset server
make sure this only happens after a client connected to the server :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,274 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  Client/Server proper finalizing transfer wolfman5874 1 1,421 Jul-04-2022, 07:35 PM
Last Post: wolfman5874
Bug Problem connecting TLS client written in C++ and Twisted server gpropf 0 1,361 Jun-12-2022, 05:57 PM
Last Post: gpropf
  Server/client basic communication ebolisa 0 2,009 Sep-30-2021, 12:22 PM
Last Post: ebolisa
  Client server Multithreading Anan 6 5,752 Apr-21-2021, 08:19 PM
Last Post: SheeppOSU
Question Trouble with Client/Server reverse Shell! Gilush 0 2,757 Feb-03-2021, 01:04 PM
Last Post: Gilush
  Basic client server code question swisscheese 4 3,191 Dec-12-2020, 08:51 AM
Last Post: Larz60+
  How can i create a server for already existing client using Python? Chapanson 21 7,311 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Simple TCP Client and TCP Server Problem Vapulabis 5 4,335 Jul-12-2020, 05:09 PM
Last Post: ndc85430
  how to send an image from server to client using PICKLE module dafdaf 1 3,066 Jun-02-2020, 01:08 PM
Last Post: nuffink

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020