简体   繁体   中英

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.

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. 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, …

But an even simpler way to is to just use str and int , if all you ever want to send is ints:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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