一个fi中两个子进程的套接字通信

2024-06-28 10:59:40 发布

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

我在两个文件(客户机和服务器)之间进行信息交换,但我无法将代码实现为一个包含两个子进程的文件。到目前为止,有一个父级生成一个与另一个客户端通信的子级。在

我试图将所有的套接字绑定放入子进程,但没有成功。它只打印前两行。 我应该找什么?在

谢谢你的帮助

在服务器.py(工程)

#!/usr/bin/python           # This is server.py file
import socket               # Import socket module
import random
import os

g=101 # publicly known
p=103 # publicly known

x= random.randint(1,100) # Alice's random number
y= random.randint(1,100) # Bob's random number

aliceSends = (p**x)%g
bobComputes = (aliceSends**y)%g
bobSends = (p**y)%g

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection up to 5.

print 'Qsocket: Using sockets for IPC'
print 'Diffie-Hellman Parameters p=103 and g=101'
print 'Parent: pid= ',os.getpid(),'\n'

while True:
   c, addr = s.accept()     # Establish connection with client.
   print 'Alice Accepted connection from Bob\n'

   if os.fork() == 0:
      #sends first message
      print 'Alice: pid= ',os.getpid(), ', my Parent pid= ',os.getppid()
      print 'Alice Random Secret: ' + str(x)
      print 'Alice Public T: ' + str(aliceSends)
      print 'Alice send to Bob its Public T: ',aliceSends,'\n'
      c.send(str(aliceSends))

      #receieves message
      bobSends = int(c.recv(1024))

      print '<< Alice Got Bob Public T: ',str(bobSends)
      aliceComputes = (bobSends**x)%g
      print 'Alice-to-Bob Shared Secret: << (' + str(aliceComputes) + ') >>'

   c.close()                # Close the connection

print 'Parent: Alice and Bob exited'

在客户端.py(工程)

^{pr2}$

下面是上面的压缩代码,与我希望它做的类似,但它挂在服务器调用上,永远无法到达客户端: 客户服务.py(固定)

#!/usr/bin/python           # This is server.py file
import socket               # Import socket module
import random
import os

g=101 # publicly known
p=103 # publicly known

x= random.randint(1,100) # Alice's random number
y= random.randint(1,100) # Bob's random number

aliceSends = (p**x)%g
bobComputes = (aliceSends**y)%g
bobSends = (p**y)%g

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = random.randint(0,65500)                # Reserve a port for your service.

print 'Qsocket: Using sockets for IPC'
print 'Diffie-Hellman Parameters p=103 and g=101'
print 'Parent: pid= ',os.getpid(),'\n'

if os.fork() == 0:
   s = socket.socket()
   s.bind((host, port))        # Bind to the port
   s.listen(5)                 # Now wait for client connection up to 5.
   c, addr = s.accept()     # Establish connection with client.

   print 'Alice Accepted connection from Bob\n'

   #sends first message
   print 'Alice: pid= ',os.getpid(), ', my Parent pid= ',os.getppid()
   print 'Alice Random Secret: ' + str(x)
   print 'Alice Public T: ' + str(aliceSends)
   print 'Alice send to Bob its Public T: ',aliceSends,'\n'
   c.send(str(aliceSends))

   #receieves message 'thanks for thanking'
   bobSends = int(c.recv(1024))

   print '<< Alice Got Bob Public T: ',str(bobSends)
   aliceComputes = (bobSends**x)%g
   print 'Alice-to-Bob Shared Secret: << (' + str(aliceComputes) + ') >>'
   c.close()                # Close the connection
elif os.fork() ==0:
   g=101 # publicly known
   p=103 # publicly known

   s = socket.socket()
   s.connect((host, port))
   #receieves first message
   print 'Bob: Connected to Alice\n'
   print 'Bob: pid= ',os.getpid()
   print 'Bob Random Secret: ' + str(y)
   bobSends = (p**y)%g
   print 'Bob Public T: ' + str(bobSends),'\n'

   aliceSends = int(s.recv(1024))
   bobComputes = str((aliceSends**y)%g)

   print '>> Bob Got Alice Public T: ',str(aliceSends)
   print 'Bob-to-Alice Shared Secret: << (' + str(bobComputes) +') >>'
   print 'Bob send to Alice its Public T: ',bobSends


   #sends message back
   s.send(str(bobSends))
   s.close                     # Close the socket when done
else:
   #parent
   os.wait()
   # one child has exited
   os.wait()
   # both children have exited
   print 'Parent: Alice and Bob exited'

输出应如下所示:

Qsocket: Using sockets for IPC
Diffie-Hellman Parameters p=103 and g=101
Parent: pid=  5462
Alice: pid=  5463  my Parent pid=  5462 

Alice Random Secret: 64
Alice Public T:  79
Alice send to Bob its Public T:  79 

Bob: pid=  5464  my Parent pid=  5462 

Bob Random Secret: 85
Bob Public T: 62

>> Bob Got Alice Public T:  79
Bob-to-Alice Shared Secret: << (36) >>
Bob send to Alice its Public T:  62 

<< Alice Got Bob Public T:  62
Alice-to-Bob Shared Secret: <<(36)>>

Parent: Alice Exited
Parent: Bob Exited

编辑:更新代码,使所有三个版本都能工作。在


Tags: tosecretosportrandomsocketpublicpid
1条回答
网友
1楼 · 发布于 2024-06-28 10:59:40

{我的回答来自^我的回答。在

修复代码的步骤是。在

  1. 去掉while循环。没有必要,在while循环中有叉子会导致叉子炸弹。在
  2. 服务器和客户机进程应该使用单独的套接字和网络逻辑。但是,它们应该使用相同的端口号。在
  3. 父进程应该在结尾调用os.wait两次,以确保两个子进程都已退出。在

相关问题 更多 >