python - 将C套接字程序转换为python

标签 python c sockets

我一直在尝试将 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()

最佳答案

您没有正确地将 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)

关于python - 将C套接字程序转换为python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22832617/

相关文章:

python - 无法在 ubuntu 上使用 pip 安装 mysqlclient

Java程序阻塞端口?

c - 如何确定客户端是否已连接到我的套接字?

c# - C#、C/C++ 或 Objective-C 中的眼动追踪库

c - 错误 : redeclaration of enumerator for contiki os libtomcrypt library

sockets - Python 3.4,socket.error 已弃用,新的​​等价物?

python - 如何从 curl 获取 session ID 并在下一个 GET 请求中再次使用它

python - 运算符优先级对字符串的工作方式与对数字的工作方式相同吗?

python - Pandas Dataframe 上两列的条件更新

c++ - 对以下 c/c++ 解决方案的逻辑解释