客户端服务器套接字编程无响应

时间:2014-03-19 09:25:26

标签: python sockets

我的问题是,这个示例Web套接字python程序没有响应。我也尝试使用本地主机作为服务器地址我放IP但仍然结果是相同的请帮助

服务器代码: 导入套接字

server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
server_address=("192.168.1.105",8081)

print ('Starting up on %s port %s' % server_address)

response=None

try :

    server.bind(server_address)
    server.listen(5)
    connection,client_address=server.accept()
    print ("connection from" % connection.getpeername())

    data=connection.recv(4096)

    if data:
        print ("recived ", repr(data))

        data=data.rstrip()
        connection.send("%$\n%$\n%$\n" % ('-'*80,data.center(80),'-'*80) )

    connection.shutdown(socket.SHUT_RD | socket.SHUT_WR) 
    connection.close()
    print ('Connection closed()')

    server.close()

except socket.error as msg:
    response=msg


print (response)

客户代码:

import socket


s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("192.168.1.105",8080))
v=bytes("HI man","utf-8")
s.send(v);
data=s.recv(1024)
s.close()
print(data)

输出: server =在192.168.1.105端口8081上启动 客户= b' '

(Python版本3.3)

3 个答案:

答案 0 :(得分:1)

我正在使用Python 2.7,代码适用于我,如下所示。

import socket

server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
server_address=("127.0.0.1",8081)

print ('Starting up on %s port %s' % server_address)

response=None

try :

    server.bind(server_address)
    server.listen(5)
    connection,client_address=server.accept()
    print ("connection from " + str(connection.getpeername()))

    data=connection.recv(4096)

    if data:
        print ("recived ", repr(data))

        data=data.rstrip()
        connection.send("%s\n%s\n%s\n" % ('-'*80,data.center(80),'-'*80) )

    connection.shutdown(socket.SHUT_RD | socket.SHUT_WR)
    connection.close()
    print ('Connection closed()')

    server.close()

except socket.error as msg:
    response=msg


print (response)

客户端

import socket


s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("127.0.0.1",8081))
v="HI man"
s.send(v);
data=s.recv(1024)
s.close()
print(data)

您的代码中存在一些错误,例如在8081上收听并在8080上发送数据,我也会更正其他错误。请试试这个必须在你的工作。

答案 1 :(得分:0)

您尝试连接到端口8080而不是服务器的8081端口

答案 2 :(得分:0)

我也在使用Python 3.3。为了解决您的问题,我们首先让您的服务器接受来自任何NIC的连接,并让您的客户端通过环回IP地址连接到服务器。似乎端口8080不可用。请尝试修改您的代码并让我知道

服务器:

import socket

server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
server_address=('',5000)

print ('Starting up on',server_address)

response=None

try :

    server.bind(server_address)
    server.listen(5)
    connection,client_address=server.accept()
    print ("connection from ",client_address)

    data=connection.recv(4096)


    if data:
        data=data.decode()

        print ("recived: ", data)

        data=data.rstrip()
        reply="%s\n%s\n%s\n" % ('-'*80,data.center(80),'-'*80)
        connection.sendall(reply.encode())


    connection.close()
    print ('Connection closed()')

    server.close()

except socket.error as msg:
    response=msg

if not response:
    print("Program success")
else:
    print (response)

客户端

import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("127.0.0.1",5000))
v="HI man"
s.send(v.encode());
data=s.recv(1024)
s.close()
print(data.decode())