You didn't specify the exact OS you are using. I found that Linux does have an API call which will reset a TCP connection, I have no idea how portable it is. The way to do it is to use the connect system call on the already connected socket but this time with family AF_UNSPEC.
After you have reset a socket that way it is even possible to connect the socket again with another connect call.
int main(int argc, char** argv)
{
int fd = socket(AF_INET6, SOCK_STREAM, 0);
while (1) {
struct sockaddr_in6 sockaddr = {
.sin6_family = AF_INET6,
.sin6_port = htons(80),
.sin6_flowinfo = 0,
.sin6_scope_id = 0,
};
struct timespec s = {
.tv_sec = 2,
.tv_nsec = 0,
};
/* Connect to port 80 on localhost */
inet_pton(AF_INET6, "::1", &sockaddr.sin6_addr.s6_addr);
connect(fd, (struct sockaddr*)&sockaddr,sizeof(sockaddr));
nanosleep(&s, NULL);
/* Reset previously connected socket */
sockaddr.sin6_family = AF_UNSPEC;
connect(fd, (struct sockaddr*)&sockaddr,sizeof(sockaddr));
nanosleep(&s, NULL);
}
}
SOCK_DESTROYfunction (conditionally included in kernel) that can be used by an appropriately privileged process to close an arbitrary connection, but it's also completely out-of-band from you application socket. Why do you want to terminate with RST anyway?$socket=IO::Socket::INET->new(Listen=1,ReuseAddr=>1,LocalPort=>...,...); $client=$socket->accept(); $client->sockopt(SO_LINGER, pack('II', 1, 0)); close $client. curl and wget always handle this condition with return code 56 and 4 respectively. however netcat nondeterministically somewhen detects "Connection refused by peer" but sometimes not.closewill produce RST if the receive buffer has data that has not yet been read by your application.