【问题标题】:Convert C socket program to python将 C 套接字程序转换为 python
【发布时间】:2014-04-03 08:59:07
【问题描述】:

我一直在尝试将 C 中的此接收器代码转换为 python.. 我使用的python代码似乎没有收到任何东西.. 发送方使用的C代码

        sendto(sid,buffer,1023,0,(struct sockaddr *)&saddr,sizeof(saddr));

以下代码转换成python时出现什么问题。

        #include<stdio.h>
        #include<sys/types.h>
        #include<sys/socket.h>
        #include<netinet/in.h>
        #include<errno.h>
        #include<unistd.h>
        #include<stdlib.h>
        int main(char *argv[])
        {
        socklen_t sid,clen;
        char buffer[1024];
        struct sockaddr_in saddr,caddr;
        int n;
        sid=socket(AF_INET,SOCK_DGRAM,0);
        if(sid<0)
            perror("socket_create");
        bzero((char*)&saddr,sizeof(saddr));
        saddr.sin_family=AF_INET;
        saddr.sin_port=htons(12346);
        saddr.sin_addr.s_addr=INADDR_ANY;
        if(bind(sid,(struct sockaddr *)&saddr,sizeof(saddr))<0)
            perror("socket_bind");
        while(1)
        {
        clen=sizeof(caddr);
        bzero(buffer,1024);
        n=recvfrom(sid,buffer,1023,0,(struct sockaddr*)&caddr,&clen);
        if(n<0)
            perror("receive");
        printf("Array : %s\n", buffer);
        }
        close(sid);
        return 0;
        }

我试过的 Python 代码是

            import socket
            import sys
            from thread import *

            HOST = "127.0.0.1"   # Symbolic name meaning all available interfaces
            PORT = 12346 # Arbitrary non-privileged port

            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            print 'Socket created'

            #Bind socket to local host and port
            try:
                s.bind((HOST, PORT))
            except socket.error , msg:
                print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
                sys.exit()

            print 'Socket bind complete'

            #Start listening on socket
            s.listen(10)
            print 'Socket now listening'

            #Function for handling connections. This will be used to create threads
            def clientthread(conn):
                #Sending message to connected client
                conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string

                #infinite loop so that function do not terminate and thread do not end.
                while True:

                    #Receiving from client
                    data = conn.recv(1024)
                    reply = 'OK...' + data
                    if not data:
                        break

                    conn.sendall(reply)

                #came out of loop
                conn.close()

            #now keep talking with the client
            while 1:
                #wait to accept a connection - blocking call
                conn, addr = s.accept()
                print 'Connected with ' + addr[0] + ':' + str(addr[1])

                #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
                start_new_thread(clientthread ,(conn,))

            s.close()

【问题讨论】:

标签: python c sockets


【解决方案1】:

您没有正确地将 C 代码转换为 Python。在 C 中,一个 UDP 套接字由

创建
    sid=socket(AF_INET,SOCK_DGRAM,0);

而在 Python 中,您是通过

创建 TCP 套接字的
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

并且使用 listen()accept() 足以满足 TCP 连接 - 当您的发件人使用 UDP 时,这无法正常工作。

您只需将 C 代码直接转换为 Python,例如。 g.

    n=recvfrom(sid,buffer,1023,0,(struct sockaddr*)&caddr,&clen);

        buffer, caddr = s.recvfrom(1023)

【讨论】:

    猜你喜欢
    • 2013-02-08
    • 2014-01-19
    • 1970-01-01
    • 2016-05-14
    • 2011-11-22
    • 1970-01-01
    • 2011-06-06
    • 2010-09-30
    • 2012-08-27
    相关资源
    最近更新 更多