0

I am trying to send the image from a windows port and receive it in ubuntu using python -jupyter notebook and socket programming. Both Windows and Ubuntu are partitioned in the same laptop.

I am facing issues in identifying the right port and getting the image.

Tried both client.py method and clientserver.py methods but am continuously stuck with errors.

import socket                   # Import socket module
s = socket.socket()             # Create a socket object
host_ip = '10.1.2.107'  
port =  47136        ###21
print ('ip address', host_ip)
s.bind((host_ip, port))            # Bind to the port
s.listen(5)                     # Now wait for client connection.

print ('Server listening....')



while True:
    conn, addr = s.accept()
    print ('Got connection from', addr)
    conn.sendall(b"Hello server!")   
    data = conn.recv(1024)          
    print('Server received',  repr(data))

    filename='/home/dev/Downloads/dev.jpg'
    f = open(filename,'r')
    l = f.read(1024)
    while (l):
        conn.send(l)
        print('Sent ',repr(l))
        l = f.read(1024)
         f.close()

print('Done sending')
conn.send('Thank you for connecting')
conn.close()

# client.py

import socket                             # Import socket module

s = socket.socket()                      # Create a socket object
host = socket.gethostname()              # Get local machine name
host_ip = socket.gethostbyname(host)
port = 631                   # Reserve a port for your service.

s.connect((host, port))
s.send("Hello server!")

with open('received_file', 'w') as f:
    print ('file opened')
    while True:
        print('receiving data...')
        data = s.recv(1024)
        print('data=%s', (data))
        if not data:
           break
        # write data to a file
          f.write(data)

   f.close()
   print('Successfully get the file')
s.close()
print('connection closed')

###============================USING UDP/IP Protocol
import socket                     
import time
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)                      

host_ip = '10.1.2.207'
port =  5000  
data = "my first socket program"
#s.connect((host_ip,port))

s.sendto(b'data', (host_ip,port))
print ("send  " + data)
time.sleep(1)
while  True:    
data_recv , addr = s.recvfrom(1024)
print('Received',  data_recv)
s.close()
Pallavi D
  • 1
  • 1
  • 1
    "but am continuously stuck with errors" - that's not enough information. If you would like to get some help quicker, please include the full traceback you're getting in your post. Out of curiosity: why does your client connect to port 631 when the server is listening on port 47136? – ForceBru Aug 20 '19 at 12:11
  • here's what I have done while I waited for a response from you. I have commented the complete code and am testing part by part. I am trying to test the part conn , addr = s.accept() – Pallavi D Aug 21 '19 at 10:47
  • This functionality is provided by the standard library, and chances are that it's been tested extensively, so you can be sure it works as intended. The question is, why are you connecting to port 631, provided that the server _does not_ listen on this port? – ForceBru Aug 21 '19 at 10:52
  • But the issue is with identifying the correct port.Looks like only one port is enabled 21, but it does not connect. I get connection refused error. So I am creating new ports and passing to the program but s.accept() is not listening and not printing back the response address.Due to this the program is executing in infinite loop in the While True part and is never completing execution. We can ignore the Port numbers that I am using like 631, 47136 etc.. – Pallavi D Aug 21 '19 at 10:57
  • I am trying to connect to Windows 10 partition mounted onto ubuntu. I got the IP address using the command nmap IP address but I am unable to get correct port. I have an image file which I need to import from there so that I can access it from python program in ubuntu. – Pallavi D Aug 21 '19 at 11:00
  • 1) `while True:` _is_ an infinite loop, and it won't terminate unless there's a `break` statement; 2) I don't understand why you need to "identify the correct port" in the first place: you should start up the server listening on any predefined port and then use the client to connect to _this exact port_. – ForceBru Aug 21 '19 at 11:04
  • sure , I understand. And I tried starting the server by getting the port, and that's how I found port 47136, but when I use it and re-execute the program, it throws port is already in use. How will I test if I cannot use it? I also get Permission refused errors when I connect to the IP. I am able to change my root password to give permission for the ports, but it does not allow me to enter into the root folders....Looks like all this is happening only because Im unable to access Windows 10 folders from ubuntu – Pallavi D Aug 21 '19 at 11:12
  • Here's how you can solve the error about the socket being in use: https://stackoverflow.com/questions/6380057/python-binding-socket-address-already-in-use. As for "starting the server by getting the port": you don't "get" the port number anywhere, you choose the port number that you like, set the socket up as described in the link, fire up the server and have the client connect to that exact port. TBH, I don't see how this is related to Windows 10 folders. If your Ubuntu installation has access to the Windows partition, you shouldn't need any sockets as you can copy the data with `shutil` – ForceBru Aug 21 '19 at 11:17
  • I tried these options of reuse_address before and they didn't seem to work....I am trying using sockets, because I am working on a code where I can get data from multiple locations.. Im trying to use my Windows 10 as one such location for now as I do not have access to any other servers. – Pallavi D Aug 21 '19 at 11:32
  • Let me clarify some things: 1) have you ever managed to start the server or is it always erroring out, no matter what port number you're using? 2) is your client trying to connect to a port that's different from the one the server is listening on, like in your post? – ForceBru Aug 21 '19 at 11:40
  • I have managed to connect to the local host as server with different port numbers, but it gives me a Port already in use when I execute the program multiple times. – Pallavi D Aug 22 '19 at 07:06
  • I am trying to connect to the same port number unlike the code posted. – Pallavi D Aug 22 '19 at 07:07
  • when I connect to the Windows 10 port it gives me the same problem like above. Only one port 21 available but it does not give me permission to execute. Everytime I I execute the program I get the already in use error so I use a different port number. As mentioned , s.accept() does not return back the address or anyother print commands below this do not get executed. The program loops infinitely for the connection. – Pallavi D Aug 22 '19 at 07:10
  • I also tried to use the UDP/IP protocol to check if I am getting a response. It does send the data but does not receive the response. Please find the code below for the same. – Pallavi D Aug 22 '19 at 08:57

0 Answers0