簡體   English   中英

客戶端套接字無法連接到服務器套接字,[Errno 32]管道損壞錯誤

[英]client socket not able to get connected to server socket, [Errno 32] Broken pipe error

我編寫了一個客戶端 - 服務器python程序,客戶端可以將數據發送到服務器。 但是當客戶端嘗試連接服務器時,我收到以下錯誤。

[Errno 110] Connection timed out
Sending Key to Server
Traceback (most recent call last):
   File "client.py", line 43, in <module>
       s.send(str(id))
socket.error: [Errno 32] Broken pipe

我嘗試了以下解決方案Broken Pipe錯誤如何防止管道錯誤但沒有解決問題。

這是我的客戶端和服務器代碼

client.py

import socket
import os
import subprocess
from optparse import OptionParser
from random import randint 

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   
    print "Socket has been successfully created"
except socket.error as err:
    print "socket creation failed with error %s" %(err)

# The Manager Address and port 

host_ip='192.168.43.123'
port =10106

# Generates a random number say xxxx then its client id becomes 'clxxxx' and home directory made at the server as '/home/clxxxx' with permissions 700
def random_with_N_digits(n):
    range_start = 10**(n-1)
    range_end = (10**n)-1
    return randint(range_start, range_end)

id=random_with_N_digits(4)
id="cl"+ str(id)

# Looks for a public key in .ssh folder if temp.pub not present. If not found generates a ssh public private key and sends it to manager which then copies it to the server
subprocess.call(["bash","keygen.sh"])


#s = socket.socket()

try:
    s.connect((host_ip,port))
    print "the socket has successfully connected to Backup Server IP == %s" %(host_ip)

except socket.error as err:
    print err

f = open('temp.pub','r')
print "Sending Key to Server"

j = "-"
s.send(str(id))
l=f.read(8192)
while(l):
    print 'Sending...'
    s.send(l)
    l = f.read(8192)

try:
    client_id=s.recv(1024)
    data=s.recv(12)
    ip=s.recv(24)
    print  client_id,
    print  data, ip
except:
    print "An Unknown Error Occurred!"

f.close()

# Writes the parameters of client in the file 'backup_dir.txt'
with open('backup_dir.txt','w') as the_file:
    the_file.write(client_id)
    the_file.write('\n')
    the_file.write(data)
    the_file.write('\n')
    the_file.write(ip)
    the_file.write('\n')
f.close()


s.close()

server.py

import socket
import subprocess
import os

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   
    print "Socket has been successfully created"
except socket.error as err:
    print "socket creation failed with error %s" %(err)

port = 10106

s.bind(('', port))
print("socket binded to %s port" %port)

s.listen(10)
print("socket is listening")

while(True):
    print("waiting for a connection")
    c, addr = s.accept()
    print("Got a connection from", addr,c)
    clientID =(c.recv(8192))
    key =(c.recv(8192))
    print clientID
    print key

    with open("temp.pub", 'w') as fp:
        fp.write(key)
    note=subprocess.check_output("./test_user.sh "+ clientID, shell=True)
    note = str(note)
    print(len(note))
    flag, path, serverIP = note.split(":")
    print(flag)
    print(path)
    print(serverIP)
    if flag:
        c.send(clientID)
        c.send(path)
        c.send(serverIP)
        os.remove("temp.pub")
    else:
        c.send("Unable to add Client.")

如何解決此問題,以便客戶端可以將數據發送到服務器而不會出現任何錯誤? 先感謝您。

錯誤已解決。
這是由於@RaymondNijland提到的防火牆問題,謝謝。
我在防火牆中添加了規則以允許以下端口用於套接字連接並且它可以工作。

sudo ufw allow 10106

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM