I am trying to use an example for a server, but the client can only send one message and then the server will reply with a number.
Here is the code.
import socket
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
mySocket.bind ( ( '', 2000 ) )
mySocket.listen ( 1 )
while True:
channel, details = mySocket.accept()
print 'We have opened a connection with', details
print channel.recv ( 100 )
channel.send ( 'Green-eyed monster.' )
channel.close()
Questions:
-
Why is it that whenever the client sends a message to the server, it responds with a number?
-
How can I use sockets over the Internet, not over LAN?
-
Is there a way for me to have the server move data from client to client using sockets – somewhat like an IM program.
-
I will most likely host this IM server for my friends on my Mac – will it work between OS’s (Mac, PC)
-
Are there any good libraries to use for this? (I have heard that HTTP is great)
-
These lines of code are really confusing. How do they work?
print channel.recv ( 100 ) channel.send ( 'Green-eyed monster.' ) -
Also, when I close the server (using the red X), and reuse the port, it says this:
Traceback (most recent call last): File "C:\Users\****\Desktop\Python\Sockets\First Server\server.py", line 3, in <module> mySocket.bind ( ( '', 2003 ) ) File "C:\Python27\lib\socket.py", line 224, in meth return getattr(self._sock,name)(*args) error: [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted -
Lastly, where are some good tutorials for me to understand this better?
Sorry for asking so many questions in the same post, but when I posted this and this people got mad with me for posting about similar problems in different problems.
Python version: Python 2.7.3
I have done some research (over several hours) and have found some solutions to my issues!
3: Yes, There is a way to have the clients communicate with each other, they just have to use the server! You have to initialize an infinite loop on the server side, which will receive data and send it. This is how I solved the issue:
This program is part of a definition called clientthread which initializes all connections with clients. The “Actual” loop carries on the rest of the thread:
4: Yes, it will work between OS’s. Sockets are independent of the platform.
6: That line of code is actually quite simple. The line
print channel.recv ( 100 )tells the server to print the user input, up to 100 characters. The next line,channel.send ( 'Green-eyed monster.' )is simply telling the server to send out a message,Green-eyed monsterto the client.7: That error occurs because you cannot have more than one socket open per port. I would suggest using a line such as
s.close()orsys.exit()at the end of your code, to close the port. Or you can simply choose another one!8: There are some great tutorials out on the internet. One that teaches you the basics of sockets is this. Telnet was really confusing for me, so I discovered that in conjunction with this client one can create a customized client which is in fact much better.
If you have any questions, feel free to comment and I will try to answer.