C sockets - client continue reading after close file descriptor on server side











up vote
0
down vote

favorite












I'm going to conclude an accademic project written in C: the project consist in a multithreaded chat server using AF_UNIX sockets, so both client and server run on the same machine.



In summary the program run a main thread where happends the initialization (structures, data, ect..) and a pool of threads that serve clients request.



The problem appear when a client wait for the last of a list of messages (with a read() call) and for istance i raise a termination signal to the server while the client is still waiting (Let me clarify, this is not the normal sequence of events, but it may happend for some reason), at this point the server does all the closure procedures that it is supposed to do and after that, it finish correctly.



My doubt is about the client because when the server close the file descriptor of the socket where they comunicated before, it endlessy start to recive the second-last message that it recived but i don't know why.



I write some details that can be useful for answer:
-The server (in the main thread) uses epoll function for detecting new connections and new requests
-All messages exchange happends in threads
-Communication happneds through readv and writev



I hope I was enough clear and i will reply for all further clarification.



Thank you all in advance










share|improve this question






















  • Are you checking the return-value your read() calls in the client and responding correctly to it? (I only ask because a lot of new network programmers will ignore the return value and just assume that a full buffer was received, which leads to them believing that they are receiving the same content over and over again when really the read() call returned 0/EOF or a negative/error value instead, and left their buffer un-modified)
    – Jeremy Friesner
    Nov 12 at 17:34










  • The client is not repeatedly receiving the message. Instead, there is a bug in your client code that is causing it to repeatedly output that message.
    – William Pursell
    Nov 12 at 17:34










  • Yes you were both right. The problem was in the client code code in the return value of my readMessage() function. Thanks for your help
    – Alessandro Meschi
    Nov 13 at 8:32

















up vote
0
down vote

favorite












I'm going to conclude an accademic project written in C: the project consist in a multithreaded chat server using AF_UNIX sockets, so both client and server run on the same machine.



In summary the program run a main thread where happends the initialization (structures, data, ect..) and a pool of threads that serve clients request.



The problem appear when a client wait for the last of a list of messages (with a read() call) and for istance i raise a termination signal to the server while the client is still waiting (Let me clarify, this is not the normal sequence of events, but it may happend for some reason), at this point the server does all the closure procedures that it is supposed to do and after that, it finish correctly.



My doubt is about the client because when the server close the file descriptor of the socket where they comunicated before, it endlessy start to recive the second-last message that it recived but i don't know why.



I write some details that can be useful for answer:
-The server (in the main thread) uses epoll function for detecting new connections and new requests
-All messages exchange happends in threads
-Communication happneds through readv and writev



I hope I was enough clear and i will reply for all further clarification.



Thank you all in advance










share|improve this question






















  • Are you checking the return-value your read() calls in the client and responding correctly to it? (I only ask because a lot of new network programmers will ignore the return value and just assume that a full buffer was received, which leads to them believing that they are receiving the same content over and over again when really the read() call returned 0/EOF or a negative/error value instead, and left their buffer un-modified)
    – Jeremy Friesner
    Nov 12 at 17:34










  • The client is not repeatedly receiving the message. Instead, there is a bug in your client code that is causing it to repeatedly output that message.
    – William Pursell
    Nov 12 at 17:34










  • Yes you were both right. The problem was in the client code code in the return value of my readMessage() function. Thanks for your help
    – Alessandro Meschi
    Nov 13 at 8:32















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm going to conclude an accademic project written in C: the project consist in a multithreaded chat server using AF_UNIX sockets, so both client and server run on the same machine.



In summary the program run a main thread where happends the initialization (structures, data, ect..) and a pool of threads that serve clients request.



The problem appear when a client wait for the last of a list of messages (with a read() call) and for istance i raise a termination signal to the server while the client is still waiting (Let me clarify, this is not the normal sequence of events, but it may happend for some reason), at this point the server does all the closure procedures that it is supposed to do and after that, it finish correctly.



My doubt is about the client because when the server close the file descriptor of the socket where they comunicated before, it endlessy start to recive the second-last message that it recived but i don't know why.



I write some details that can be useful for answer:
-The server (in the main thread) uses epoll function for detecting new connections and new requests
-All messages exchange happends in threads
-Communication happneds through readv and writev



I hope I was enough clear and i will reply for all further clarification.



Thank you all in advance










share|improve this question













I'm going to conclude an accademic project written in C: the project consist in a multithreaded chat server using AF_UNIX sockets, so both client and server run on the same machine.



In summary the program run a main thread where happends the initialization (structures, data, ect..) and a pool of threads that serve clients request.



The problem appear when a client wait for the last of a list of messages (with a read() call) and for istance i raise a termination signal to the server while the client is still waiting (Let me clarify, this is not the normal sequence of events, but it may happend for some reason), at this point the server does all the closure procedures that it is supposed to do and after that, it finish correctly.



My doubt is about the client because when the server close the file descriptor of the socket where they comunicated before, it endlessy start to recive the second-last message that it recived but i don't know why.



I write some details that can be useful for answer:
-The server (in the main thread) uses epoll function for detecting new connections and new requests
-All messages exchange happends in threads
-Communication happneds through readv and writev



I hope I was enough clear and i will reply for all further clarification.



Thank you all in advance







c sockets epoll






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 17:31









Alessandro Meschi

367




367












  • Are you checking the return-value your read() calls in the client and responding correctly to it? (I only ask because a lot of new network programmers will ignore the return value and just assume that a full buffer was received, which leads to them believing that they are receiving the same content over and over again when really the read() call returned 0/EOF or a negative/error value instead, and left their buffer un-modified)
    – Jeremy Friesner
    Nov 12 at 17:34










  • The client is not repeatedly receiving the message. Instead, there is a bug in your client code that is causing it to repeatedly output that message.
    – William Pursell
    Nov 12 at 17:34










  • Yes you were both right. The problem was in the client code code in the return value of my readMessage() function. Thanks for your help
    – Alessandro Meschi
    Nov 13 at 8:32




















  • Are you checking the return-value your read() calls in the client and responding correctly to it? (I only ask because a lot of new network programmers will ignore the return value and just assume that a full buffer was received, which leads to them believing that they are receiving the same content over and over again when really the read() call returned 0/EOF or a negative/error value instead, and left their buffer un-modified)
    – Jeremy Friesner
    Nov 12 at 17:34










  • The client is not repeatedly receiving the message. Instead, there is a bug in your client code that is causing it to repeatedly output that message.
    – William Pursell
    Nov 12 at 17:34










  • Yes you were both right. The problem was in the client code code in the return value of my readMessage() function. Thanks for your help
    – Alessandro Meschi
    Nov 13 at 8:32


















Are you checking the return-value your read() calls in the client and responding correctly to it? (I only ask because a lot of new network programmers will ignore the return value and just assume that a full buffer was received, which leads to them believing that they are receiving the same content over and over again when really the read() call returned 0/EOF or a negative/error value instead, and left their buffer un-modified)
– Jeremy Friesner
Nov 12 at 17:34




Are you checking the return-value your read() calls in the client and responding correctly to it? (I only ask because a lot of new network programmers will ignore the return value and just assume that a full buffer was received, which leads to them believing that they are receiving the same content over and over again when really the read() call returned 0/EOF or a negative/error value instead, and left their buffer un-modified)
– Jeremy Friesner
Nov 12 at 17:34












The client is not repeatedly receiving the message. Instead, there is a bug in your client code that is causing it to repeatedly output that message.
– William Pursell
Nov 12 at 17:34




The client is not repeatedly receiving the message. Instead, there is a bug in your client code that is causing it to repeatedly output that message.
– William Pursell
Nov 12 at 17:34












Yes you were both right. The problem was in the client code code in the return value of my readMessage() function. Thanks for your help
– Alessandro Meschi
Nov 13 at 8:32






Yes you were both right. The problem was in the client code code in the return value of my readMessage() function. Thanks for your help
– Alessandro Meschi
Nov 13 at 8:32



















active

oldest

votes











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',
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%2f53267268%2fc-sockets-client-continue-reading-after-close-file-descriptor-on-server-side%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267268%2fc-sockets-client-continue-reading-after-close-file-descriptor-on-server-side%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 send String Array data to Server using php in android

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Is anime1.com a legal site for watching anime?