7

Just a preface to this question: I have no clue what I am doing, so please excuse any stupidity.

I am making a socket based chatroom that I want to use on a local network (My dad's computer and mine connected through the same wifi).

Here is the server code:

import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
sock.listen(1)
while True:
 # Find connections
 connection, client_address = sock.accept()
 try:
 data = connection.recv(999)
 print data
 except:
 connection.close()

Here is the client:

import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 10000)
print >>sys.stderr, 'connecting to %s port %s' % server_address
while True:
 try:
 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 sock.connect(server_address)
 message=raw_input('Message: ')
 if message=='quit':
 break
 sock.sendall(message)
 except:
 break
sock.close()

When I run the client on one computer, and the server on the other.. the connection isn't made. I believe it is because of server_address = ('localhost', 10000) but I am not sure.. any suggestions?

asked Dec 20, 2013 at 2:48
6
  • How do I find that? ipconfig? And where do a put each? Commented Dec 20, 2013 at 2:55
  • 1
    ipconfig or ifconfig in linux, and you have tu put your ip in the server code ¨server_address = ('myip', 10000)¨ and you have to put a client in the computer of your dad and in the client you have to put this ¨server_address = ('myip', 10000)¨ Commented Dec 20, 2013 at 3:00
  • Ok, got it. I'll let you know how it goes. Commented Dec 20, 2013 at 3:03
  • 1
    @VictorCastilloTorres Add that as an answer. Commented Dec 20, 2013 at 3:40
  • @aIKid Sure, wait a minute :) Commented Dec 20, 2013 at 3:48

1 Answer 1

14

You have to use your local ip's for example if you are going to be the server you have to use your own ip in the server code:

to find the ip:

ipconfig - Windows CMD
ifconfig - Linux Shell, Mac Terminal

after you know your ip you have to replace localhost with your own ip:

Server code:

server_address = ('myip', 10000)

so in your father's computer you have to connect to your server:

Client Code:

server_address = ('myip', 10000)
answered Dec 20, 2013 at 3:54
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.