0

I am trying to do simple code to send file from the client to the server after saving in t some data. I am a beginner so I can't figure where the problem is or what is the missing function or line in my code

The Server :

import socket

server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8000))
server_socket.listen(0)
BUFFER_SIZE = 1024
conn, addr = server_socket.accept()
print ('Got connection from', addr)
while 1:
    data = conn.recv(BUFFER_SIZE)
    if not data: 
        break
    fileREC=open (data , 'rb')

The Client

import socket
client_socket = socket.socket()
client_socket.connect(("192.168.1.4", 8000))
BUFFER_SIZE = 1024
TextFile= open ("TextFile","w")
TextFile.write("Here is the file")
TextFile.write("Writing data")
TextFile.close()
f=open (TextFile , 'wb')
print ("Writing the file to binart ")
client_socket .send(f)
print ("Data Sent")

The Error

  ERROR:Traceback (most recent call last):
  File "tenmay.py", line 5, in <module>
   client_socket.connect(("192.168.1.4", 8000))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
  return getattr(self._sock,name)(*args)
  socket.error: [Errno 111] Connection refused
3
  • What have you done to resolve the problem? Commented May 10, 2014 at 21:48
  • Are you sure that the server has 192.168.1.4 ip? Can you ping this address? Commented May 10, 2014 at 22:05
  • yes I can ping it and send a string but not a text file Commented May 10, 2014 at 22:10

1 Answer 1

1

Send the contents of the file not the filehandle:

f=open ("TextFile", 'rb')
client_socket.send(f.read())

The second time the client runs the server is waiting to recv data because the accept() command is outside of the loop.

The client could repeatedly send data from a loop, but not if the program ends and has to be restarted.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.