为什么客户机/服务器机制(使用套接字)不起作用?

2024-04-26 23:27:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有免费帐户www.pythonanwhere.com。我有一个用c++编写的服务器

some_space::socket_server::socket_server(unsigned int port):
     m_port(port),
     m_tcp_fd(0),
     m_udp_fd(0),
     m_newfd(0)
 {
     m_addr.sin_family = AF_INET;
     m_addr.sin_addr.s_addr = htonl(INADDR_ANY);
     m_addr.sin_port = htons(m_port);
 }

 void some_space::socket_server::set_port(unsigned int port)
 {
     assert(port != 0);
     m_port = port;
 }

 int some_space::socket_server::create_tcp_connection()
 {
         m_tcp_fd = socket(AF_INET, SOCK_STREAM, 0); 
         if(m_tcp_fd < 0) {
             perror("Error: Cannot set up the communication");
             return -1; 
         }   
         int status = bind(m_tcp_fd, (struct sockaddr *)&m_addr, sizeof(m_addr));       if(status < 0) {
             perror("Error: Cannot set up the communication");
             return -1;
         }
         status = listen(m_tcp_fd, 5);
         if(status == 0) {
             m_newfd = accept(m_tcp_fd, (struct sockaddr*)NULL, NULL);// ####################### The code freezes here (on the accept)
             if(m_newfd != -1) {
                 return m_newfd;
             }
             perror("Error: Cannot accept the connection");
             return -1;
         }
         perror("Error: The port cannot be listened");
         return -1;
     }

其中m_port = 9999 这条鳕鱼被吃掉了。pythonanwhere.com服务器终端。在

总的来说。在

^{pr2}$

另外,我在本地的pc上有一个用python编写的客户机程序。在

#!/usr/bin/env python

 import socket

 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)     
 sock.connect(('<username>.pythonanywhere.com', 9999))
 sock.send('hello, world!')     
 data = sock.recv(1024)     
 print "receive >>> %s" % data     
 sock.close()

但问题是客户机无法与服务器连接,它总是等待。哪里有问题?在


Tags: thereturnifserverportstatuserrorsocket
1条回答
网友
1楼 · 发布于 2024-04-26 23:27:08

PythonAnywhere dev here:PythonAnywhere只支持使用pythonwsgi协议的web应用程序,该协议覆盖了几乎所有主要的Python web框架(Django、web2py、Flask、Bottle等),但不能与您自己的基于C的服务器一起工作。在

相关问题 更多 >