V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
2a
V2EX  ›  Python

哪位大神能告诉我问题出在哪?

  •  
  •   2a · 2015-07-31 11:27:37 +08:00 · 2349 次点击
    这是一个创建于 3198 天前的主题,其中的信息可能已经有所发展或是发生改变。
    点进来的,先说声thanks。

    自己跟着教程写了个服务器和客户端,一个电脑上运行,本想做给妹子的简易聊天工具

    可惜客户端这边出了问题,客户端先发送信息,服务器接收到后返回,客户端就出错了,新手不知道问题出在哪,无法百度谷歌,只好来求助了,客户端代码如下:

    try:
    import socket

    s = socket.socket()

    host = socket.gethostname()
    port = 1234

    while True:
    s.connect((host,port))
    request = raw_input("say sth: ")
    s.sendall(request)

    reply = s.recv(1024)
    print "reply is: ",reply
    s.close()
    except:
    raw_input('something wrong happend')
    12 条回复    2015-07-31 15:15:57 +08:00
    2a
        1
    2a  
    OP
       2015-07-31 11:34:59 +08:00
    服务器代码如下:


    import socket

    s = socket.socket()

    host = socket.gethostname()
    port = 1234
    s.bind((host, port))

    s.listen(5)
    while True:
    c, addr = s.accept()
    request = c.recv(1024)
    print "request is: ",request
    print "connect by ",addr

    sentence = raw_input("please enter\n")
    c.sendall(sentence)
    c.close()
    aheadlead
        2
    aheadlead  
       2015-07-31 13:55:36 +08:00 via iPhone
    妹子…
    aheadlead
        3
    aheadlead  
       2015-07-31 13:57:23 +08:00 via iPhone
    错误信息请贴上来…
    2a
        4
    2a  
    OP
       2015-07-31 14:08:25 +08:00
    Traceback (most recent call last):
    File "<stdin>", line 2, in <module>
    File "C:\Python27\lib\socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
    File "C:\Python27\lib\socket.py", line 170, in _dummy
    raise error(EBADF, 'Bad file descriptor')
    socket.error: [Errno 9] Bad file descriptor
    2a
        5
    2a  
    OP
       2015-07-31 14:08:57 +08:00
    @aheadlead 到底是什么导致了我像个妹子
    kkhaike
        6
    kkhaike  
       2015-07-31 14:17:35 +08:00
    @aheadlead 你的萌头像呢。。。
    楼主好像是你服务器close了,按道理这个关闭应该是客户端完成。
    oska874
        7
    oska874  
       2015-07-31 14:32:15 +08:00
    socket 编程好多问题。改过之后
    server:
    import socket

    s = socket.socket()

    host = socket.gethostname()
    port = 1234
    s.bind((host, port))

    s.listen(5)
    c, addr = s.accept()
    while True:
    request = c.recv(1024)
    print "request is: ",request
    print "connect by ",addr
    sentence = raw_input("please enter\n")
    c.sendall(sentence)
    c.close()

    client:
    import socket

    s = socket.socket()

    host = socket.gethostname()
    port = 1234

    s.connect((host,port))
    while True:
    request = raw_input("say sth: ")
    s.sendall(request)
    reply = s.recv(1024)
    print "reply is: ",reply
    s.close()

    ubuntu 底下试了ok
    2a
        8
    2a  
    OP
       2015-07-31 14:46:05 +08:00
    @oska874
    1.client发送
    2.server接收成功
    3.server发送
    4..client接收成功
    5.client发送(此时报错)
    oska874
        9
    oska874  
       2015-07-31 14:56:18 +08:00   ❤️ 1
    @2a 我这边改了之后没问题,先启动server,再client,发-收-发-收....
    还有你注意下换行

    wkdhf233
        10
    wkdhf233  
       2015-07-31 15:02:15 +08:00   ❤️ 1
    你把客户端的s = socket.socket()丢循环外边,第一轮通信完毕都close了
    第二轮还s.connect((host,port)) 个条毛。。s都没有了。。
    2a
        11
    2a  
    OP
       2015-07-31 15:12:55 +08:00
    @wkdhf233 理解了,刚在CSDN找到答案改完了代码,你就来了:p。眼瞎没看到s.close()!被自己蠢哭,哈哈。。。直接循环import之后的代码就行了,谢谢!
    2a
        12
    2a  
    OP
       2015-07-31 15:15:57 +08:00
    @oska874 谢谢!是我把close放到循环里了!:>
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2031 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 01:18 · PVG 09:18 · LAX 18:18 · JFK 21:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.