Home > Enterprise >  Establishing TCP socket connection between 2 VMs using Python
Establishing TCP socket connection between 2 VMs using Python

Time:09-17

I am running 2 Virtual Machines as Client-Server.

One VM is a Windows[Client] and the other VM is a CentOS[Server].

I have written a program[python] to create TCP connections between them.

When I am running both the client and server code on the same machine[server as localhost], the script works fine and I can see the TCP connections being established. But when I run the scripts separately[client script on windows and server script on CentOS] the TCP connections are not being made.

Both VMs are pingable from each other however.

Is there something I am missing?

Following is the client side script. When I change server name from "local host" to the server IP, no connection is made.

# Client Side Script

from socket import *
server_name = 'localhost' #DOES NOT WORK WHEN REPLACED WITH SERVER IP
server_port = 7000
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect((server_name,server_port))

while True:
  sentence = input(">> ")
  client_socket.send(sentence.encode())
  message = client_socket.recv(2048)
  print (">> ", message.decode())
  if(sentence == 'q'):
    client_socket.close()

Following is the firewall conf on my CentOS Server. If it helps:

user#sudo firewall-cmd --list-all
  public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: dhcpv6-client http https ssh
  ports: 1234(changed)/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules:

CodePudding user response:

The following worked. Thanks for your inputs. I followed https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-using-firewalld-on-centos-7

sudo firewall-cmd --zone=public --permanent --add-port=5000/tcp
  • Related