How to create a socket at a specific port?
SolutionFollowing example shows how to sing Socket constructor of Socket class.And also get Socket details using getLocalPort() getLocalAddress , getInetAddress() & getPort() methods.
import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; public class Main { public static void main(String[] args) { try { InetAddress addr = InetAddress.getByName("74.125.67.100"); Socket theSocket = new Socket(addr, 80); System.out.println("Connected to " + theSocket.getInetAddress() + " on port " + theSocket.getPort() + " from port " + theSocket.getLocalPort() + " of " + theSocket.getLocalAddress()); } catch (UnknownHostException e) { System.err.println("I can't find " + e ); } catch (SocketException e) { System.err.println("Could not connect to " +e ); } catch (IOException e) { System.err.println(e); } } }Result
The above code sample will produce the following result.
Connected to /74.125.67.100 on port 80 from port 2857 of /192.168.1.4
java_networking.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4