Hi! When I'm trying to write to a closed socket (in my project I wanted to use this, to inform the other side about an abortion) the data is written successfully. Shouldn't the be an error or an EOF or something? Reading on a closed socket is working resp. read returns 0.
Comprehensible code:
Comprehensible code:
C:
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
//server
int main()
{
int sd;
int client;
struct sockaddr_in sa;
int reuse = 1;
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd == -1)
{
printf("socket\n");
return 1;
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(12345);
sa.sin_addr.s_addr = htonl(INADDR_ANY);
if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
{
printf("setsockopt\n");
return 1;
}
if(bind(sd, (struct sockaddr *) &sa, sizeof(sa)) == -1)
{
printf("bind\n");
return 1;
}
if(listen(sd, 1) == -1)
{
printf("listen\n");
return 1;
}
if((client = accept(sd, NULL, NULL)) == -1)
{
printf("accept\n");
return 1;
}
if(close(sd) == -1)
{
printf("close sd\n");
return 1;
}
if(close(client) == -1)
{
printf("close client\n");
return 1;
}
printf("success\n");
return 0;
}
C:
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
//client
int main()
{
int sd;
struct sockaddr_in sa;
int reuse = 1;
ssize_t bytessent;
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd == -1)
{
printf("socket\n");
return 1;
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(12345);
if(inet_pton(AF_INET, "127.0.0.1", &sa.sin_addr) != 1)
{
printf("inet_pton\n");
return 1;
}
if(connect(sd, (struct sockaddr *) &sa, sizeof(sa)) == -1)
{
printf("connect\n");
return 1;
}
//wait until the server closed the connection
sleep(5);
bytessent = write(sd, "abcdefgh", 8);
if(bytessent <= 0)
{
printf("write\n");
return 1;
}
if(close(sd) == -1)
{
printf("close sd\n");
return 1;
}
printf("success\n");
return 0;
}