1

I know that similar questions have been raised but they don't seem to work for me! I have tried serializing the dictionary then converting that to a string then encoding it before I send it over the socket. No success so far!

This is my server code: #library import socket import pickle

#socket initialization
host = "127.0.0.1"
port = 5000
mainAddr = (host, port)

#dict initialization
dataDict = {} #just imagine that dict has content

#create socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP
s.bind((mainAddr))
s.listen(4)
print('program started')
print('listening..')

while True:
    try:
        conn, addr = s.accept()
        print("connection from: "+str(addr))
        print("sending message..")
        pickle.dumps(dataDict)
        print('pickled!')
        dataS = str(dataP)
        print('stringed!')
        dataE = dataS.encode('UTF-8')
        print('encoded!')
        s.sendto(dataE,addr)
        print('data sent!')
    except: 
        pass

s.close()

For the socket initialization, I've tried other types:

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #UDP
s = socket.socket()

For the sending part, I've tried these alternatives:

s.send(dataE)
s.send(dataE,addr)
s.sendall(dataE)
s.sendall(dataE,addr)

When I run the program, these get printed out:

program started
listening..
connection from:<insert addr here>
sending message..
pickled!
stringed!
encoded!

Only data sent! is not sent. So I am guessing that it's the sending part that has a problem.

For the client side, here's the code:

#library
import socket
import pickle

#initialization
host = '127.0.0.1'
port = 5000
buffer = 1024

#create socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP
s.connect((host,port))
print('connected!')

#receive dictionary
print('receiving message..')
while True:
    data, addr = s.recvfrom(buffer)
    print('received!')
    dataD = data.decode("UTF-8")
    print('decoded!')
    dataP = pickle.loads(dataD)
    print('unpickled!')
    print(str(dataP))

s.close()

In the client terminal, only the following prints:

connected!
receiving message..

On the client side, I've tried changing the order of unpickling and decoding but still, to no avail.

4
  • This is not the real code. Please post a functional code. (dataP assignment is missing) Commented May 11, 2017 at 16:22
  • Just some things to note on your approach to the problem, pickled objects are binary. you're convertng them to str... and then encoding in utf.. which is completely nonsense. Sockets are binary so you can just send the raw bytestring returned from pickle over the socket. Just try to simplify it and return if you still face problems :) Commented May 11, 2017 at 16:25
  • Wow! Thank you for that suggestion on just pickling! My code is finally working! This one plus the answer below is what helped. Commented May 12, 2017 at 3:58
  • pickle is not secure Commented Jan 4, 2021 at 12:16

1 Answer 1

1

A TCP server socket is not actually used for sending/receiving data; I'm surprised you're not getting an error when calling s.send() or similar on it. Instead, it's a factory for producing individual sockets for each client that connects to the server - conn, in your code. So, conn.sendall() is what you should be using. No address parameter is required, the individual socket already knows who it is talking to. (.send() is unreliable without some extra work on your part; .sendto() is only used with UDP sockets that have not been connected to a particular client.)

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

1 Comment

Thanks! That did it! This is what I wrote on the server side: try: conn,addr = s.accept() dataP = pickle.dumps(dataDict) #dataDict is the dictionary I want to send conn.sendall(dataP) I took off the typecasting to str and the encoding part encode.() For the client side: while True: data, addr = s.recvfrom(buffer) dataP = pickle.loads(data) print(dataP) I should also mention that I did this in the creation of the sockets for both client and server: s = socket.socket() That's it! Thanks all 4 helpin!

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.