First of all, excuse me for bad English, I speak Spanish...

Well, my question is how do I implement sockets in my program? I'm working on a little game call "Battleship", I already have how to set your ships and the function to attack, but I need to connect 2 computers to play it, so they must be sending information constantly to the other computer (the information they need to send is the coordinate that the player wants to attack [example: attack on "A6"]). I've been looking for info, and I see that I need to create a server and client program, but I don't understand how it work each, I don't know how to make my program request the other computer's IP or if this it's really necessary... So If anybody can help me with this, please...

PD: i'm working on python 2.6

Dani AI

Generated

Brief practical guide that builds on ’s goal and the pointers from and . One program acts as the server (binds a port and listens); the other acts as the client (connects to server IP:port). For a simple Battleship protocol send short text messages terminated by a newline (for example ATTACK A6\n), and reply with RESULT HIT\n or RESULT MISS\n. The client needs the server’s IP (or a discovery step); on a LAN use the host’s local IP, on the Internet use the host’s public IP plus router port‑forwarding.

Minimal Python 2.6 examples (line‑based messages):

# server.py (Python 2.6)
import socket
HOST = ''          # accept on all interfaces
PORT = 50000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
print 'Listening on port', PORT
conn, addr = s.accept()
print 'Connected by', addr
try:
    while True:
        data = conn.recv(1024)
        if not data: break
        coord = data.strip()
        print 'Attack:', coord
        # compute hit/miss and send back
        conn.sendall('RESULT MISS\n')
finally:
    conn.close()
    s.close()
# client.py (Python 2.6)
import socket
HOST = 'server.ip.here'   # replace with server IP
PORT = 50000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('A6\n')
reply = s.recv(1024)
print 'Reply:', reply.strip()
s.close()

Practical notes and cautions: test locally with 127.0.0.1 first; check ipconfig/ifconfig for LAN IP; configure firewall and router port forwarding for Internet play; pick ports >1024. Use sendall and a line buffer on receive (accumulate until \n) to avoid partial messages. For simultaneous send/receive use threads or select(); set timeouts and catch socket errors. Prefer JSON for structured messages and never pickle data from untrusted peers. Finally, Python 2.6 is outdated—migrate to Python 3 for long‑term projects and libraries that simplify networking.

Recommended Answers

All 2 Replies

Actually, if you want to learn about sockets then i suggest this tutorial just here

it gives more than just the syntax and actually helps you learn how to use them.

Though, this one:
http://www.devshed.com/c/a/Python/Sockets-in-Python/
is the one that i learnt with, it was pretty good.

Hope that helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.