Reputation: 465
I learned how to connect the client and server using sockets when both of them are on the same wifi. However, when the client computer and server computer are connected to different wifi modems, the client can not find the server. I wonder how should I change my code so that the client becomes able to connect to the server regardless of the wifi network they're connected to?
import sys
from socket import socket, AF_INET, SOCK_DGRAM
SERVER_IP = '192.168.1.2'
PORT_NUMBER = 5000
SIZE = 1024
print ("Test client sending packets to IP {0}, via port {1}\n".format(SERVER_IP, PORT_NUMBER))
mySocket = socket( AF_INET, SOCK_DGRAM )
myMessage = "Hello!"
b_myMessage = myMessage.encode()
print(type(b_myMessage))
myMessage1 = "Ended"
i = 0
while i < 10:
mySocket.sendto(b_myMessage,(SERVER_IP,PORT_NUMBER))
i = i + 1
mySocket.sendto(myMessage1.encode('utf-8'),(SERVER_IP,PORT_NUMBER))
sys.exit()
Upvotes: 1
Views: 1548
Reputation: 171
I believe you need to use
socket(AF_INET, SOCK_STREAM)
So that it will connect over TCP, and then port forward port 5000 (or whatever port you're using) on both your PC and your WiFi router. You'll then need to change the server IP to your server's Public IP, rather than the private one, in your code.
Upvotes: 1