1

I am trying to communicate between two computers and one is Mac and other one is 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. 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

3
  • If you can edit your question so the code is formatted as code this will be easier to help you with. stackoverflow.com/editing-help Commented Feb 16, 2018 at 21:43
  • Does sendto() actually execute, or hang? What value does it return? It should be the number of bytes sent. Commented Feb 16, 2018 at 21:51
  • It just asks me to type the message, i wanted to send and just hangs and doesn't print anymore information. I thought it posts the message to the server but it ain't Commented Feb 16, 2018 at 23:26

2 Answers 2

1

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.

< string >.encode() change the string in bytes

< bytes >.decode() change the bytes in string

You need to understand if you use UDP there is no connection between your 2 computers. So the firewall will block the link, if you want to change that use TCP socket.

I recommand you to use python 3 if you can and to print this syntax is cool :

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

You add the variables in the format(..), and they will replace the " {} ".

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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"

port = 10000

For the client :

ip = "server ip"

port = 10000

4 Comments

Hi, The server ip is only given as an address in the client program.
Please see the editted program and i was trying the above client program. Having the same ip address of the server to the client in order to send message
I don't know if mac has a firewall as windows but when you are using a UDP datagram (socket is for TCP), the firewall can block the message because there is no connection between the client and the server in UDP. The client can send a message and doesn't know if the server received it or not. You can try your client/server on the same host (linux).
May be i will give it a go with using just two linux machines and keeping mac aside.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.