简体   繁体   English

两台计算机之间的套接字编程

[英]Socket programming between two computers

I am trying to communicate between two computers and one is Mac and other one is Linux. 我正在尝试在两台计算机之间进行通信,一台是Mac,另一台是Linux。

The code I have at server side: 我在服务器端的代码:

import os
from socket import *

host = "192.168.1.47"
port = 10000
buf = 1024

address = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
UDPSock.bind(address)

print ("Waiting to receive messages...")

while True:
    (data, address) = UDPSock.recvfrom(buf)
    print("Received message: " + data)
    if data == "exit":
        break

UDPSock.close()
os._exit(0)

At client side I have: 在客户端,我有:

import os
from socket import *

host = "192.168.1.47" # set to IP address of target computer
port = 10000
addr = (host, port)

UDPSock = socket(AF_INET, SOCK_DGRAM)

while True:
    data = raw_input("Enter message to send or type 'exit': ")
    UDPSock.sendto(data, addr)
    if data == "exit":
        break

UDPSock.close()
os._exit(0)

The server keeps waiting for the request and though I send some message typed in client side server is unable to receive it. 服务器一直在等待请求,尽管我发送了一些在客户端服务器中键入的消息,但无法接收到该请求。 The host address is my mac ip address and asking the other computer to connect to this ip. 主机地址是我的Mac IP地址,并要求另一台计算机连接到该IP。 Could anyone help me understand where I went wrong. 谁能帮助我了解我哪里出了问题。 I referred to the other posts related to this topic but there were no appropriate solution for this. 我提到了与此主题相关的其他帖子,但是没有合适的解决方案。 Thanks 谢谢

First : use the same port :) 首先:使用相同的端口:)

And you are sending a message on the client with the localhost address. 并且您正在使用本地主机地址在客户端上发送消息。 Use the server address for the client. 使用客户端的服务器地址。

For the server : 对于服务器:

ip = "localhost" or "server ip" ip =“本地主机”或“服务器ip”

port = 10000 端口= 10000

For the client : 对于客户:

ip = "server ip" ip =“服务器ip”

port = 10000 端口= 10000

It worked for me on the same host. 它在同一主机上对我有用。 When there is a "b" before a string it is because I change the string in bytes. 当字符串前有一个“ b”时,是因为我更改了以字节为单位的字符串。

< string >.encode() change the string in bytes <string> .encode()更改字符串(以字节为单位)

< bytes >.decode() change the bytes in string <bytes> .decode()更改字符串中的字节

You need to understand if you use UDP there is no connection between your 2 computers. 您需要了解如果使用UDP,则两台计算机之间没有连接。 So the firewall will block the link, if you want to change that use TCP socket. 因此,如果您想使用TCP套接字更改该链接,防火墙将阻止该链接。

I recommand you to use python 3 if you can and to print this syntax is cool : 我建议您使用python 3,并且打印此语法很酷:

print("{}".format(msg_recv))

You add the variables in the format(..), and they will replace the " {} ". 您以format(..)添加变量,它们将替换“ {}”。

Client : 客户:

import os
from socket import *

host = "127.0.0.1" # set to IP address of target computer
port = 10000
addr = (host, port)

UDPSock = socket(AF_INET, SOCK_DGRAM)

while True:
    data = input("Enter message to send or type 'exit': ")
    UDPSock.sendto(data.encode(), addr)
    if data == "exit":
        break

UDPSock.close()
os._exit(0)

Server : 服务器:

import os
from socket import *

host = "127.0.0.1"
port = 10000
buf = 1024

address = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
UDPSock.bind(address)

print ("Waiting to receive messages...")

while True:
    (data, address) = UDPSock.recvfrom(buf)
    print("Received message: " + data.decode())
    if data == b"exit":
        break

UDPSock.close()
os._exit(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM