0% found this document useful (0 votes)
87 views

File Transfer Using TCP

This document describes a TCP client/server application to transfer a text file from a client to a server. The server code opens a port, accepts a connection from the client, reads a file and sends it in 1024 byte chunks to the client. The client code connects to the server, receives the file in 1024 byte chunks and writes it to a new file. When complete, the client closes the connection.

Uploaded by

Hansraj Rouniyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

File Transfer Using TCP

This document describes a TCP client/server application to transfer a text file from a client to a server. The server code opens a port, accepts a connection from the client, reads a file and sends it in 1024 byte chunks to the client. The client code connects to the server, receives the file in 1024 byte chunks and writes it to a new file. When complete, the client closes the connection.

Uploaded by

Hansraj Rouniyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Develop a TCP client/server application for transferring a text file from client to

server?

Server file

import socket

port = 60003
s = socket.socket()
host = socket.gethostname()
s.bind((host, port))
s.listen(5)

print 'Server listening....'

while True:
conn, addr = s.accept()
print 'Got connection from', conn,addr
data = conn.recv(1024)
print('Server received', repr(data))

filename='mytext.txt'
f = open(filename,'rb')
l = f.read(1024)
while (l):
conn.send(l)
print('Sent ',repr(l))
l = f.read(1024)
f.close()

print('Done sending')
conn.send('Thank you for connecting')
conn.close()

Client File

import socket
s = socket.socket()
host = socket.gethostname()
port = 60003

s.connect((host, port))
s.send("Hello server!")

with open('received_file', 'wb') as f:


print 'file opened'
while True:
print('receiving data...')
data = s.recv(1024)
print('data=%s', (data))
if not data:
break
# write data to a file
f.write(data)

f.close()
print('Successfully get the file')
s.close()
print('connection closed')

You might also like