浏览器能连接到原始套接字吗?

0 投票
0 回答
47 浏览
提问于 2025-04-12 10:15

我们被布置了一个作业,要做一个井字棋游戏,采用服务器-客户端架构。我的教授希望服务器使用原始套接字来构建(比TCP/UDP还要低级),而客户端则是在浏览器上。这样做真的可行吗? 作业要求

我知道作业要求中提到了TCP/IP,但我后来问了教授,她说要用更低级的原始套接字。她给了我一个使用TCP的服务器示例(她不希望我们使用这个):

# Import the required libraries
from socket import *

# Listening port for the server
serverPort = 12000

# Create the server socket object
serverSocket = socket(AF_INET,SOCK_STREAM)

# Bind the server socket to the port
serverSocket.bind(('',serverPort))

# Start listening for new connections
serverSocket.listen(1)


# For python 2.x use the following syntax for displaying strings:
print 'The server is ready to receive messages'

# For python 3.x use the following syntax for displaying strings:
#print('The server is ready to receive messages')


while 1:
        # Accept a connection from a client
    connectionSocket, addr = serverSocket.accept()

        ## Retrieve the message sent by the client

        # For python 2.x use the following (no need to convert from byte string to unicode):
        sentence = connectionSocket.recv(1024)

        # For python 3.x use the following
        # (explicit conversion from byte string to unicode using .encode() method):
    #sentence = connectionSocket.decode().recv(1024)
    
        #Modify the message
    capitalizedSentence = sentence.upper()

        ## Send the modified message back to the client
    
        # For python 2.x use the following (no need to convert from unicode to byte string):
        connectionSocket.send(capitalizedSentence)

        # For python 3.x use the following
        # (explicit conversion from unicode to byte string using .encode() method):
    #connectionSocket.send(capitalizedSentence.encode())

        # Close the connection
    connectionSocket.close()

所以她希望我们能做到比这个更低级。

回到我的问题,这样做可能吗?我查了一些资料,发现浏览器似乎无法与原始套接字进行通信。但她说她做过,所以我现在有点困惑。

0 个回答

暂无回答

撰写回答