Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

Please help me in socket programming in python

Problem while making connection with server.

server side:

import socket
import threading
import sys
ip = "let ip address of server, cant type real one for security purposes, example: 1.2.3.4"
port = 9999
def make_socket(ip, port):
    global server
    try:
        server = socket.socket()
        server.bind((ip, port))
    except:
        make_socket(ip, port)
def handle_client(conn, addr):
    print(f"Connected with {addr}
")
    connected = True
    while connected:
        msg = conn.recv(1024).decode("utf-8")
        if msg == "exit()":
            connected = False
        if len(msg) > 0:
            print(f"CLIENT: {msg}")
            if connected:
                msg = input("You: ")
                if len(msg) > 0:
                    conn.send(msg.encode("utf-8"))
    conn.close()
def make_connection():
    server.listen(2)
    while True:
        conn, addr = server.accept()
        thread = threading.Thread(target=handle_client, args=(conn, addr))
        thread.start()
        print(f"ACTIVE CONNECTIONS:{threading.activeCount() - 1}")
print("SERVER INITIATED.")
make_socket(ip, port)
make_connection()

client side:

import socket
ip = "same address as written in server side, example: 1.2.3.4"
port = 9999
client = socket.socket()
client.connect((ip, port))
def send(msg):
    message = msg.encode("utf-8")
    client.send(message)
run = True
while run:
    msg = input("You: ")
    if msg == "exit()":
        send(msg)
        run = False
    else:
        send(msg)
    print(f"Server: {client.recv(100).decode('utf-8')}")

It runs as expected in the same pc.

But when I am running client script and server script in 2 different pcs, they are not connecting. Even though the address is same. I have to type the ip address of server in server.bind and client.connect, right? They both should be same, right?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The IP address you pass to client.connect() should be the IP address of the computer where the server is running (if it's the same computer as where the client is running, you can just pass 127.0.0.1 as that address always means localhost). For the bind() call I recommend passing in '' (i.e. an empty string) as the address, so that your server will accept incoming connections from any active network interface. You only need to pass in an explicit IP address to bind() if you want limit incoming connections to only those coming through the local network card that is associated with the specified IP address.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...