public void Listen_internal(int backlog, out int error) { error = 0; if (jSocket == null || !jSocket.isBound()) { error = 10022; //WSAEINVAL (Invalid argument) return; } if (jSocket.isConnected() || jSocketChannel.isConnectionPending()) { error = 10056; //WSAEISCONN (Socket is already connected) return; } bool blockMode = jSocketChannel.isBlocking(); bool reuseAddr = jSocket.getReuseAddress(); try { jSocket.close(); } catch (Exception e) { #if DEBUG Console.WriteLine("Caught exception during Listen_internal close old jSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace); #endif } try { jSocketChannel.close(); } catch (Exception e) { #if DEBUG Console.WriteLine("Caught exception during Listen_internal close old jSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace); #endif } jSocket = null; jSocketChannel = null; try { jServerSocketChannel = java.nio.channels.ServerSocketChannel.open(); jServerSocket = jServerSocketChannel.socket(); jServerSocket.bind(jTempLocalSocketAddress, backlog); jServerSocketChannel.configureBlocking(blockMode); jServerSocket.setReuseAddress(reuseAddr); } catch (Exception e) { error = 10048; //WSAEADDRINUSE (Address already in use) #if DEBUG Console.WriteLine("Caught exception during Listen_internal create server socket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace); #endif } }
public void Close_internal(out int error) { error = 0; if (jServerSocket != null) { try { jServerSocket.close(); } catch (Exception e) { error = 10022; //WSAEINVAL (Invalid argument) #if DEBUG Console.WriteLine("Caught exception during Close_internal jServerSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace); #endif } try { jServerSocketChannel.close(); } catch (Exception e) { error = 10022; //WSAEINVAL (Invalid argument) #if DEBUG Console.WriteLine("Caught exception during Close_internal jServerSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace); #endif } jServerSocket = null; jServerSocketChannel = null; } else if (jSocket != null) { try { jSocket.close(); } catch (Exception e) { error = 10022; //WSAEINVAL (Invalid argument) #if DEBUG Console.WriteLine("Caught exception during Close_internal jSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace); #endif } try { jSocketChannel.close(); } catch (Exception e) { error = 10022; //WSAEINVAL (Invalid argument) #if DEBUG Console.WriteLine("Caught exception during Close_internal jSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace); #endif } jSocket = null; jSocketChannel = null; } }
public Sniff(String [] args) // throws java.io.IOException, java.net.UnknownHostException, InterruptedException { : base(args) { int listen_port = option.intt("listen-port", "port number to listen to", 6001); int talk_port = option.intt("talk-port", "port number to talk to", 6000); about("0.1", "network middle-layer sniffer", "Stephen Tse <*****@*****.**>", "http://escher.sourceforge.net/"); if (help_option) { return; } Socket listen_socket = new java.net.ServerSocket( listen_port).accept(); InputStream listen_is = listen_socket.getInputStream(); OutputStream listen_os = listen_socket.getOutputStream(); Socket talk_socket = new Socket("127.0.0.1", talk_port); InputStream talk_is = talk_socket.getInputStream(); OutputStream talk_os = talk_socket.getOutputStream(); while (true) { bool something = false; while (listen_is.available() > 0) { something = true; int data = listen_is.read(); talk_os.write(data); Console.WriteLine("> " + Data.byte_to_string(data)); } while (talk_is.available() > 0) { something = true; int data = talk_is.read(); listen_os.write(data); Console.WriteLine("< " + Data.byte_to_string(data)); } if (!something) { gnu.util.Misc.sleep(100); } } }
public void Bind(EndPoint local_end) { var lEndPoint = (IPEndPoint)local_end; if (lEndPoint.Address == null) { lEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), lEndPoint.Port); } #if cooper var lAddress = java.net.InetAddress.getByAddress(lEndPoint.Address.GetAddressBytes()); fSocketAddress = new java.net.InetSocketAddress(lAddress, lEndPoint.Port); fIsServer = true; fServerHandle = new java.net.ServerSocket(); #else void *lPointer; int lSize; #if posix || toffee || darwin rtl.__struct_sockaddr_in lIPv4; rtl.__struct_sockaddr_in6 lIPv6; #if posix && !darwin && !android rtl.__CONST_SOCKADDR_ARG lSockAddr; #endif #else rtl.SOCKADDR_IN lIPv4; sockaddr_in6 lIPv6; #endif IPEndPointToNative(lEndPoint, out lIPv4, out lIPv6, out lPointer, out lSize); #if posix && !darwin && !android lSockAddr.__sockaddr__ = (rtl.__struct_sockaddr *)lPointer; lSockAddr.__sockaddr_in__ = (rtl.__struct_sockaddr_in *)lPointer; if (rtl.__Global.bind(fHandle, lSockAddr, lSize) != 0) { throw new Exception("Error calling bind function"); } #elif toffee || darwin || android if (rtl.bind(fHandle, (rtl.__struct_sockaddr *)lPointer, lSize) != 0) { throw new Exception("Error calling bind function"); } #elif island && windows if (rtl.bind(fHandle, lPointer, lSize) != 0) { throw new Exception("Error calling bind function"); } #else throw new Exception("Error calling bind function"); #endif #endif LocalEndPoint = lEndPoint; }
// throws java.io.IOException, java.net.UnknownHostException, InterruptedException { public Sniff(String [] args) : base(args) { int listen_port = option.intt ("listen-port", "port number to listen to", 6001); int talk_port = option.intt ("talk-port", "port number to talk to", 6000); about ("0.1", "network middle-layer sniffer", "Stephen Tse <*****@*****.**>", "http://escher.sourceforge.net/"); if (help_option) return; Socket listen_socket = new java.net.ServerSocket ( listen_port).accept (); InputStream listen_is = listen_socket.getInputStream (); OutputStream listen_os = listen_socket.getOutputStream (); Socket talk_socket = new Socket ("127.0.0.1", talk_port); InputStream talk_is = talk_socket.getInputStream (); OutputStream talk_os = talk_socket.getOutputStream (); while (true) { bool something = false; while (listen_is.available () > 0) { something = true; int data = listen_is.read (); talk_os.write (data); Console.WriteLine ("> " + Data.byte_to_string (data)); } while (talk_is.available () > 0) { something = true; int data = talk_is.read (); listen_os.write (data); Console.WriteLine ("< " + Data.byte_to_string (data)); } if (!something) gnu.util.Misc.sleep (100); } }