Sending and receiving files on socket

You need to: Send the length of the file ahead of the file. You can use DataOutputStream.writeLong() for that, and DataInputStream.readLong() at the receiver. Read exactly that many bytes from the stream at the receiver: while (total < length && (count = in.read(buffer, 0, length-total > buffer.length ? buffer.length : (int)(length-total))) > 0) { out.write(buffer, … Read more

Get the Default Gateway

It will probably be the first valid and enabled gateway address of the first enabled network interface: public static IPAddress GetDefaultGateway() { return NetworkInterface .GetAllNetworkInterfaces() .Where(n => n.OperationalStatus == OperationalStatus.Up) .Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback) .SelectMany(n => n.GetIPProperties()?.GatewayAddresses) .Select(g => g?.Address) .Where(a => a != null) // .Where(a => a.AddressFamily == AddressFamily.InterNetwork) // .Where(a => … Read more

Application control of TCP retransmission on Linux

Looks like this was added in Kernel 2.6.37. Commit diff from kernel Git and Excerpt from change log below; commit dca43c75e7e545694a9dd6288553f55c53e2a3a3 Author: Jerry Chu Date: Fri Aug 27 19:13:28 2010 +0000 tcp: Add TCP_USER_TIMEOUT socket option. This patch provides a “user timeout” support as described in RFC793. The socket option is also needed for the … Read more