2

I am trying to create applications that can communicate through sockets in python.I was already able to achieve this in a console program and was trying to do the same in WPF.It is quite a simple program that should act as the server,where a socket is created,bound to a port then listens for connections.The problem i am facing now is that everytime i include the code for the socket to accept a connection,my program hangs. I have tried to look for the solution online but everyone seems to be doing it exactly the same way i am doing it.Has anyone encountered this problem before and if so how did you solve it. Thanks in advance for your help.

This is the code:

import wpf
import socket
import sys
import clr
import System


HOST=''
PORT = 8888

from System.Windows import Application, Window

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)


class MyWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'managementToolSimulator2.xaml')


    def Button1_Click(self, sender, e):
        pass

    def Window_Loaded(self, sender, e):
        s.bind((HOST,PORT))
        s.listen(5)
        self.ListBox1.AddText('Listening...')

        while 1:
            conn,addr=s.accept()
            self.ListBox1.AddText('Got connection')
        pass


if __name__ == '__main__':
    Application().Run(MyWindow())

If i run the program without the line:

     while 1:
        conn,addr=s.accept()
        self.ListBox1.AddText('Got connection')

it runs fine but if i include the line the program hangs and has to be stopped from the task manager.

Jayanth Koushik
  • 9,476
  • 1
  • 44
  • 52
techRooky
  • 51
  • 1
  • 3

1 Answers1

0

Thread off the server.

You must not make blocking calls in GUI event handlers. The GUI thread must handle Windows messages in a timely manner - you cannot just block execution in a handler.

Martin James
  • 24,453
  • 3
  • 36
  • 60