Home>

I want to perform TCP/IP communication between two PCs using Python
(Additional question: I want to know if the local address that comes out with netstat is the server side or the client side when I go with one computer)

Communication is not possible even if you enter the IP address of the server for the following.

Connection destination server: I typed the IP address of the server
Can't connect
Process completed with exit code 0


You can communicate if you do it on the same computer

Connection server: localhost
Enter message: Hello
Message from the server:
2020-11-22 17: 19: 18.560995
Process completed with exit code 0
Corresponding source code 1 (server side)
# Module import
import socket
import datetime

#Global variables
PORT = 50002 #port number
BUFSIZE = 4096 # Receive buffer size
#Main executive department
# Create socket
server = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
# Address setting
server.bind (("", PORT))
# Standby for connection
server.listen ()
#Responding to clients
while True: #Repeat correspondence
    client, addr = server.accept () # Get communication socket
    msg = str (datetime.datetime.now ()) # Compose message
    print (msg, "Connect Requested")
    print (client)
    data = client.recv (BUFSIZE) # Received from client
    print (data.decode ("UTF-8")) # Output of received contents
    client.sendall (msg.encode ("utf-8")) # Send message
    client.close () # Close connection
Corresponding source code 2 (client side)
# Module import
import socket
import sys
#Global variables
PORT = 50002 #port number
BUFSIZE = 4096 # Receive buffer size
#Main executive department
# Create socket
client = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
# Connection with server
host = input ("destination server:")
try: try:
    client.connect ((host, PORT))
except:
    print ("cannot connect")
    sys.exit ()
# Send a message to the server
msg = input ("Enter message:")
client.sendall (msg.encode ("utf-8"))
#Receiving a message from the server
data = client.recv (BUFSIZE)
print ("Message from server:")
print (data.decode ("UTF-8"))
# Close connection
client.close ()
What I tried

At first I tried to try it on the same computer, and when I entered "localhost" in the host of the client and executed it, communication was possible, but when I set that value to the IP address of the second computer, I could not.

Supplementary information (FW/tool version, etc.)

Python 3.6
Anaconda3
PyCharm
Windows 10

  • Answer # 1

    (1) If you are connecting two PCs directly with a LAN cable, you need a cross cable. Since it is almost a straight cable that is rolling at home, if you connect two PCs directly using the one that is rolling at home, communication will not be possible with a high probability. Please go to Yamada Denki to buy a cross cable.

    (2) When you buy it, set the IPs of the two PCs by referring to the URL below. http://qa.elecom.co.jp/sp/faq_detail.html?id=5089
    I think you can now communicate.

    ③ In the case of wifi, check if the two PCs are connected via a network.
    In case of Windows10, ping communication is not possible unless the firewall settings are changed.
    https://n-archives.net/software/nwol/articles/how-to-allow-ping-response-in-windows10/

    From former Isofura Endunia. .. ..

  • Answer # 2

    Let's study socket communication with Qiita's python. Isn't the article helpful?

    I don't understand what kind of operation means the following of "I can't communicate even if I enter the IP address of the server for the following". Please be specific about the explanation.

    The problem with the program in question (server side) seems to be that you didn't specify the IP address in bind.