![]() |
Java Networking - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE) +--- Thread: Java Networking (/Thread-Java-Networking--3931) |
Java Networking - ICE_ - 02-01-2013 [shadow=black]JAVA NETWORKING TUTORIAL[/shadow]
[shadow=green]Imports Needed:[/shadow]
[shadow=red]Objects Used:[/shadow]
[shadow=blue]Examples/explenations of all:[/shadow] InetAddress: Code: public static void inetmethod() throws UnknownHostException{ The above doe will print out the IP address of anything given and it's host name. For example, if ADDRESS_AS_STRING_GOES_HERE was substituted with "www.google.com" then "173.194.46.20 || www.google.com" would print out. Inet4Address: Code: import java.net.*; The above code will print out the IPv4 address of any given host. For example, the above code, will print out "IP address is: 127.0.0.1". However, if we substitute "localhost" with "www.google.com" the output will end up as "IP address is: 173.194.46.20" Inet6Address: Code: public static void Inetsix throws UnknownHostException { The above code would print out the IPv6 address of any given host. For example, if you replace ADDRESS_AS_STRING_GOES_HERE with a host that has an IPv6 address, it would print out said IPv6 address. However, due to the fact that I don't know of any IPv6 address using sites/hosts, I can not show you example output. SocketAddress: Code: public static void socketaddressmethod(){ The above code will set up a new connection to a given host on a given port. For example, if HOST_AS_STRING was replaced with "www.google.com" and PORT_AS_INT was replaced with 80, a new connection would be set up(accessible)(connections are made with a socket) with google on port 80 (www.google.com:80). Socket: Code: public static void SocketExample(){ The above code will make a connection to any given host/port. For example, if HOST_AS_STRING was replaced with "www.google.com" and PORT_AS_INT was replaced with 80 a new connection would be made with google on port 80 (www.google.com:80) and "Connection made" would be printed to the screen. ServerSocket: Code: public static void serversocketexample(){ The above code will accept all incoming connections from clients to that ip address. Thus, if we use a Socket and change the IP to the IP of the server, the server will willingly accept the connection. DatagramPacket: Code: byte[] buffer = new byte[65508]; The above code will make a UDP packet with the size of 65508 and set it up so that when it is sent it will go straight to the given host on the given port. For example, If we changed ADDRESS_AS_STRING to "www.google.com" and PORT_AS_INT to 80, we would have made a large packet set up to send to google on port 80 (not sent until we send it with DatagramSocket). DatagramSocket: Code: DatagramSocket datagramSocket = new DatagramSocket(); The above code will send a udp packet with the given message on it, to the given host on the given port. For example, if we changes MESSAGE_AS_STRING to "Hello Java Networking!" and ADDRESS_AS_STRING to "www.google.com" and PORT_AS_INT to 80, we would have sent a UDP packet with the message "Hello Java Networking!" to google on port 80 (www.google.com:80) [shadow=black]I HOPE THIS TUTORIAL HELPED YOU! KEEP AN EYE OUT FOR MY OTHER NETWORKING TUTORIALS FOR DIFFERENT LANGUAGES![/shadow] RE: Java Networking - Zealotry - 02-01-2013 Damn. HQ. Good job. ![]() RE: Java Networking - ICE_ - 02-01-2013 (02-01-2013, 05:21 AM)Zealotry Wrote: Damn. HQ. Good job. thanks, I'll be making another one for python soon. |