简体   繁体   English

客户端套接字无法连接到服务器套接字,[Errno 32]管道损坏错误

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

I have written a client-server python program where the client can send the data to the server. 我编写了一个客户端 - 服务器python程序,客户端可以将数据发送到服务器。 But when the client is trying to connect with the server I am getting the following error. 但是当客户端尝试连接服务器时,我收到以下错误。

[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

I tried the following solutions Broken Pipe error and How to prevent Broken pipe error but none of them solved the issue. 我尝试了以下解决方案Broken Pipe错误如何防止管道错误但没有解决问题。

Here are my client and server code 这是我的客户端和服务器代码

client.py 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 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.")

How do I fix this problem so that the client can send the data to the server without any error? 如何解决此问题,以便客户端可以将数据发送到服务器而不会出现任何错误? Thank You in advance. 先感谢您。

The error resolved. 错误已解决。
It was due to the firewall issue as @RaymondNijland was mentioning, Thanks. 这是由于@RaymondNijland提到的防火墙问题,谢谢。
I added the rule in the firewall to allow the following port for Socket Connection and it worked. 我在防火墙中添加了规则以允许以下端口用于套接字连接并且它可以工作。

sudo ufw allow 10106

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM