Python Forum
Python sockets : Client sending to the server
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python sockets : Client sending to the server
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Networking Issues - Python GUI client and server connection always freezes Veritas_Vos_Liberabit24 0 679 Mar-21-2023, 03:18 AM
Last Post: Veritas_Vos_Liberabit24
  Getting "SSL client not supported by this Python installation" error prabirsarkar 0 915 Mar-13-2023, 05:01 PM
Last Post: prabirsarkar
  creating python server and client in a pc kucingkembar 4 1,955 Nov-29-2021, 01:37 PM
Last Post: kucingkembar
  Real-Time output of server script on a client script. throwaway34 2 2,010 Oct-03-2021, 09:37 AM
Last Post: ibreeden
  Sending string commands from Python to a bluetooth device Rovelin 13 9,276 Aug-31-2021, 06:40 PM
Last Post: deanhystad
  How to take the tar backup files form remote server to local server sivareddy 0 1,871 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  Python win32com.client: What are the syntax to open exe file & activate its window? JaneTan 0 4,130 Oct-14-2020, 09:09 AM
Last Post: JaneTan
  Sending Out Email via Python JoeDainton123 1 4,699 Aug-31-2020, 12:54 AM
Last Post: nilamo
  How can I make this server stop listening for connections to sockets Emekadavid 0 3,385 Jun-03-2020, 02:28 PM
Last Post: Emekadavid
  problems sending BIN file with XMODEM python shaya2103 0 2,775 Nov-23-2019, 10:27 AM
Last Post: shaya2103

Forum Jump:

User Panel Messages

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