Find the next TCP port in .NET
Here is what I was looking for: static int FreeTcpPort() { TcpListener l = new TcpListener(IPAddress.Loopback, 0); l.Start(); int port = ((IPEndPoint)l.LocalEndpoint).Port; l.Stop(); return port; }
Here is what I was looking for: static int FreeTcpPort() { TcpListener l = new TcpListener(IPAddress.Loopback, 0); l.Start(); int port = ((IPEndPoint)l.LocalEndpoint).Port; l.Stop(); return port; }
Run lsof -P. And make sure the P goes before the i, if you combine the option with -i: lsof -Pi According to man lsof, -P inhibits the conversion of port numbers to port names for network files. Inhibiting the conversion may make lsof run a little faster. It is also useful when port name … Read more
First of all not both of them disables Nagle’s algorithm. Nagle’s algorithm is for reducing more number of small network packets in wire. The algorithm is: if data is smaller than a limit (usually MSS), wait until receiving ACK for previously sent packets and in the mean time accumulate data from user. Then send the … Read more
what are the differences between pipes and sockets, and when/how should you choose one over the other? Both pipes and sockets handle byte streams, but they do it in different ways… pipes only exist within a specific host, and they refer to buffering between virtual files, or connecting the output / input of processes within … Read more
You have to close the stream before closing the connection: tcpClient.GetStream().Close(); tcpClient.Close(); Closing the client does not close the stream.
UDP: Anything where you don’t care too much if you get all data always Tunneling/VPN (lost packets are ok – the tunneled protocol takes care of it) Media streaming (lost frames are ok) Games that don’t care if you get every update Local broadcast mechanisms (same application running on different machines “discovering” each other) TCP: … Read more
It sounds like what you’re waiting for is WebRTC which is working it’s way through the standards process. WebSockets, as other people have pointed out, run over TCP as a result of initiating with an HTTP Upgrade.
Since there are no events available to signal when the socket is disconnected, you will have to poll it at a frequency that is acceptable to you. Using this extension method, you can have a reliable method to detect if a socket is disconnected. static class SocketExtensions { public static bool IsConnected(this Socket socket) { … Read more
The isConnected method won’t help, it will return true even if the remote side has closed the socket. Try this: public class MyServer { public static final int PORT = 12345; public static void main(String[] args) throws IOException, InterruptedException { ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(PORT); Socket s = ss.accept(); Thread.sleep(5000); ss.close(); s.close(); } } public class … Read more
I achieved 1600k concurrent idle socket connections, and at the same time 57k req/s on a Linux desktop (16G RAM, I7 2600 CPU). It’s a single thread http server written in C with epoll. Source code is on github, a blog here. Edit: I did 600k concurrent HTTP connections (client & server) on both the … Read more