Make server socket not tkinter allow client input

2

The program works correctly, it creates a server for your user, the server opens without problems. But when I use a client to access it, the following message appears on the server client:

  

Traceback (most recent call last):     File "C: \ Users \ JF Andrade \ Desktop \ ScriptsPython \ Program011 (Server_Server) .py", line 10, in       sockobj.connect ((serverHost, serverPort))   ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused them.

Could it explain why this error? Thank you 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()

Customer Side

  from socket import *

   serverHost = 'localhost'
   serverPort = 45




   sockobj = socket(AF_INET, SOCK_STREAM)
   sockobj.connect((serverHost, serverPort))
   print("Conexão estabelecida")
    
asked by anonymous 04.01.2017 / 17:30

2 answers

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

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

So to make it work out of doing something, do not just put a call to socketobj.accept at the end of the CriarServer method - ideally you should set a timeout for the socket, call accept - and if there is a connection, manage this in a series of functions that function as events to tkinter - and do not make any blocking calls to the sockets (be accept , be recv ) if a timeout. You can air Python lists and dictionaries to manage multiple connections to the server on the main thread - or use one thread per connection - but then you'll have to think of a way these threads work well with tkinter.

It can work out there if it's a low-demand feature program: a personal chat, or a test for intranet use. If you are a server for a service that may need to escalate to hundreds (or even several tens) of clients, it is best not to try to use it in conjunction with tkinter - write the server using asyncio, and create a program in tkinter even a web page, why not?) to be the interface that also connects as a client, but with special capabilities. That way you do not have to worry about dividing the resources of the process that is your server with all the graphical part that tkinter has to manage.

    
20.03.2018 / 17:10
-1

That seems a little strange:

sockobj = socket(AF_INET, SOCK_STREAM)

Instead of:

sockobj = socket.socket(AF_INET, SOCK_STREAM)
    
21.03.2017 / 17:49