networking

Hi, I came across some network code and was working just find on my network (my 2 computer that are connected) but not over the net itself (one computer in house a and another in house b). All the code does is creat a socket on both client and server then server lisens as the client can send a msg with raw strings, a basic one-way chat msger. I have two questions. One is how do I make this work over the net (with house a and b)? My other is how do I go about combining them so that they both are client/server at the same time?

Client code:

# Client program

from socket import *

# Set the socket parameters
host = "myipaddress"
port = 21567
buf = 1024
addr = (host,port)

# Create socket
UDPSock = socket(AF_INET,SOCK_DGRAM)

def_msg = "===Enter message to send to server===";
print "\n",def_msg

# Send messages
while (1):
	data = raw_input('>> ')
	if not data:
		break
	else:
		if(UDPSock.sendto(data,addr)):
			print "Sending message '",data,"'....."

# Close socket
UDPSock.close()

Server:

# Server program

from socket import *

# Set the socket parameters
host = "myipaddress"
port = 21567
buf = 1024
addr = (host,port)

# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)

# Receive messages
while 1:
	data,addr = UDPSock.recvfrom(buf)
	if not data:
		print "Client has exited!"
		break
	else:
		print "\nReceived message '", data,"'"

# Close socket
UDPSock.close()

In answer to your questions: I don’t know.

However, Hypnos did create a networking library. You can find it here…

panda3d.etc.cmu.edu/phpbb2/viewt … networking

I don’t know if anyone tested it yet, or how good it is, but maybe you could try it out.

Works exactly the same over the internet as over a lan. You just need to set the correct ip addresses for both computers. Note that these will be different for the internet and for your lan.

1 Big difference between the net and your home network : Firewall

especially for UDP!!

Most firewal are configured to deny UDP port scanning.
So your UDP connection, even if using the righ IP adress (ie the public adress of your remote computer) may fail.

1st ligth in the tunnel: often firewall may be configured for Nat push through
Meaning if the people behind the firewall initiate a connection to you, then you are able to send back UPD answer and he can answer also by UDP as long as you send back packet fast enough…

Now if you control both computer (local and remote) then you can configure your firewall to accept udp connection on a single port and use it…
NAT Push Through is not a simple issue :frowning:

its the same problem for the TCP except that udp can do NAT Push Through while TCP can’t.

I was woundering if it was a firewall or a router problem, or both lol. Is there any work around that can be done like other online games?

Usually you have to do port-forwarding on the router for games or any other programs that use tcp.

Ok, now for the last thing, how would I go about with the flow of it making both client and server at the same time so they can chat between them?

Hi,

I’m not sure how to do it in Python but I already done one with Java, 3 years ago. Each one of your computer must have the same code. When your program start, you specify the connection to the other one (Ip adresse, port). Be careful here like the other said, firewall can be tricky sometimes. For a chat server, I would recommend using TCP instead of UDP because with UDP you can lose packet and that’s not very cool with chat system :slight_smile:

After that, each computer have the code to send a message and code to listen on the port waiting for new message. When you press enter or press the submit button, the “shoot message” code is activated, sending content to the other computer.

I remember doing this with thread in java (the shoot message and wait code were running “at the same time”). I’m not sure if it’s possible in Python. Other people can tell you that. I think that’s all for the theory of doing a chat apps!

Have a great day

Jaff

I think you are looking for something like the TCP RendezVous in panda library.

The TCP RendezVous is dedicated to receive connection attempt on a specific port on the “server”

When it receive a connection attempt it call a callback where you can accept the connection and add it to your active connections
(see in manual)
You can then send an aknowledgement from the server telling the client “OK you are in”
=> it’s a stripped down handshake system between your 2 computers…

The TCP Rendez vous is a kind of background tasK