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
Corresponding source code 1 (server side)
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 2 (client 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
What I tried
# 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 ()
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.)
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.
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.
- python - statuscodeunimplemented during grpc streaming communication
- python 3x - python3 serial port communication how to write in hexadecimal
- python - i want to communicate with the measuring instrument via usb on win10
- i want to display the value obtained by socket communication in python with flask in html on the browser, but it is not displaye
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- xcode - pod install [!] no `podfile 'found in the project directory