Skip to content

Commit 1d831f5

Browse files
authored
Merge pull request #2 from IoTSharp/master
Check the TCP connection status before writing
2 parents 7a39502 + c0a9320 commit 1d831f5

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

platform/linux/platform_net_socket.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,24 @@ int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int ti
8080
}
8181
return len - nleft;
8282
}
83+
ssize_t write_check_tcp(int __fd, __const void* __buf, size_t __n)
84+
{
85+
struct tcp_info info;
86+
socklen_t _len = sizeof(info);
87+
getsockopt(__fd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t*)&_len);
88+
if ((info.tcpi_state == TCP_ESTABLISHED))
89+
{
90+
return write(__fd, __buf, __n);
91+
}
92+
else
93+
{
94+
return -1;
95+
}
96+
}
8397

8498
int platform_net_socket_write(int fd, void *buf, size_t len)
8599
{
86-
return write(fd, buf, len);
100+
return write_check_tcp(fd, buf, len);
87101
}
88102

89103
int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout)
@@ -99,8 +113,8 @@ int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int t
99113
}
100114

101115
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));
102-
103-
return write(fd, buf, len);
116+
117+
return write_check_tcp(fd, buf, len);
104118
}
105119

106120
int platform_net_socket_close(int fd)

0 commit comments

Comments
 (0)