只需从套接字中获取最后一个值 [英] Just get one last value from socket

查看:170
本文介绍了只需从套接字中获取最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每秒都会在一边发送数据给socket,但是我可以随时读取另一边的数据。以下是作者:

  from设置import Config 

filename = Config.NAVIGATION_SOCKET_FILE

client = socket.socket(socket.AF_UNIX,socket.SOCK_DGRAM)
client.settimeout(None)
而True:
try:
client.connect(filename)
break
除了例外:
continue
messages = [$ GPRMC,125504.049,A,5542.2389,N,03741.6063,E,0.06,25.82,200906 ,,, * 17 ,
$ GPRMC,155604.049,A,5542.2389,N,03741.6063,E,0.06,25.82,200906 ,,, * 19,]
而真:
msg =随机。 (message)
client.send(msg)
print msg
time.sleep(1)

这里是读者:
$ b $ pre $ navigation_socket = socket.socket(socket.AF_UNIX,socket.SOCK_DGRAM )
如果os.path.exists(app.config ['NAVIGATION_SOCKET_FILE']):
os.remove(app.config ['NAVIGATION_SOCKET_FILE'])
navigation_socket.bind(app.config ['导航_SOCKET_FILE'])

class NavigationInfo(restful.Resource):
def get(self):
msg = navigation_socket.recv(1024)
regex = re。 (?P<时间> \\ d {6} \\\\\\\\\\\\\\' (ΔP< latitude> \ d {4} \\\\\\\\\\\\\\\\\\\\\' (ΔP E | W),
r(ΔP< hor_speed> \d + .d +),(ΔP< track_angle> ($ P $ lt; $ P $ lt; $ P $ lt; $ P $ lt; $ P $ lt; (ΔP< magnetic_decl_direction> \d)?,
r(?P A | D | E | N)?\ *(?P \\ d))
result = regex.match(msg)
navigation_info = result.groupdict()
return因此,第一个问题是编写器只是在缓冲区满时停止将数据写入套接字(至少是这就是我所看到的),当我请求另一边的数据,这是太旧了。

只能存储一个值在缓冲区,然后重写它?或者我可能弄错了所有的东西?

解决方案

我认为您正在使用解决方案。



而不是推送消息,而不是拉消息?

您的服务器可能看起来像:


  1. 等待连接

  2. 发送随机消息

  3. 转到步骤1
  4. li>

您的客户端可能只需要连接到服务器,当他需要一个消息时。

在你的情况下,连接始终打开,在我的解决方案中,套接字仅在需要时打开。并在邮件发送后立即关闭。

I send data to socket on one side every second, but I can read that data on another side in any moment. Here's the writer:

from settings import Config

filename = Config.NAVIGATION_SOCKET_FILE

client = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
client.settimeout(None)
while True:
    try:
        client.connect(filename)
        break
    except Exception:
        continue
messages = ["$GPRMC,125504.049,A,5542.2389,N,03741.6063,E,0.06,25.82,200906,,,*17",
            "$GPRMC,155604.049,A,5542.2389,N,03741.6063,E,0.06,25.82,200906,,,*19",]
while True:
    msg = random.choice(messages)
    client.send(msg)
    print msg
    time.sleep(1)

And here's reader:

navigation_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
if os.path.exists(app.config['NAVIGATION_SOCKET_FILE']):
        os.remove(app.config['NAVIGATION_SOCKET_FILE'])
navigation_socket.bind(app.config['NAVIGATION_SOCKET_FILE'])

class NavigationInfo(restful.Resource):
    def get(self):
        msg = navigation_socket.recv(1024)
        regex = re.compile(r"^\$GPRMC,(?P<time>\d{6}\.\d{3}),(?P<status>A|V),"
                           r"(?P<latitude>\d{4}\.\d{4}),(?P<lat_n_s>N|S),"
                           r"(?P<longitude>\d{5}\.\d{4}),(?P<long_e_w>E|W),"
                           r"(?P<hor_speed>\d+.\d+),(?P<track_angle>\d+.\d+),"
                           r"(?P<date>\d{6}),(?P<magnetic_declination>\d+\.\d+)?,"
                           r"(?P<magnetic_decl_direction>\d)?,"
                           r"(?P<mode>A|D|E|N)?\*(?P<checksum>\d\d)")
        result = regex.match(msg)
        navigation_info = result.groupdict()
        return navigation_info

So the first problem is that writer just stops writing data to socket when buffer is full (at least that's what I see) and when I request data on the other side, it's too old.

Can just I store one value in buffer and then rewrite it? Or maybe I'm getting it all wrong?

解决方案

I think that you are using the solution in reverse.

Instead of pushing messaging, while not pulling messages ?

Your server may look like:

  1. Wait for a connection
  2. Give a random message
  3. go to step 1

And your client may just connect to the server when he needs a message.

In your case, the connection is "opened all the time", in my solution the socket is opened only when needed. and closed right after the message is delivered.

这篇关于只需从套接字中获取最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆