套接字程序挂起

发布于 2024-10-21 22:13:00 字数 1030 浏览 3 评论 0原文

我正在尝试使用 python 进行一些基本的网络连接。下面是进行实际通信的程序片段:

客户端

    # open socket and connect to port
            sock = socket(AF_INET, SOCK_STREAM)
            sock.connect((regHost, regPort))

            # prepare flos for data
            outFlo = sock.makefile(mode='w')
            inFlo = sock.makefile(mode='r')

            outFlo.write(queryString)
            outFlo.flush()
            print "finished writing"

            tmp = inFlo.readline()
            print tmp

            outFlo.close()
            inFlo.close()
            sock.close()

服务器端

        print 'Spawned thread'
        inFlo = self.sock.makefile(mode='r')
        outFlo = self.sock.makefile(mode='w')

        outFlo.write('test writing\n')
        outFlo.flush()

        inFlo.close()
        outFlo.close()
        self.sock.close()
        print 'Closed socket'
        print 'Exiting thread'

该程序似乎挂在客户端对 inFlo.readline() 的调用上。任何帮助将不胜感激。

I'm trying to get some basic networking going in python. Here's the snippet of the program that does the actual communication:

Client Side

    # open socket and connect to port
            sock = socket(AF_INET, SOCK_STREAM)
            sock.connect((regHost, regPort))

            # prepare flos for data
            outFlo = sock.makefile(mode='w')
            inFlo = sock.makefile(mode='r')

            outFlo.write(queryString)
            outFlo.flush()
            print "finished writing"

            tmp = inFlo.readline()
            print tmp

            outFlo.close()
            inFlo.close()
            sock.close()

Server Side

        print 'Spawned thread'
        inFlo = self.sock.makefile(mode='r')
        outFlo = self.sock.makefile(mode='w')

        outFlo.write('test writing\n')
        outFlo.flush()

        inFlo.close()
        outFlo.close()
        self.sock.close()
        print 'Closed socket'
        print 'Exiting thread'

The program seems to be hanging on the call to inFlo.readline() in the client side. Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷途知返 2024-10-28 22:13:00

错误是我忘记在字符串之一的末尾添加 \n 。因此,程序挂在对 inFlo.readline() 的调用上。

The error was that I forgot to add a \n at the end of one of my strings. Due to that, the program was hanging on the call to inFlo.readline().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文