List of usage examples for java.net Socket getPort
public int getPort()
From source file:Main.java
public static void main(String[] args) throws Exception { Socket client = new Socket("google.com", 80); System.out.println(client.getPort()); client.close();/*w w w. j ava 2 s. co m*/ }
From source file:MainClass.java
public static void main(String[] args) throws IOException { int port = 2345; ServerSocket ss = new ServerSocket(port); while (true) { try {/* www .ja v a 2 s . c o m*/ Socket s = ss.accept(); String response = "Hello " + s.getInetAddress() + " on port " + s.getPort() + "\r\n"; response += "This is " + s.getLocalAddress() + " on port " + s.getLocalPort() + "\r\n"; OutputStream out = s.getOutputStream(); out.write(response.getBytes("US-ASCII")); out.flush(); s.close(); } catch (IOException ex) { } } }
From source file:getSocketInfo.java
public static void main(String[] args) { for (int i = 0; i < args.length; i++) { try {/*ww w. ja v a 2 s . c o m*/ Socket theSocket = new Socket(args[i], 80); System.out.println("Connected to " + theSocket.getInetAddress() + " on port " + theSocket.getPort() + " from port " + theSocket.getLocalPort() + " of " + theSocket.getLocalAddress()); } // end try catch (UnknownHostException e) { System.err.println("I can't find " + args[i]); } catch (SocketException e) { System.err.println("Could not connect to " + args[i]); } catch (IOException e) { System.err.println(e); } } }
From source file:flink.iso8583.example.Server.java
public static void main(String[] args) throws Exception { mfact = ConfigParser.createFromClasspathConfig("flink/iso8583/example/config.xml"); log.info("Setting up server socket..."); ServerSocket server = new ServerSocket(9999); log.info("Waiting for connections..."); while (true) { Socket sock = server.accept(); log.info("New connection from " + sock.getInetAddress() + ":" + sock.getPort()); new Server(sock).start(); }//from w ww .j a va2 s . c o m }
From source file:j8583.example.Server.java
public static void main(String[] args) throws Exception { mfact = ConfigParser.createFromClasspathConfig("j8583/example/config.xml"); System.err.println("Setting up server socket..."); ServerSocket server = new ServerSocket(9999); System.err.println("Waiting for connections..."); while (true) { Socket sock = server.accept(); System.err.println(String.format("New connection from %s:%d", sock.getInetAddress(), sock.getPort())); new Thread(new Server(sock), "j8583-server").start(); }/*from w w w . j a v a2 s . co m*/ }
From source file:GenericClient.java
public static void main(String[] args) throws IOException { try {/* w ww .j a va2 s .com*/ // Check the number of arguments if (args.length != 2) throw new IllegalArgumentException("Wrong number of args"); // Parse the host and port specifications String host = args[0]; int port = Integer.parseInt(args[1]); // Connect to the specified host and port Socket s = new Socket(host, port); // Set up streams for reading from and writing to the server. // The from_server stream is final for use in the inner class below final Reader from_server = new InputStreamReader(s.getInputStream()); PrintWriter to_server = new PrintWriter(s.getOutputStream()); // Set up streams for reading from and writing to the console // The to_user stream is final for use in the anonymous class below BufferedReader from_user = new BufferedReader(new InputStreamReader(System.in)); // Pass true for auto-flush on println() final PrintWriter to_user = new PrintWriter(System.out, true); // Tell the user that we've connected to_user.println("Connected to " + s.getInetAddress() + ":" + s.getPort()); // Create a thread that gets output from the server and displays // it to the user. We use a separate thread for this so that we // can receive asynchronous output Thread t = new Thread() { public void run() { char[] buffer = new char[1024]; int chars_read; try { // Read characters from the server until the // stream closes, and write them to the console while ((chars_read = from_server.read(buffer)) != -1) { to_user.write(buffer, 0, chars_read); to_user.flush(); } } catch (IOException e) { to_user.println(e); } // When the server closes the connection, the loop above // will end. Tell the user what happened, and call // System.exit(), causing the main thread to exit along // with this one. to_user.println("Connection closed by server."); System.exit(0); } }; // Now start the server-to-user thread t.start(); // In parallel, read the user's input and pass it on to the server. String line; while ((line = from_user.readLine()) != null) { to_server.print(line + "\r\n"); to_server.flush(); } // If the user types a Ctrl-D (Unix) or Ctrl-Z (Windows) to end // their input, we'll get an EOF, and the loop above will exit. // When this happens, we stop the server-to-user thread and close // the socket. s.close(); to_user.println("Connection closed by client."); System.exit(0); } // If anything goes wrong, print an error message catch (Exception e) { System.err.println(e); System.err.println("Usage: java GenericClient <hostname> <port>"); } }
From source file:paxos.GameServer.java
public static void main(String args[]) { Game myGame = new Game(); Socket socket = null; ServerSocket GameServer = null; System.out.println("Server Listening......"); try {// w ww . j a v a2s. c o m GameServer = new ServerSocket(port); } catch (IOException e) { e.printStackTrace(); System.out.println("Server error"); } while (true) { try { socket = GameServer.accept(); String clientAddress = socket.getRemoteSocketAddress().toString(); int clientPort = socket.getPort(); System.out.println("Connection established. Client: " + clientAddress + "on port " + clientPort); ServerThread st = new ServerThread(socket, myGame); st.start(); } catch (Exception e) { e.printStackTrace(); System.out.println("Connection Error"); } } }
From source file:com.mirth.connect.connectors.tcp.SocketUtil.java
public static String getInetAddress(Socket socket) { String inetAddress = socket == null || socket.getInetAddress() == null ? "" : socket.getInetAddress().toString() + ":" + socket.getPort(); if (inetAddress.startsWith("/")) { inetAddress = inetAddress.substring(1); }//from w ww. j a va 2s. c om return inetAddress; }
From source file:Main.java
/** * Renders the details of a socket in the returned string * @param socket The socket to render/*from w w w.j a v a 2s. c om*/ * @return the details of the socket as a string */ public static String render(Socket socket) { if (socket == null) return "NULL"; StringBuilder b = new StringBuilder("\nSocket ["); b.append("\n\tLocalPort:").append(socket.getLocalPort()); b.append("\n\tLocalAddress:").append(socket.getLocalAddress()); b.append("\n\tLocalSocketAddress:").append(socket.getLocalSocketAddress()); b.append("\n\tRemotePort:").append(socket.getPort()); b.append("\n\tRemoteAddress:").append(socket.getInetAddress()); b.append("\n\tRemoteSocketAddress:").append(socket.getRemoteSocketAddress()); b.append("\n\tChannel:").append(socket.getChannel()); b.append("\n\tHashCode:").append(socket.hashCode()); b.append("\n]"); return b.toString(); }
From source file:com.nokia.dempsy.messagetransport.tcp.TcpReceiver.java
private static String getClientDescription(Socket socket) { try {/*from w ww . j a v a2 s. co m*/ return "(" + socket.getInetAddress().getHostAddress() + ":" + socket.getPort() + ")"; } catch (Throwable th) { return "(Unknown Client)"; } }