简体   繁体   English

如何在终端的UDP连接中发送和操作整数

[英]How to send and manipulate an integer in a UDP connection in the terminal

So im having trouble with this one portion of my program. 所以我在程序的这一部分遇到了麻烦。 Right now with my current code i can connect and communicate with another computer via the terminal but i am unable to send an integer and have it be recognized as such. 现在,使用我当前的代码,我可以通过终端连接另一台计算机并与之通信,但是我无法发送整数并且无法将其识别出来。

I need to be able to send a number to a server, and get the server to manipulate the number (eg add 1), and then return the result to client and printout the result on the server side. 我需要能够将数字发送到服务器,并让服务器操纵该数字(例如,加1),然后将结果返回给客户端并在服务器端打印出结果。

I cannot for the life of me get it to recognize this communication as an integer instead of a string! 我一生都无法理解这种通信是整数而不是字符串!

Here is how i connect in the terminal Server side: 这是我在终端服务器端连接的方式:

python ./udpEchoServer.py -s 4004

And here is how i connect in the terminal Client side: 这是我在终端客户端连接的方式:

python ./udpEchoServer.py -c [IP ADDRESS OF THE SERVER] 4004

Here is my code so far: 到目前为止,这是我的代码:

#! /usr/bin/env python

# Client and server for udp (datagram) echo.
#
# Usage: udpecho -s [port]            (to start a server)
# or:    udpecho -c host [port] <file (client)

import sys
from socket import *

ECHO_PORT = 50000 + 7
BUFSIZE = 1024

def main():
    if len(sys.argv) < 2:
        usage()
    if sys.argv[1] == '-s':
        server()
    elif sys.argv[1] == '-c':
        client()
    else:
        usage()

def usage():
    sys.stdout = sys.stderr
    print 'Usage: udpecho -s [port]            (server)'
    print 'or:    udpecho -c host [port] <file (client)'
    sys.exit(2)

def server():
    if len(sys.argv) > 2:
        port = eval(sys.argv[2])
    else:
        port = ECHO_PORT
    s = socket(AF_INET, SOCK_DGRAM)
    s.bind(('', port))
    print 'udp echo server ready'
    while 1:
        data, addr = s.recvfrom(BUFSIZE)
        print 'server received %r from %r' % (data, addr)
        s.sendto(data, addr)

def client():
    if len(sys.argv) < 3:
        usage()
    host = sys.argv[2]
    if len(sys.argv) > 3:
        port = eval(sys.argv[3])
    else:
        port = ECHO_PORT
    addr = host, port
    s = socket(AF_INET, SOCK_DGRAM)
    s.bind(('', 0))
    print 'udp echo client ready, reading stdin'
    while 1:
        line = sys.stdin.readline()
        if not line:
            break
        s.sendto(line, addr)
        data, fromaddr = s.recvfrom(BUFSIZE)
        print 'client received %r from %r' % (data, fromaddr)

main()

UDP messages are just bytes. UDP消息只是字节。 If you want them to pass anything else, you have to serialize it on the sending side, and deserialize it on the receiving side. 如果希望他们传递其他任何内容,则必须在发送方序列化 ,并在接收方反序列化 (Sometimes this is called marshaling or encoding instead of serializing.) (有时这称为封送处理或编码,而不是序列化。)

A common way to serialize data is JSON—it handles smallish ints, floats, strings, lists of dicts of floats, … 序列化数据的一种常用方法是JSON-它处理较小的整数,浮点数,字符串,浮点数字典列表……

But an even simpler way to is to just use str and int , if all you ever want to send is ints: 但是,更简单的方法是只使用strint ,如果您要发送的只是int:

num = 5
s.sendto(str(num), add)

… and to receive a number: …并收到一个号码:

data, addr = s.recvfrom(BUFSIZE)
num = int(data)

In fact, you're already effectively doing this on the client side—you read a line of input, as a string, and just send it. 实际上,您已经在客户端有效地做到了—您将输入行作为字符串读取,然后将其发送。 So you don't need to add anything there, just on the server: 因此,您无需在服务器上添加任何内容:

data, addr = s.recvfrom(BUFSIZE)
num = int(data)
print 'server received %r from %r' % (data, addr)
data = str(num + 1)
s.sendto(data, addr)

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

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