|
Hi,
I wrote a program to test the scenario of lingering upon closing the
socket, but the lingering never happened.
Here is the server source of the server. Is OS/400's SO_LINGER broken?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
int main()
{
extern int errno;
int listenfd, connfd;
int rc;
int ret,on=1;
char buff[80];
char s;
struct linger sling;
struct sockaddr_in servaddr;
listenfd=socket(AF_INET, SOCK_STREAM, 0);
if (listenfd<0){
perror("socket()");
exit(0);
}
rc=setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof(on));
if (rc<0){
perror("setsockopt REUSE");
exit(0);
}
sling.l_onoff=1;
sling.l_linger=60;
ret=setsockopt(listenfd,SOL_SOCKET,SO_LINGER,(char *) &sling,sizeof(struct
linger));
if (ret<0){
perror ("setsockopt LINGER");
exit(0);
}
memset(&servaddr,0x00,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(13000);
rc=bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
if (rc<0) {
perror("bind()");
exit(0);
}
rc=listen(listenfd,100);
if (rc<0) {
perror("listen()");
exit(0);
}
connfd=accept(listenfd,NULL,NULL);
if (connfd<0) {
perror("accept()");
exit(0);
}
printf("Accepted.\n");
rc=recv(connfd,buff,sizeof(buff),0);
if (rc<0) {
perror("recv()");
exit(0);
}
printf("Received: %s\n",buff);
if ((rc=send(connfd,buff,sizeof(buff),0))<=0){
perror("send() 1 failed");
exit(0);
}
sleep(10); // unplug the cable at the client side
rc=send(connfd,buff,sizeof(buff),0);
if (rc<=0){
perror("send() 2 failed");
exit(0);
}
if (listenfd !=-1) {
ret=close(listenfd);
if (ret<0) {
perror("Linger 1");
exit(0);
}
printf("listenfd closed.");
}
if (connfd !=-1) {
ret=close(connfd);
if (ret<0) {
perror("Linger 2");
exit(0);
}
printf("connfd closed.");
}
printf("Program ended normally.\n");
gets(&s);
return;
}
Here is how I tested it. I start this server, and then I start the
client, the client just send some random characters I keyed in, and
then the client read the socket for the response. Once the client have
the response, I disconnect my client from the network, which I think
will result in the second send from the server unacknowledged
( See sleep(10) above).
My question is why this close() doesn't linger? Every time the
program displays "Program ended normally".
Thanks!
This mailing list archive is Copyright 1997-2026 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.