1

I am learning about networking and programming. I am trying to be a simple IM application. What i am trying to achieve having a server application route messages between computers over the internet creating a simple instant messenger. However i can't find any sort of tutorial for that. I want to use the client-server model. The way i think it works is that let say to computers are connected to a server through some sort of login. The server can then pass messages between thoes two connected computers. Anyone know any resources for me to beable to achieve this? Thanks

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by extr3mex
0

you will need 2 learn the basics of python network programming. You can check out the documentation in the python website. I had created a simple client to server chat application. Check out the code below,

TCP Server Code:

# TCP Server Code

host="127.0.0.1"                # Set the server address to variable host
port=4446                   # Sets the variable port to 4444
from socket import *                # Imports socket module

s=socket(AF_INET, SOCK_STREAM)

s.bind((host,port))                 # Binds the socket. Note that the input to 
                                            # the bind function is a tuple

s.listen(1)                         # Sets socket to listening state with a  queue
                                            # of 1 connection

print "Listening for connections.. "

q,addr=s.accept()               # Accepts incoming request from client and returns
                                            # socket and address to variables q and addr

data=raw_input("Enter data to be send:  ")  # Data to be send is stored in variable data from
                                            # user

q.send(data)                        # Sends data to client

s.close()

# End of code

TCP Client Code:

# TCP Client Code

host="127.0.0.1"            # Set the server address to variable host

port=4446               # Sets the variable port to 4444

from socket import *             # Imports socket module

s=socket(AF_INET, SOCK_STREAM)      # Creates a socket

s.connect((host,port))          # Connect to server address

msg=s.recv(1024)            # Receives data upto 1024 bytes and stores in variables msg

print "Message from server : " + msg

s.close()                            # Closes the socket 
# End of code

Hope this helps and dont forget to vote up! :B

Votes + Comments
Watch out for your line spacing, otherwise good effort.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.