简体   繁体   中英

How to connect Linux Virtual Machine from its host computer?

I'm creating a simple chat app using Python.

The server code is in the Linux VM (I am using Virtual Box), and the client code is in the Windows 10 computer where the Virtual Box is installed.

I'm trying to connect it with the Python socket.

It works when I ping both machines to each other.

My problem is what port should I put in the client code:

Client

import socket
import subprocess

cliente = socket.socket()

try:
    cliente.connect(('192.168.1.33',9090))
    cliente.send("1")

I tried every port available; however, nothing works. I think I am missing something to make this work.

First, You need to make sure that the port number passed to bind function in server code in Linux VM is the same port number used by connect function in your client.

Check this simple server-client example in python which is using port 12345: https://www.tutorialspoint.com/python/python.networking.htm

Second, (based on your comments) the IP address that should be used in client connect function is the IP address of the machine running the server code which is in your case the Linux VM. Try to run the shell command ifconfig in the Linux VM to get the IP address.

If this address didn't work you can change the.network settings of the virtual machine to bridged instead of NAT and try again the ifconfig command and get the new IP address.

Doing SSH from the host machine to Linux Virtual machine will work great!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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