#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# Server side of the NetworkDemo application
#

#
# External dependencies
#
import socket
import sys
import time

# Set up Internet TCP socket
lstn = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
lstn.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )

# Server port number
port = int( sys.argv[ 1 ] )

# Bind lstn socket to this port 
lstn.bind( ( '', port ) )

# Start listening for contacts from clients
lstn.listen( 5 )

# Client list
hosts = []

# Required client number
N = 2

# Client connection list
liste_client = {}

# Client connection
while len( hosts ) < N :
	
	clnt, ap = lstn.accept()
	client = clnt.getpeername()
	print( 'Client {}:{} connected...'.format( *client ) )
	hosts.append( client )
	liste_client[ client ] = clnt

# Close the server
lstn.close()

# Screen size
sw = 1920
sh = 1200

# Ball coordinates and speed
x, y, v, dx, dv = 50, 50, 0, 12, 5

# Border
sh = sh - 100 - 30

# Main loop
while 1 :

	# Horizontal move
	if x > N*sw or x < 0 :     # rebond sur les parois latérales :
		dx = -dx             # on inverse le déplacement
	x = x + dx

	# Vertical speed variation
	v = v + dv

	# Vertical move
	y = y + v 
	if y > sh :              # niveau du sol à 240 pixels : 
		y = sh             #  défense d'aller + loin !
		v = -v               # rebond : la vitesse s'inverse

	# Send ball coordinates to the clients
	for n, chost in enumerate( hosts ) :
		
	#	print( 'Coordonnées ({},{}) envoyées à {}:{}'.format( x, y, *chost ) )
		clnt = liste_client[ chost ]
		clnt.send( '{},{}.\n'.format( x-n*sw, y ) )

	# Timer
	time.sleep( 0.03 )

@@ -1547,16 +1547,50 @@
 ost %5D%0A%09%09
+%0A%09%09# Send the coordinates%0A%09%09try : 
 clnt.sen
@@ -1624,16 +1624,231 @@
 w, y ) )
+%0A%09%09%0A%09%09#%C2%A0Client isn't here anymore%0A%09%09except socket.error :%0A%09%09%09print( 'Connection lost with %7B%7D:%7B%7D...'.format( *chost ) )%0A%09%09%09hosts.remove( chost )%0A%09%09%09del liste_client%5B chost %5D%0A%09%0A%09# No more clients%0A%09if not hosts : break
 %0A%0A%09#%C2%A0Tim

Handle lost connections.