How to continuously receive data?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I've created a very simple tcp server in Python. I connect, then the server waits for data to be sent from the client, then the client waits for data to be sent back. While each is waiting, they aren't able to send data. So one side can't send two messages back to back. I'd like to for both sides to listen and still be able to send at all times.How would I go about that?
Server code:
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((ipAddr, port))
print('Server starting on ' + ipAddr + ':' + str(port))
socket.listen(10)
print('Waiting for connection...')
c, addr = socket.accept()
print('Connection recieved from ' + str(addr))
while True:
try:
buf = c.recv(1024)
if buf == 'stop':
break
elif len(buf) > 0:
print(buf)
response = input('>')
c.sendall(str.encode(str(response)))
except:
print('Error')
break
socket.close()
I'd like to be able to send back to back messages from the server to the client while still listening for responses from the client
python sockets
add a comment |
I've created a very simple tcp server in Python. I connect, then the server waits for data to be sent from the client, then the client waits for data to be sent back. While each is waiting, they aren't able to send data. So one side can't send two messages back to back. I'd like to for both sides to listen and still be able to send at all times.How would I go about that?
Server code:
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((ipAddr, port))
print('Server starting on ' + ipAddr + ':' + str(port))
socket.listen(10)
print('Waiting for connection...')
c, addr = socket.accept()
print('Connection recieved from ' + str(addr))
while True:
try:
buf = c.recv(1024)
if buf == 'stop':
break
elif len(buf) > 0:
print(buf)
response = input('>')
c.sendall(str.encode(str(response)))
except:
print('Error')
break
socket.close()
I'd like to be able to send back to back messages from the server to the client while still listening for responses from the client
python sockets
1
One good way to do this is to set your socket to non-blocking mode, and write your event loop around a call toselect()
. The only place the loop should ever block is in theselect()
call, and (assuming you pass in the correct arguments to it) that call will return whenever there is data ready-for-read, and/or whenever there is buffer-space-ready-to-write-to. (Only request that it return for the latter condition if you actually have data to send, of course)
– Jeremy Friesner
Jan 3 at 4:16
Is there an example you could link? I don't totally get what you're saying
– Austin Palmer
Jan 3 at 4:18
Maybe this article will be helpful: medium.com/vaidikkapoor/…
– Jeremy Friesner
Jan 3 at 4:22
add a comment |
I've created a very simple tcp server in Python. I connect, then the server waits for data to be sent from the client, then the client waits for data to be sent back. While each is waiting, they aren't able to send data. So one side can't send two messages back to back. I'd like to for both sides to listen and still be able to send at all times.How would I go about that?
Server code:
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((ipAddr, port))
print('Server starting on ' + ipAddr + ':' + str(port))
socket.listen(10)
print('Waiting for connection...')
c, addr = socket.accept()
print('Connection recieved from ' + str(addr))
while True:
try:
buf = c.recv(1024)
if buf == 'stop':
break
elif len(buf) > 0:
print(buf)
response = input('>')
c.sendall(str.encode(str(response)))
except:
print('Error')
break
socket.close()
I'd like to be able to send back to back messages from the server to the client while still listening for responses from the client
python sockets
I've created a very simple tcp server in Python. I connect, then the server waits for data to be sent from the client, then the client waits for data to be sent back. While each is waiting, they aren't able to send data. So one side can't send two messages back to back. I'd like to for both sides to listen and still be able to send at all times.How would I go about that?
Server code:
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((ipAddr, port))
print('Server starting on ' + ipAddr + ':' + str(port))
socket.listen(10)
print('Waiting for connection...')
c, addr = socket.accept()
print('Connection recieved from ' + str(addr))
while True:
try:
buf = c.recv(1024)
if buf == 'stop':
break
elif len(buf) > 0:
print(buf)
response = input('>')
c.sendall(str.encode(str(response)))
except:
print('Error')
break
socket.close()
I'd like to be able to send back to back messages from the server to the client while still listening for responses from the client
python sockets
python sockets
edited Jan 3 at 4:11
Austin Palmer
asked Jan 3 at 4:06
Austin PalmerAustin Palmer
33
33
1
One good way to do this is to set your socket to non-blocking mode, and write your event loop around a call toselect()
. The only place the loop should ever block is in theselect()
call, and (assuming you pass in the correct arguments to it) that call will return whenever there is data ready-for-read, and/or whenever there is buffer-space-ready-to-write-to. (Only request that it return for the latter condition if you actually have data to send, of course)
– Jeremy Friesner
Jan 3 at 4:16
Is there an example you could link? I don't totally get what you're saying
– Austin Palmer
Jan 3 at 4:18
Maybe this article will be helpful: medium.com/vaidikkapoor/…
– Jeremy Friesner
Jan 3 at 4:22
add a comment |
1
One good way to do this is to set your socket to non-blocking mode, and write your event loop around a call toselect()
. The only place the loop should ever block is in theselect()
call, and (assuming you pass in the correct arguments to it) that call will return whenever there is data ready-for-read, and/or whenever there is buffer-space-ready-to-write-to. (Only request that it return for the latter condition if you actually have data to send, of course)
– Jeremy Friesner
Jan 3 at 4:16
Is there an example you could link? I don't totally get what you're saying
– Austin Palmer
Jan 3 at 4:18
Maybe this article will be helpful: medium.com/vaidikkapoor/…
– Jeremy Friesner
Jan 3 at 4:22
1
1
One good way to do this is to set your socket to non-blocking mode, and write your event loop around a call to
select()
. The only place the loop should ever block is in the select()
call, and (assuming you pass in the correct arguments to it) that call will return whenever there is data ready-for-read, and/or whenever there is buffer-space-ready-to-write-to. (Only request that it return for the latter condition if you actually have data to send, of course)– Jeremy Friesner
Jan 3 at 4:16
One good way to do this is to set your socket to non-blocking mode, and write your event loop around a call to
select()
. The only place the loop should ever block is in the select()
call, and (assuming you pass in the correct arguments to it) that call will return whenever there is data ready-for-read, and/or whenever there is buffer-space-ready-to-write-to. (Only request that it return for the latter condition if you actually have data to send, of course)– Jeremy Friesner
Jan 3 at 4:16
Is there an example you could link? I don't totally get what you're saying
– Austin Palmer
Jan 3 at 4:18
Is there an example you could link? I don't totally get what you're saying
– Austin Palmer
Jan 3 at 4:18
Maybe this article will be helpful: medium.com/vaidikkapoor/…
– Jeremy Friesner
Jan 3 at 4:22
Maybe this article will be helpful: medium.com/vaidikkapoor/…
– Jeremy Friesner
Jan 3 at 4:22
add a comment |
1 Answer
1
active
oldest
votes
while True:
#receiving
client,add=server.accept()
data = client.recv(1024)
str = data.decode('ascii')
print("client is saying: ",str)
#sending
msg = input("server:")
a= msg.encode('ascii')
client.send(a)
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%2f54016220%2fhow-to-continuously-receive-data%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
while True:
#receiving
client,add=server.accept()
data = client.recv(1024)
str = data.decode('ascii')
print("client is saying: ",str)
#sending
msg = input("server:")
a= msg.encode('ascii')
client.send(a)
add a comment |
while True:
#receiving
client,add=server.accept()
data = client.recv(1024)
str = data.decode('ascii')
print("client is saying: ",str)
#sending
msg = input("server:")
a= msg.encode('ascii')
client.send(a)
add a comment |
while True:
#receiving
client,add=server.accept()
data = client.recv(1024)
str = data.decode('ascii')
print("client is saying: ",str)
#sending
msg = input("server:")
a= msg.encode('ascii')
client.send(a)
while True:
#receiving
client,add=server.accept()
data = client.recv(1024)
str = data.decode('ascii')
print("client is saying: ",str)
#sending
msg = input("server:")
a= msg.encode('ascii')
client.send(a)
answered Jan 3 at 4:54
sushants367sushants367
163
163
add a comment |
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%2f54016220%2fhow-to-continuously-receive-data%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
1
One good way to do this is to set your socket to non-blocking mode, and write your event loop around a call to
select()
. The only place the loop should ever block is in theselect()
call, and (assuming you pass in the correct arguments to it) that call will return whenever there is data ready-for-read, and/or whenever there is buffer-space-ready-to-write-to. (Only request that it return for the latter condition if you actually have data to send, of course)– Jeremy Friesner
Jan 3 at 4:16
Is there an example you could link? I don't totally get what you're saying
– Austin Palmer
Jan 3 at 4:18
Maybe this article will be helpful: medium.com/vaidikkapoor/…
– Jeremy Friesner
Jan 3 at 4:22