Make server socket in Tkinter allow client input

Asked

Viewed 1,503 times

3

The program works properly, it creates a server for your user, the server opens smoothly. But when I use a client to access it, the following message appears in the server client:

Traceback (Most recent call last): File "C: Users JF Andrade Desktop Scriptspython Program011(Client). py", line 10, in sockobj.connect((serverHost, serverPort)) Connectionrefusederror: [Winerror 10061] No connection could be made because the destination machine actively refused them.

Could someone explain why this mistake? Thanks in advance.**

Server-side

from tkinter import *
from socket import *
import time




 class AdminTools(object):
      def __init__(self, main):


            self.font = ("Verdana", "8", "bold")

            self.Frame1 = Frame(main)
            self.Frame1["bg"] = "LightBlue"

            self.LabDiv2 = Label(main,text = "-----------------------------------------------------------")
            self.LabDiv2["bg"] = "LightBlue"
            self.LabDiv2.pack()


            self.Lab1 = Label(main,text = "Bem-vindo ao Server Manager", fg = "Red", font = self.font)
            self.Lab1["bg"] = "LightBlue"
            self.Lab1.pack()

            self.LabDiv1 = Label(main,text = "-----------------------------------------------------------")
            self.LabDiv1["bg"] = "LightBlue"
            self.LabDiv1.pack()

            self.Lab2 = Label(main, text = "CRIAR NOVO SERVIDOR ", fg = "Green")
            self.Lab2["bg"] = "LightBlue"
            self.Lab2.pack()

            self.Lab3 = Label(main, text = "HOST:", fg = "Black")
            self.Lab3["bg"] = "LightBlue"
            self.Lab3.pack()

            self.Txt1 = Entry(main, bg = "LightGrey", fg = "Red")
            self.Txt1.pack()

            self.Del1 = Button(main, bg = "Red", text = "Del", command = self.ExcluirTexto, width = 6)
            self.Del1.pack()


            self.Lab4 = Label(main, text = "PORTA:", fg = "Black")
            self.Lab4["bg"] = "LightBlue"
            self.Lab4.pack()


            self.Txt2 = Entry(main, fg = "Red", bg = "LightGrey")
            self.Txt2.pack()


            self.LabSpc1 = Label(main,text = "")
            self.LabSpc1["bg"] = "LightBlue"
            self.LabSpc1.pack()


            self.Bt1 = Button(self.Frame1, text = "CRIAR SERVER", fg = "Black", bg = "Green", command = self.CriarServer, width = 12)
            self.Bt1.pack()
            self.Frame1.pack()

            self.LabSpc1 = Label(self.Frame1, text = "", pady = 0)
            self.LabSpc1["bg"] = "LightBlue"
            self.LabSpc1.pack()

            self.Bt2 = Button(self.Frame1, text = "FECHAR SERVER", bg = "RED", width = 12, command = self.FecharServer)
            self.Bt2.pack()

    def ExcluirTexto(self):
                    self.Txt1.delete(0, END)



    def CriarServer(self):
                    Host = str(self.Txt1.get())
                    Port = int(self.Txt2.get())
                    sockobj = socket(AF_INET, SOCK_STREAM) 
                    sockobj.bind((Host, Port))
                    sockobj.listen(5)
                    print("Servidor iniciado")
                    self.Lab3["text"] = "SERVIDOR INICIADO COM SUCESSO!"
                    self.Lab3["fg"] = "Blue"                                                               




  AdminTools(main)

  main.title("Server Manager v1.0")

  main["bg"] = "LightBlue"

  main = Tk()

  main.geometry ("300x300")

  main.mainloop()

Client side

  from socket import *

   serverHost = 'localhost'
   serverPort = 45




   sockobj = socket(AF_INET, SOCK_STREAM)
   sockobj.connect((serverHost, serverPort))
   print("Conexão estabelecida")
  • I’ll put in the post.

  • Look but even so the server does not seem to be very well, take a look here... https://github.com/Miguel-Frazao/simple-chat , no Tkinter but is a multi-threading server (multiple clients) chat... You can run server Xtended that has more prints to see what’s happening

  • I am already working on a multi-thread server, this is a test server. But it has how to explain what happened on the test server?

  • The code you set from the server gives me that main is not set. And you shouldn’t do an infinite loop for the server to run?

  • When I put the loop to keep it online and click create server it decides to crash.

  • Take a look here at the answer http://answall.com/questions/166545/broken-pipe-com-sockets-em-python

Show 1 more comment

2 answers

1

The problem with this code is that although the function that creates a socket for the server is called, it does nothing else: creates a new server, connects it to the port, says it will listen to up to 5 connections and then "ends" - when it ends, all objects created inside it, that are referenced outside the scope of the function are destroyed by Python (and if by chance Python did not do this, you would have a socket in the system, occupying the port, but having no way to access it from the Python code).

In practice your sockets are destroyed fractions of a second after they are created - a call to the method is required accept that will stop the execution of your program (and freeze the Tkinter interface), while waiting for a new connection.

So to make it work from the outside that you do something, you don’t just put a call to socketobj.accept at the end of the method CriarServer - ideally you want to establish a timeout for the socket, call Accept - and, if there is an incoming connection, manage this in a series of functions that function as events to Tkinter - and make no blocking calls to sockets (be accept, be it recv) if a timeout. You can air Python lists and dictionaries to manage multiple server connections in the main thread - or use one thread per connection - but then you’ll have to think of a way for those threads to work well together with Tkinter.

It can work there if it’s a low-resource program: a personal chat, or a test for intranet use. If it’s a server for a service that you might need to scale to hundreds (or even dozens) of clients, it’s best not to try to use it in conjunction with Tkinter - write the server using asyncio, and create a program on Tkinter (or even a web page, why not?) to be the interface that also connects as client, but with special capabilities. That way you don’t have to worry about splitting the resources of the process that is your server with all the graphical part that Tkinter has to manage.

-1

It seems a little strange:

sockobj = socket(AF_INET, SOCK_STREAM)

Instead of:

sockobj = socket.socket(AF_INET, SOCK_STREAM)
  • 1

    drk3, welcome to Stack Overflow in English. In the case of your answer the ideal is that it be explanatory generally informing what the person has missed and why. when it is a short answer, it would be better to add a comment to the question. Here are some tips on how to give a good answer: http://answall.com/help/how-to-answer

  • 1

    In the code, it’s done from socket import * instead of import socket - what plays all the names defined in socket.* in the current namespace. That is: this is not the problem, definitely.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.