Client Server Connection Program

D

diffuser78

Hi,

I have the following code for a cline and server which I got from a
small tutorial on the web.

#!/ usr/bin/env python
# tms.py (SERVER)

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = int(sys.argv[1])

s.bind((host,port))
s.listen(1)
conn, addr = s.accept()
print 'client is at', addr

data = conn.recv(1000000)
data = data * 1000

z = raw_input()
conn.send(data)
conn.close()

AND

#/ usr/bin/env python
# filename: tmc.py (CLIENT)

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = int(sys.argv[1])
port = int(sys.argv[2])

s.connect((host,port))
s.send(sys.argv[3])
i = 0
while True:
data = s.recv(1000000)
i+=1
if (i<5):
print data
if not data:
break
print 'received', len(data), 'bytes'
s.close()


Server is installed on machine 192.168.1.4 and Client is 192.168.1.2


When I start server and the server is listening I start the clinet.

I started Client using ........

python tmc.py 192.168.1.4 2000 abcxyz

where 2000 is the port number and abcxyz is the string I pass to
server, I get the following error...

ValueError: invalid literal for int(): 192.168.1.4

How can I fix this ? I am trying to achieve the most basic level of
networking between a client and a server.

Every help is appreciated.
 
F

Fredrik Lundh

#/ usr/bin/env python
# filename: tmc.py (CLIENT)

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = int(sys.argv[1])
port = int(sys.argv[2])

the port number is an integer, but the host name/ip is obviously not an
integer -- so why are you trying to convert it an integer ?
s.connect((host,port))
s.send(sys.argv[3])
i = 0
while True:
data = s.recv(1000000)
i+=1
if (i<5):
print data
if not data:
break
print 'received', len(data), 'bytes'
s.close()


Server is installed on machine 192.168.1.4 and Client is 192.168.1.2


When I start server and the server is listening I start the clinet.

I started Client using ........

python tmc.py 192.168.1.4 2000 abcxyz

where 2000 is the port number and abcxyz is the string I pass to
server, I get the following error...

ValueError: invalid literal for int(): 192.168.1.4

How can I fix this ?

by reading the *entire* trackback, and looking at the line it tells you
to look at, before you post.

</F>
 
D

diffuser78

removing int frrom host = int(sys.argv[1]) fixed the problem. Thanks
Fred!!!

Fredrik said:
#/ usr/bin/env python
# filename: tmc.py (CLIENT)

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = int(sys.argv[1])
port = int(sys.argv[2])

the port number is an integer, but the host name/ip is obviously not an
integer -- so why are you trying to convert it an integer ?
s.connect((host,port))
s.send(sys.argv[3])
i = 0
while True:
data = s.recv(1000000)
i+=1
if (i<5):
print data
if not data:
break
print 'received', len(data), 'bytes'
s.close()


Server is installed on machine 192.168.1.4 and Client is 192.168.1.2


When I start server and the server is listening I start the clinet.

I started Client using ........

python tmc.py 192.168.1.4 2000 abcxyz

where 2000 is the port number and abcxyz is the string I pass to
server, I get the following error...

ValueError: invalid literal for int(): 192.168.1.4

How can I fix this ?

by reading the *entire* trackback, and looking at the line it tells you
to look at, before you post.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top