0

I have written a client-server python program where the client can send the data to the server. 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.

Here are my client and server code

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

How do I fix this problem so that the client can send the data to the server without any error? Thank You in advance.

  • ideally you want to create the socket in the client the same way as the server.. meaning using `s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)` in the client.py code to make sure the correct protocol is used.. – Raymond Nijland May 19 '19 at 22:14
  • @RaymondNijland Yeah one of my friend suggested me that and I tried it, but it didn't solve the issue. – Suryavanshi Virendrasingh May 19 '19 at 22:16
  • ok then it's maybe a local or some firewall which is in the way? – Raymond Nijland May 19 '19 at 22:17
  • The access point that I am using is a hotspot from my mobile without any proxy. Still, if that might be the case can you please elaborate how can I check if the issue is due to the firewall? – Suryavanshi Virendrasingh May 19 '19 at 22:20
  • *"The access point that I am using is a hotspot from my mobile without any proxy"* well it can be a firewall at the mobile phone provider i assume you are tethering your mobile phone (edge/gprs/3g/4g) data? – Raymond Nijland May 19 '19 at 22:33
  • @RaymondNijland No, I am not running a server on mobile phone connection I am just providing an access point through mobile phone connection. I have run various socket connections this way for my raspi, it works and I also tried to run it through connecting to the same wifi to both server and client but the error remains same. – Suryavanshi Virendrasingh May 19 '19 at 22:35
  • No, I am not tethering my mobile phone I am providing [hotspot](https://en.wikipedia.org/wiki/Hotspot_(Wi-Fi)) through it to both the client and server. – Suryavanshi Virendrasingh May 19 '19 at 22:40
  • well iam out of options.. but it's pretty clear that or the server or client is cleary not in the same wifi network then.. – Raymond Nijland May 19 '19 at 22:40
  • @RaymondNijland Client and Server both are connected to the same wifi network. – Suryavanshi Virendrasingh May 19 '19 at 22:41
  • then i would suggest using `ping ip` to see if that works, to check if the server and client machine can ping one and other.. To rule out network problems between them. – Raymond Nijland May 19 '19 at 22:44
  • Yeah, both server and client are able to ping to each other and for confirmation, I also checked in Network that both are connected to the same wifi. – Suryavanshi Virendrasingh May 19 '19 at 22:48
  • 1
    @RaymondNijland It was a firewall issue. – Suryavanshi Virendrasingh May 20 '19 at 08:05

1 Answers1

0

The error resolved.
It was due to the firewall issue as @RaymondNijland was mentioning, Thanks.
I added the rule in the firewall to allow the following port for Socket Connection and it worked.

sudo ufw allow 10106