OSError: [Errno 57] Socket is not connected2019 Community Moderator ElectionWhat is the difference between a port and a socket?Sockets with Tor and PythonWhy am I getting the error “connection refused” in Python? (Sockets)basic understanding of simple socket serverPython: Socket.error Connection Refused in Unix [Errno 111]I get an error when trying to receive data from the server in a python UDP socket using recvfrom()Python socket programming with multiple servers and one clientssl.SSLZeroReturnError: TLS/SSL connection has been closed (EOF) (_ssl.c:661)Broadcasting hello message in adhoc network using socket () in pythonP5js and Python communication via socket

How to copy the rest of lines of a file to another file

What will happen if my luggage gets delayed?

Should we avoid writing fiction about historical events without extensive research?

Finding the minimum value of a function without using Calculus

Volume of hyperbola revolved about the y -axis

Why is there an extra space when I type "ls" on the Desktop?

If nine coins are tossed, what is the probability that the number of heads is even?

Is it a Cyclops number? "Nobody" knows!

Use Mercury as quenching liquid for swords?

-1 to the power of a irrational number

Having the player face themselves after the mid-game

Are small insurances worth it?

Do black holes violate the conservation of mass?

Is it possible to clone a polymorphic object without manually adding overridden clone method into each derived class in C++?

Is it appropriate to ask a former professor to order a book for me through an inter-library loan?

Was it really inappropriate to write a pull request for the company I interviewed with?

Are these two graphs isomorphic? Why/Why not?

How can I portion out frozen cookie dough?

What do you call someone who likes to pick fights?

If sound is a longitudinal wave, why can we hear it if our ears aren't aligned with the propagation direction?

Is this Paypal Github SDK reference really a dangerous site?

Help! My Character is too much for her story!

Does an unused member variable take up memory?

What is better: yes / no radio, or simple checkbox?



OSError: [Errno 57] Socket is not connected



2019 Community Moderator ElectionWhat is the difference between a port and a socket?Sockets with Tor and PythonWhy am I getting the error “connection refused” in Python? (Sockets)basic understanding of simple socket serverPython: Socket.error Connection Refused in Unix [Errno 111]I get an error when trying to receive data from the server in a python UDP socket using recvfrom()Python socket programming with multiple servers and one clientssl.SSLZeroReturnError: TLS/SSL connection has been closed (EOF) (_ssl.c:661)Broadcasting hello message in adhoc network using socket () in pythonP5js and Python communication via socket










0















I'm trying to understand networking on python using the socket module, but I think the problem is that I don't really understand how a server or client actually functions.



What I am trying to do:



  • Send a dictionary from the client to the server by turning it to a string first using JSON

  • Make the server send back a message to the client to confirm they are connected

Server program



import socket
import json

HOST = '192.168.X.X'
PORT = 50034


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()

with conn:
while True:
data = conn.recv(1024)
new_data = data.decode()
new_data = json.loads(data)
print(new_data["Name"],"has connected from",addr)
message = ("Hello %s welcome to the chat server" % new_data["Name"]).encode()
s.sendall(message)
if not data:
conn.sendall(data)


Client program



import socket
import json

HOST = '192.168.X.X'
PORT = 50034

my_dict = "Name":"Bob"
jsn = json.dumps(my_dict)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST,PORT))
s.sendall((jsn).encode())
data = s.recv(1024)
print(data)


But I am getting the error:




OSError: [Errno 57] Socket is not connected




Why?










share|improve this question






















  • You are trying to send via the listening socket.

    – user207421
    Mar 6 at 21:58











  • What is the listening socket? Is it the code inside the with conn:?

    – Newbie101
    Mar 6 at 21:59












  • s is the listening socket, the one you called listen() on and accept from, and s.send(...) is where you try to send from it, and where you get this error.

    – user207421
    Mar 6 at 22:09












  • So how would I send data outside the listening socket?

    – Newbie101
    Mar 6 at 22:12











  • Same way you are reading it?

    – user207421
    Mar 6 at 22:21















0















I'm trying to understand networking on python using the socket module, but I think the problem is that I don't really understand how a server or client actually functions.



What I am trying to do:



  • Send a dictionary from the client to the server by turning it to a string first using JSON

  • Make the server send back a message to the client to confirm they are connected

Server program



import socket
import json

HOST = '192.168.X.X'
PORT = 50034


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()

with conn:
while True:
data = conn.recv(1024)
new_data = data.decode()
new_data = json.loads(data)
print(new_data["Name"],"has connected from",addr)
message = ("Hello %s welcome to the chat server" % new_data["Name"]).encode()
s.sendall(message)
if not data:
conn.sendall(data)


Client program



import socket
import json

HOST = '192.168.X.X'
PORT = 50034

my_dict = "Name":"Bob"
jsn = json.dumps(my_dict)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST,PORT))
s.sendall((jsn).encode())
data = s.recv(1024)
print(data)


But I am getting the error:




OSError: [Errno 57] Socket is not connected




Why?










share|improve this question






















  • You are trying to send via the listening socket.

    – user207421
    Mar 6 at 21:58











  • What is the listening socket? Is it the code inside the with conn:?

    – Newbie101
    Mar 6 at 21:59












  • s is the listening socket, the one you called listen() on and accept from, and s.send(...) is where you try to send from it, and where you get this error.

    – user207421
    Mar 6 at 22:09












  • So how would I send data outside the listening socket?

    – Newbie101
    Mar 6 at 22:12











  • Same way you are reading it?

    – user207421
    Mar 6 at 22:21













0












0








0








I'm trying to understand networking on python using the socket module, but I think the problem is that I don't really understand how a server or client actually functions.



What I am trying to do:



  • Send a dictionary from the client to the server by turning it to a string first using JSON

  • Make the server send back a message to the client to confirm they are connected

Server program



import socket
import json

HOST = '192.168.X.X'
PORT = 50034


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()

with conn:
while True:
data = conn.recv(1024)
new_data = data.decode()
new_data = json.loads(data)
print(new_data["Name"],"has connected from",addr)
message = ("Hello %s welcome to the chat server" % new_data["Name"]).encode()
s.sendall(message)
if not data:
conn.sendall(data)


Client program



import socket
import json

HOST = '192.168.X.X'
PORT = 50034

my_dict = "Name":"Bob"
jsn = json.dumps(my_dict)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST,PORT))
s.sendall((jsn).encode())
data = s.recv(1024)
print(data)


But I am getting the error:




OSError: [Errno 57] Socket is not connected




Why?










share|improve this question














I'm trying to understand networking on python using the socket module, but I think the problem is that I don't really understand how a server or client actually functions.



What I am trying to do:



  • Send a dictionary from the client to the server by turning it to a string first using JSON

  • Make the server send back a message to the client to confirm they are connected

Server program



import socket
import json

HOST = '192.168.X.X'
PORT = 50034


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()

with conn:
while True:
data = conn.recv(1024)
new_data = data.decode()
new_data = json.loads(data)
print(new_data["Name"],"has connected from",addr)
message = ("Hello %s welcome to the chat server" % new_data["Name"]).encode()
s.sendall(message)
if not data:
conn.sendall(data)


Client program



import socket
import json

HOST = '192.168.X.X'
PORT = 50034

my_dict = "Name":"Bob"
jsn = json.dumps(my_dict)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST,PORT))
s.sendall((jsn).encode())
data = s.recv(1024)
print(data)


But I am getting the error:




OSError: [Errno 57] Socket is not connected




Why?







python json sockets






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 6 at 21:41









Newbie101Newbie101

10910




10910












  • You are trying to send via the listening socket.

    – user207421
    Mar 6 at 21:58











  • What is the listening socket? Is it the code inside the with conn:?

    – Newbie101
    Mar 6 at 21:59












  • s is the listening socket, the one you called listen() on and accept from, and s.send(...) is where you try to send from it, and where you get this error.

    – user207421
    Mar 6 at 22:09












  • So how would I send data outside the listening socket?

    – Newbie101
    Mar 6 at 22:12











  • Same way you are reading it?

    – user207421
    Mar 6 at 22:21

















  • You are trying to send via the listening socket.

    – user207421
    Mar 6 at 21:58











  • What is the listening socket? Is it the code inside the with conn:?

    – Newbie101
    Mar 6 at 21:59












  • s is the listening socket, the one you called listen() on and accept from, and s.send(...) is where you try to send from it, and where you get this error.

    – user207421
    Mar 6 at 22:09












  • So how would I send data outside the listening socket?

    – Newbie101
    Mar 6 at 22:12











  • Same way you are reading it?

    – user207421
    Mar 6 at 22:21
















You are trying to send via the listening socket.

– user207421
Mar 6 at 21:58





You are trying to send via the listening socket.

– user207421
Mar 6 at 21:58













What is the listening socket? Is it the code inside the with conn:?

– Newbie101
Mar 6 at 21:59






What is the listening socket? Is it the code inside the with conn:?

– Newbie101
Mar 6 at 21:59














s is the listening socket, the one you called listen() on and accept from, and s.send(...) is where you try to send from it, and where you get this error.

– user207421
Mar 6 at 22:09






s is the listening socket, the one you called listen() on and accept from, and s.send(...) is where you try to send from it, and where you get this error.

– user207421
Mar 6 at 22:09














So how would I send data outside the listening socket?

– Newbie101
Mar 6 at 22:12





So how would I send data outside the listening socket?

– Newbie101
Mar 6 at 22:12













Same way you are reading it?

– user207421
Mar 6 at 22:21





Same way you are reading it?

– user207421
Mar 6 at 22:21












1 Answer
1






active

oldest

votes


















0














In your server code, you need to change ...



s.sendall(message)


... to ...



conn.sendall(message)


This is because when you do ...



conn, addr = s.accept()


... conn is a new socket instance for that connection. So that's the object you should use for sending and receiving data on that connection. Before you call accept() to accept a connection, you don't have a socket that's actually connected to anything.






share|improve this answer

























  • Or afterwards. The listening socket is never connected.

    – user207421
    2 days ago











  • @user207421 I clarified the wording.

    – jnrbsn
    2 days ago










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032621%2foserror-errno-57-socket-is-not-connected%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














In your server code, you need to change ...



s.sendall(message)


... to ...



conn.sendall(message)


This is because when you do ...



conn, addr = s.accept()


... conn is a new socket instance for that connection. So that's the object you should use for sending and receiving data on that connection. Before you call accept() to accept a connection, you don't have a socket that's actually connected to anything.






share|improve this answer

























  • Or afterwards. The listening socket is never connected.

    – user207421
    2 days ago











  • @user207421 I clarified the wording.

    – jnrbsn
    2 days ago















0














In your server code, you need to change ...



s.sendall(message)


... to ...



conn.sendall(message)


This is because when you do ...



conn, addr = s.accept()


... conn is a new socket instance for that connection. So that's the object you should use for sending and receiving data on that connection. Before you call accept() to accept a connection, you don't have a socket that's actually connected to anything.






share|improve this answer

























  • Or afterwards. The listening socket is never connected.

    – user207421
    2 days ago











  • @user207421 I clarified the wording.

    – jnrbsn
    2 days ago













0












0








0







In your server code, you need to change ...



s.sendall(message)


... to ...



conn.sendall(message)


This is because when you do ...



conn, addr = s.accept()


... conn is a new socket instance for that connection. So that's the object you should use for sending and receiving data on that connection. Before you call accept() to accept a connection, you don't have a socket that's actually connected to anything.






share|improve this answer















In your server code, you need to change ...



s.sendall(message)


... to ...



conn.sendall(message)


This is because when you do ...



conn, addr = s.accept()


... conn is a new socket instance for that connection. So that's the object you should use for sending and receiving data on that connection. Before you call accept() to accept a connection, you don't have a socket that's actually connected to anything.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









jnrbsnjnrbsn

1,64211219




1,64211219












  • Or afterwards. The listening socket is never connected.

    – user207421
    2 days ago











  • @user207421 I clarified the wording.

    – jnrbsn
    2 days ago

















  • Or afterwards. The listening socket is never connected.

    – user207421
    2 days ago











  • @user207421 I clarified the wording.

    – jnrbsn
    2 days ago
















Or afterwards. The listening socket is never connected.

– user207421
2 days ago





Or afterwards. The listening socket is never connected.

– user207421
2 days ago













@user207421 I clarified the wording.

– jnrbsn
2 days ago





@user207421 I clarified the wording.

– jnrbsn
2 days ago



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032621%2foserror-errno-57-socket-is-not-connected%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229