0

i can't figure out how to send a message from client (on the main machine, windows 10) to the server (ubuntu on VM). Here's my client code on windows 10:

import socket
 
target_host = "10.0.2.15" target_port = 1236
 
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
client.connect((target_host, target_port))
 
client.send("ABCDEF")
 
response = client.recv(1024)
 
print(response)

And here's the server code on the VM:

import socket 
import threading
 
bind_ip: "10.0.2.15" 
bind_port: 1236
     
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

server.bind((bind_ip, bind_port))
         
server.listen(5)
print("Listening on %s:%d"%(bind_ip, bind_port))
        
def handle_client(client_socket): 
   request = client_socket.recv(1024)
   print("Recived: %s"%request) 
   client_socket.send("Recived!")
   client_socket.close()
 
while True: 
   client, addr = server.accept()
   print("Accepted connection from %s:%d"%(addr[0],addr[1])) 
   client_handler = threading.Thread(target=handle_client, args=(client,))
   client_handler.start()

If i run both the client and the server con the same machine it works fine, but if i run the server on the VM and the client on the main machine it doesn't work.

Micio
  • 30
  • 1
  • 4
  • 1
    The likely culprit is the IP address in which you are using. Likely the VM is either (a) not exposing its IP to the host, or (b) has a different IP than the one you are setting. Option a is the more likely. Go on console and try to `ping ` - if that doesn't work, your virtual machine is simply not reachable for reasons that one could not explain without more details. – felipe Oct 16 '20 at 09:09
  • If i launch "ping 10.0.2.15" on cmd gives me in output "Response from 10.203.13.1: Destination host not reachable". Why the IP in the response is different from the IP I typed in the ping command? – Micio Oct 16 '20 at 09:20
  • Unsure, but if I had to guess the VM software you are using is re-routing the ping request. Ping uses the [ICMP](https://en.wikipedia.org/wiki/Ping_(networking_utility)) protocol, which is something I'm not too familiar with myself, so it is hard for me to comment with confidence. – felipe Oct 16 '20 at 09:26
  • I tried to ping 10.203.13.1, and i get back a successful result. I can't understend why... – Micio Oct 16 '20 at 09:26
  • 10.x.x.x is your local network IP (your router). 10.203.13.1 is either your local machine, or some service that is running that is capable of replying. If the VM is running inside your machine, it likely has its own internal IP inside the VM's network, but if the VM is accessible outside the VM's network, it would be done so through your host IP (in most cases). – felipe Oct 16 '20 at 09:28
  • If you are just trying to develop something and the VM is not a dependency, you can just run both scripts inside your own computer (without a VM) and instead use the IP [`127.0.0.1`](https://en.wikipedia.org/wiki/Localhost). – felipe Oct 16 '20 at 09:29
  • Also, `a: 1` is valid syntax in Python, but it does not set `a` to `1`. You must do `a = 1`. The following two lines; `bind_ip: "10.0.2.15"` and `bind_port: 1236` aren't doing much. – felipe Oct 16 '20 at 09:30
  • the problem could therefore lies in the vm's settings... I wrote the post wrong, in my code i used =. Thanks anyway! – Micio Oct 16 '20 at 09:37

0 Answers0