-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_fileio_TCP.py
More file actions
32 lines (25 loc) · 1.09 KB
/
client_fileio_TCP.py
File metadata and controls
32 lines (25 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#The client program sets up its socket differently from the way a server does. Instead of binding to a port and listening, it uses connect() to attach #the socket directly to the remote address.
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('127.0.0.1', 10000)
print >>sys.stderr, 'connecting to %s port %s' % server_address
sock.connect(server_address)
foc = open("client.txt", "r")
#After the connection is established, data can be sent through the socket with sendall() and received with recv(), just as in the server.
try:
# Look for the response
while True:
bufc = foc.read(10)
if bufc:
sock.sendall(bufc)
else:
print >>sys.stderr, 'all contents read, no more data in file'
break
finally:
print >>sys.stderr, 'closing socket'
sock.close()
foc.close()
#When the entire message is sent and a copy received, the socket is closed to free up the port.