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
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
add a comment |
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
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 thewith conn:?
– Newbie101
Mar 6 at 21:59
sis the listening socket, the one you calledlisten()on and accept from, ands.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
add a comment |
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
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
python json sockets
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 thewith conn:?
– Newbie101
Mar 6 at 21:59
sis the listening socket, the one you calledlisten()on and accept from, ands.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
add a comment |
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 thewith conn:?
– Newbie101
Mar 6 at 21:59
sis the listening socket, the one you calledlisten()on and accept from, ands.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
add a comment |
1 Answer
1
active
oldest
votes
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.
Or afterwards. The listening socket is never connected.
– user207421
2 days ago
@user207421 I clarified the wording.
– jnrbsn
2 days ago
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Or afterwards. The listening socket is never connected.
– user207421
2 days ago
@user207421 I clarified the wording.
– jnrbsn
2 days ago
add a comment |
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.
Or afterwards. The listening socket is never connected.
– user207421
2 days ago
@user207421 I clarified the wording.
– jnrbsn
2 days ago
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
sis the listening socket, the one you calledlisten()on and accept from, ands.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