Bottom Page

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()
Quote
#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 :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  how to send an image from server to client using PICKLE module dafdaf 1 212 Jun-02-2020, 01:08 PM
Last Post: nuffink
  Selector file descriptor issue in simple multiclient server program dohpam1ne 0 59 May-28-2020, 02:39 PM
Last Post: dohpam1ne
  how can i send a list of tuples from the server to the client using sockets? dafdaf 1 170 Apr-13-2020, 10:51 PM
Last Post: Larz60+
  Python script multi client server sonra 1 225 Mar-24-2020, 03:49 PM
Last Post: Larz60+
  simple udp server/client cardmaker 2 599 Nov-26-2019, 12:36 AM
Last Post: micseydel
  Slow websockets server -> client pattagghiu 0 413 Sep-17-2019, 09:28 AM
Last Post: pattagghiu
  Chat (Client-Server) andresdrr 3 865 Aug-26-2019, 02:01 PM
Last Post: ThomasL
  Send raw string via socket with out any changes john36 18 1,545 Aug-01-2019, 07:54 AM
Last Post: john36
  Python server(Django web site)/client(Python app) connection Junior_Pythoneer 5 686 Jul-05-2019, 05:41 PM
Last Post: noisefloor
  How to combine data taken from server between client and GUI? bescf 9 1,605 Apr-02-2019, 11:48 AM
Last Post: bescf

Forum Jump:


Users browsing this thread: 1 Guest(s)