1. <tfoot id='WohCA'></tfoot>
  2. <i id='WohCA'><tr id='WohCA'><dt id='WohCA'><q id='WohCA'><span id='WohCA'><b id='WohCA'><form id='WohCA'><ins id='WohCA'></ins><ul id='WohCA'></ul><sub id='WohCA'></sub></form><legend id='WohCA'></legend><bdo id='WohCA'><pre id='WohCA'><center id='WohCA'></center></pre></bdo></b><th id='WohCA'></th></span></q></dt></tr></i><div id='WohCA'><tfoot id='WohCA'></tfoot><dl id='WohCA'><fieldset id='WohCA'></fieldset></dl></div>
      <bdo id='WohCA'></bdo><ul id='WohCA'></ul>
    <legend id='WohCA'><style id='WohCA'><dir id='WohCA'><q id='WohCA'></q></dir></style></legend>

    <small id='WohCA'></small><noframes id='WohCA'>

    1. 通过 Socket 发送和接收数组

      时间:2023-09-28

    2. <small id='Ca7kS'></small><noframes id='Ca7kS'>

      <i id='Ca7kS'><tr id='Ca7kS'><dt id='Ca7kS'><q id='Ca7kS'><span id='Ca7kS'><b id='Ca7kS'><form id='Ca7kS'><ins id='Ca7kS'></ins><ul id='Ca7kS'></ul><sub id='Ca7kS'></sub></form><legend id='Ca7kS'></legend><bdo id='Ca7kS'><pre id='Ca7kS'><center id='Ca7kS'></center></pre></bdo></b><th id='Ca7kS'></th></span></q></dt></tr></i><div id='Ca7kS'><tfoot id='Ca7kS'></tfoot><dl id='Ca7kS'><fieldset id='Ca7kS'></fieldset></dl></div>
      <tfoot id='Ca7kS'></tfoot>
        <legend id='Ca7kS'><style id='Ca7kS'><dir id='Ca7kS'><q id='Ca7kS'></q></dir></style></legend>

                <tbody id='Ca7kS'></tbody>
              • <bdo id='Ca7kS'></bdo><ul id='Ca7kS'></ul>
                本文介绍了通过 Socket 发送和接收数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                是否可以使用 Python 通过 UDP 套接字发送数组?我正在使用 Python 2.5 并尝试发送一个简单的数组,但它不起作用.它可以成功发送数组,但是当我尝试用数组的一个项目打印它时,程序崩溃了.我不确定错误是什么,因为我采取了将数据转换为数组的预防措施,但它不起作用.希望我尽可能清楚地解释了这个问题.我将不胜感激!

                Is it possible to send an array through UDP Sockets using Python? I am using Python 2.5 and trying to send a simple array but it's not working. It can send the array successfully but when I try to print it with an item of the array the program crashes. I'm not sure what the error is as I take the precaution of converting the data into an array but it's not working. Hope I explained the problem as clearly as possible. I would appreciate the help!

                # Client program
                
                from socket import *
                import numpy
                from array import*
                
                # Set the socket parameters
                host = "localhost"
                port = 21567
                buf = 4096
                addr = (host,port)
                
                # Create socket
                UDPSock = socket(AF_INET,SOCK_DGRAM)
                
                def_msg = "===Enter message to send to server===";
                print "
                ",def_msg
                a = array('i',[1,3,2])
                # Send messages
                while (1):
                    data = raw_input('yes or now')
                    if data!= "yes":
                        break
                    else:
                        if(UDPSock.sendto(a,addr)):
                            print "Sending message"
                
                # Close socket
                UDPSock.close()
                
                
                
                # Server program
                
                from socket import *
                
                # Set the socket parameters
                host = "localhost"
                port = 21567
                buf = 4096
                addr = (host,port)
                
                # Create socket and bind to address
                UDPSock = socket(AF_INET,SOCK_DGRAM)
                UDPSock.bind(addr)
                
                # Receive messages
                while 1:
                    data,addr = UDPSock.recvfrom(buf)
                    L = eval(data)
                    if not data:
                        print "Client has exited!"
                        break
                    else:
                        print "
                Received message '", L[1],"'"
                
                # Close socket
                UDPSock.close()
                

                推荐答案

                eval 做的事情与你想的完全不同.

                eval is doing something completely different than what you think.

                要通过网络发送数据,您需要将其序列化为字节数组,然后反序列化返回.在 Python 中,大多数对象的序列化可以通过 pickle 模块完成:

                To send data over network, you need to serialize it into an array of bytes, then deserialize it back. In Python, serialization of most objects can be done via pickle module:

                if (UDPSock.sendto( pickle.dumps(a), addr)):
                

                反序列化:

                data,addr = UDPSock.recvfrom(buf)
                L = pickle.loads(data)
                print repr(L) # prints array('i', [1, 3, 2])
                

                这篇关于通过 Socket 发送和接收数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:UDP声音传输:播放的声音有很大的噪音 下一篇:使用 select() 监听 tcp 和 udp 消息

                相关文章

                <small id='QHepB'></small><noframes id='QHepB'>

                <legend id='QHepB'><style id='QHepB'><dir id='QHepB'><q id='QHepB'></q></dir></style></legend>
                1. <tfoot id='QHepB'></tfoot>
                      <bdo id='QHepB'></bdo><ul id='QHepB'></ul>
                    <i id='QHepB'><tr id='QHepB'><dt id='QHepB'><q id='QHepB'><span id='QHepB'><b id='QHepB'><form id='QHepB'><ins id='QHepB'></ins><ul id='QHepB'></ul><sub id='QHepB'></sub></form><legend id='QHepB'></legend><bdo id='QHepB'><pre id='QHepB'><center id='QHepB'></center></pre></bdo></b><th id='QHepB'></th></span></q></dt></tr></i><div id='QHepB'><tfoot id='QHepB'></tfoot><dl id='QHepB'><fieldset id='QHepB'></fieldset></dl></div>