I've just started learning to use sockets in python and I've got a couple of programs which I am satisfied with apart from a small problem. The client only works on my computer. Here are the codes.
Server:
Client:Code:from socket import * import pygame from os import chdir chdir('/home/noodels/prg/python/network/nudge/') pygame.init() alert = pygame.mixer.Sound('alert.wav') mySocket = socket( AF_INET , SOCK_STREAM ) mySocket.bind( ( '' , 1992 ) ) while True: mySocket.listen( 1 ) while True: channel , details = mySocket.accept() print "[SYSTEM]Message from" , details print channel.recv(100) alert.play() channel.send("Nudge successful.") channel.close()
I can run the server fine. And like that, the client can send a message to the server no problem. Of course, the client has to be on the same server as the computer. Replacing [host = 'localhost'] with [host = '127.0.0.1'] also works. But as soon as I put my ip address in there, when I try to run the program it stops, it doesn't complete the connection, it justs sits there with the _ thing just blinking as if it were mocking my novice attempts at networking. Is there something I need to do to the ip address first?Code:from socket import * host = 'localhost' port = 1992 s = socket( AF_INET , SOCK_STREAM ) s.connect( (host , port) ) print "Enter something to send (or leave blank):" message = raw_input() if message == "": message = 'I am a fish.' s.send(message) print s.recv(100) s.close()



Adv Reply




Bookmarks