Python Forum

Full Version: Python sockets : Client sending to the server
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm working on a game project with tkinter and python 3. I already did the connexion beetween my server and my client.

When I open my server, a window opens with the possibility of launching the server or not (start and stop). The names of the participants are displayed at the bottom of the window when they are connected.

When I launch my client, another window opens where I have to type the nickname for the game. When I clicks on "connect" the client should sends the information to the server which adds the name of the participant to the list.

I did several tests with print to know how far it will go in the function connect_to_the_server and the print does not appear anymore when I put it belowclient.send (name )

Server.py
import tkinter as tk
import socket
import threading
from time import sleep
import random

server = None
HOST_ADDR = "192.168.1.13"
HOST_PORT = 5555
client_name = ""
clients = []
clients_names = []
player_data = []

def send_receive_client_message(client_connection, client_ip_addr):
    global server, client_name, clients, player_data, player0, player1

    client_msg = " "  
    client_name = client_connection.recv(4096)

    clients_names.append(client_name)
    update_client_names_display(clients_names)  # update client names display

def accept_clients(the_server, y):
    while True:
        if len(clients) < 2:
            client, addr = the_server.accept()
            clients.append(client)

            # use a thread so as not to clog the gui thread
            threading._start_new_thread(send_receive_client_message, (client, addr))
Client.py
import tkinter as tk
from tkinter import PhotoImage
from tkinter import messagebox
import socket
from time import sleep
import threading

#This is the button that should send the name to the server
btn_connect = tk.Button(top_welcome_frame, text="Connect", command=lambda: connect()) 

client = None
HOST_ADDR = "192.168.1.13"
HOST_PORT = 5555

def connect():
    global your_details
    if len(ent_name.get()) < 1:
        tk.messagebox.showerror(title="ERROR!!!", message="You MUST enter your first name <e.g. John>")
    else:
        your_details["name"] = ent_name.get()
        connect_to_server(ent_name.get())


def connect_to_server(name):
    global client, HOST_PORT, HOST_ADDR
    try:
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.connect((HOST_ADDR, HOST_PORT))
        print("Connexion OK")
        client.send(name)  # Send name to server after connecting
        print("name send")
        # start a thread to keep receiving message from server
        # do not block the main thread :)
        threading._start_new_thread(receive_message_from_server, (client, "m"))
        top_welcome_frame.pack_forget()
        top_frame.pack(side=tk.TOP)
        window_main.title("Tic-Tac-Toe Client - " + name)
    except Exception as e:
        tk.messagebox.showerror(title="ERROR!!!", message="Cannot connect to host: " + HOST_ADDR + " on port: " + str(
            HOST_PORT) + " Server may be Unavailable. Try again later")
I'm trying to solve this problem, but I don't know where it come from. Thanks for your help.