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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page