简体   繁体   English

TCP Python套接字服务器响应

[英]TCP Python Socket Server Response

I'm writing a TCP python script that will act as a server and retrieve a temperature reading from a machine (the client). 我正在编写一个TCP python脚本,它将充当服务器并从计算机(客户端)中检索温度读数。 I need to pass a command to the client from the server and listen for an output of the response. 我需要将命令从服务器传递给客户端,并监听响应的输出。 I successfully reach the cmd definition line, but when s.accept() is called I'm left hanging with no response from the client. 我成功地到达了cmd定义行,但是当调用s.accept()时,我一直处于悬而未决的状态,而客户端没有响应。

Server.py Server.py

import socket
port = 7777
ip = raw_input('192.168.62.233')

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((ip, port))
s.listen(1)
print "waiting on port: ", port

while True:
    cmd = raw_input('KRDG? A[term]')#command send to client

    conn, addr = s.accept()

    s.send(cmd)

    print "It sent"

    data = conn.recv(4096)

    print "Received:", data, " from address ", addr

Edit: 编辑:

I believe you're correct, I should consider my code the client and temperature readout at the server. 我相信您是正确的,我应该考虑将我的代码作为客户端和服务器上的温度读数。 I do now get left hanging after "here 2" when I go to s.recv(). 现在,当我去s.recv()时,我确实会挂在“这里2”之后。

Client.py 客户端

import socket

ip = '192.168.62.233'
port = 7777              # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, port))

print 'here'
s.send('KRDG? A[term]')

print 'here 2'
data = s.recv(4096)
print 'here 3'
s.close()
print 'Received', repr(data)

Usually, a TCP server accept will block while it waits for a client to connect. 通常,TCP服务器接受将在等待客户端连接时阻塞。 Have you checked to make sure that you client can and is connecting? 您是否检查过以确保客户端可以并且正在连接? You could use a tool like tcpdump or similar to watch the network activity and make sure the client is connecting. 您可以使用tcpdump之类的工具或类似工具监视网络活动,并确保客户端正在连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM