0

I am asking this question as an extension to the following question: https://superuser.com/questions/713409/how-to-message-any-user-on-your-lan

I don't think I have a real answer. I found it quite difficult to get the net send command working on Windows 7 and even if I could have it would have been enabled only for admin users.

So I have decided to write my own program to solve the problem. Using python I can write a program to check for messages on a common file (which I can store on a common harddrive which we have on our LAN). So the program will write from:192.168.23.44 to:192.168.23.45 Hello. Assuming the program is installed on both machines, I can ask the machines to poll this file every few seconds and pop-up a message if a message has been written for it, by searching for it in the to: field. If writing to a single file becomes an issue I will make multiple files which will act as inboxes for each of the computers on the LAN.

However, this approach is very inelegant. I don't suppose internet messengers, Google talk for instance, poll a server to check whether there are any new messages for the user. So my question is: what is the optimal way to write a LAN messenger program. Now that I think of it, I am looking for a minimalist approach. I will just put the program in the Startup folder of all the machines and it should come alive whenever any user logs in.

I was just able to configure the machine to respond to the ping command by making a new rule for the ICMPv4 protocol (ref: http://answers.microsoft.com/en-us/windows/forum/windows_7-networking/how-to-enable-ping-response-in-windows-7/5aff5f8d-f138-4c9a-8646-5b3a99f1cae6). I am guessing that answer lies in python and the ICMPv4 protocol, but I needed some direction.

  • Why don't you just use IRC? – whatsisname Feb 9 '14 at 14:26
1

You could create your own chat protocol based on TCP/IP sockets.

There are two common ways to implement such an application. Either a client/server architecture or a peer-to-peer architecture.

In the client/server architecture, you run a central chat server on a machine which is always on. Each participant uses a separate client application. When a client goes online, it connects to the server. When a client want to send a message, it sends that message to the server. The server then relays it to the recipient(s).

When you don't have a server which is always online, you could instead go for a peer-to-peer architecture.

In that scenario your chat program is both client and server at the same time. To send a message to another user, the client of the sender connects directly to the client of the receiver. In order to be able to do this, you need to know the receivers IP address or at least its hostname so you can resolve the IP through DNS.

  • Thanks! I know the IP addresses of all the machines on the LAN. All the users know the IP addresses of all the machines (say there are like 8 machines but located some distance apart). Can you be more specific? Is there a simple way to send such messages using Python? – Shashank Sawant Feb 8 '14 at 20:07
  • @ShashankSawant I would recommend you to google for some Python socket programming tutorials. How to write a chat program is a frequent subject of such tutorials. – Philipp Feb 8 '14 at 20:09
  • Thanks again! One last question: for the python sockets, what protocol should I allow through the custom rule in Windows 7 firewall? I couldn't find IPv4 in the dropdown list (ICMPv4 and IPv6 exist). – Shashank Sawant Feb 8 '14 at 20:17
  • ICMP is the protocol used for ping. Your company may be using IPv6 exclusively in-house. If that's the case, then you probably won't find anything for IPv4 on the Windows firewall. – Adam Zuckerman Feb 8 '14 at 22:59
  • For P2P discovery, clients can IP Multicast some kind of join request. All nodes on the network listen for that multicast and respond with their client info. You can quickly/easily discover all of the chat peers on the network that way. – Steven Evers Feb 10 '14 at 2:08
1

I made a litle concept script with sockets.

See.

The server:

# Sockets server.
import socket

s = socket.socket()
s.bind(("localhost", 44000))
s.listen(1)

print("Connection Waiting...")
sc, addr = s.accept()
print("Connection Matched!")

while True:
    print("Waiting message...")
    received = sc.recv(128)
    print(type(received))
    a = received.decode('utf-8')
    print(type(a), len(a))
    if a == "quit":
        break
    print("Received Message:", a)
    sc.send(received)

print("Good bye!")

sc.close()
s.close()
a = input("Enter key to End:")

And this is the client script:

# Sockets client script
import socket, time

s = socket.socket()
print("Connecting to server...")
while True:
    try:
        s.connect(("localhost", 44000))
        break
    except:
        print("Retrying connection to server...")
        time.sleep(1)

print("Connection Matched!")

while True:
      keyboardInput = input("Enter the message: ")
      # s.send(bytes(message, 'UTF-8'))
      message = keyboardInput.encode('utf-8')
      s.send(message)
      print("Message sent!")
      r = s.recv( 128)
      a = r.decode('utf-8')
      print("Confirmation received message: ", a)
      if keyboardInput == "quit":
         break

print("Good bye")

s.close()

a = input("Enter key to End:")

Edit: to implement multi-threads. In order to avoid the server blocks the flow while is waiting for incoming message.

# Example script with sockets and multi-threads
import socket, threading, time

a = ""
s = socket.socket()
s.bind(("localhost", 44000))
s.listen(1)

sc, addr = s.accept()

class MiThread(threading.Thread):   # Defines the class MiThread, subclass of Thread
    def __init__(self, sc):
        threading.Thread.__init__(self)

    def run(self):  # this method is the code of the thread, it will works when be called by .start() method
        received = sc.recv(128)
        print(type(received))
        a = received.decode('utf-8')
        print(type(a), len(a))
        print("Received Message:", a)
        return a

t = MiThread(sc)     # creates an object of class MiThread, subclass Thread
t.start()       # method start() calls to .run() method as a thread

# this code is for debugging, change it and put your own code
# I'll works in the main proccess while the thread is waiting for messages
for i in range(0, 60):
    time.sleep(1)
    print("Seconds: ",i)
    #sc.send(received)

print("Time out. Good bye!")

sc.close()
s.close()
a = input("Enter key to End:")

If you want to code a litle chat, you might to use this two utils: sockets and multi-threading.

  • How much of a burden will the while loop be on the processor? Thanks for the sample script... – Shashank Sawant Feb 9 '14 at 18:18
  • In the server side, the received = sc.recv(128) method blocks the flow until it receives a message. In the client side the r = s.recv( 128) also do the same. You might use try: estament to handle exception errors. I'm working in implement multi-threads to prevent the flow blocking while is waiting the message. – Trimax Feb 9 '14 at 21:03
  • Programmers is tour conceptual questions and answers are expected to explain things. Throwing code dumps instead of explanation is like copying code from IDE to whiteboard: it may look familiar and even sometimes be understandable, but it feels weird... just weird. Whiteboard doesn't have compiler – gnat Feb 17 '14 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.