Java TCP/IP socket programming (2)

Source: Internet
Author: User
2.1 socket address

1. in Java, the address can be an IP address or a host name.

The following is an example of a Java address.

Package COM. suifeng. tcpip. chapter2; import java.net. inet4address; import java.net. inet6address; import java.net. inetaddress; import java.net. networkinterface; import java.net. socketexception; import java.net. unknownhostexception; import Java. util. enumeration;/*** inetaddress test * @ author suifeng **/public class inetaddressexample {public static void main (string [] ARGs) {// obtain the address associated with the network interface and the local machine. Try {Enumeration <Networkinterface> interfacelist = networkinterface. getnetworkinterfaces (); If (interfacelist = NULL) {system. out. println ("----------- no networkinterface founded -----------");} else {system. out. println ("check local Nic settings:"); While (interfacelist. hasmoreelements () {// obtain a network adapter networkinterface = interfacelist. nextelement (); // obtain the network adapter address enumeration <inetaddress> Addresslist = networkinterface. getinetad Dresses (); // empty list Detection If (! Addresslist. hasmoreelements () {continue; // system. out. println ("\ t no address for this interface");} // The name, IP type, and address of each network card. out. println (networkinterface. getname (); While (Addresslist. hasmoreelements () {inetaddress address = Addresslist. nextelement (); system. out. println ("\ taddress" + (address instanceof inet4address )? "(IPv4)": (address instanceof inet6address )? "(IPv6 )":"(?) ") +" \ T "+ address. gethostaddress () ;}}} catch (socketexception e) {system. out. println ("error get Network Interface:" + E. getmessage (); E. printstacktrace ();} // print the Host Name and address system for input data from the command line. out. println (""); For (string HOST: ARGs) {system. out. println ("Host:" + host); try {inetaddress [] Addresslist = inetaddress. getallbyname (host); For (inetaddress Address: Addresslist) {system. out. println ("\ t" + address. gethostname () + "/" + address. gethostaddress () ;}} catch (unknownhostexception e) {system. out. println ("unable to find address for:" + host );}}}}

Execution result

There are several important methods in inetaddress. Let's take a look at them using the following program.

Package COM. suifeng. tcpip. chapter2; import Java. io. ioexception; import Java. io. interruptedioexception; import java.net. datagrampacket; import java.net. datagramsocket; import java.net. inetaddress; import java.net. socketexception; import java.net. unknownhostexception;/*** UDP client ** @ author suifeng **/public class udpechoclienttimeout {// timeout Private Static final int timeout = 3000; // maximum number of connections Private Static Final int maxtries = 5; public static void main (string [] ARGs) throws ioexception {If (ARGs. length <2 | args. length> 3) {Throw new illegalargumentexception ("paramters: <Server> <word> [<port>]") ;}// server address inetaddress serveraddress = inetaddress. getbyname (ARGs [0]); // The Data byte [] bytetosend = ARGs [1]. getbytes (); // server port int SERVERPORT = (ARGs. length = 3 )? Integer. parseint (ARGs [2]): 7; // UDP client datagramsocket socket = new datagramsocket (); // receiving data blocking time socket. setsotimeout (timeout); system. out. println ("the UDP client has been created .... "); // send the datagram package mongorampacket sendpacket = new mongorampacket (bytetosend, bytetosend. length, serveraddress, SERVERPORT); // The received datagram package datagrampacket receiverpacket = new datagrampacket (New byte [bytetosend. length], bytetosend. length); // number of attempts to receive int Trie S = 0; Boolean receivedresponse = false; do {system. out. println ("send data to the service end .... "); // send data socket to the server. send (sendpacket); try {system. out. println ("receiving data returned from the server... "); // receives data from the server socket. receive (receiverpacket); If (! Receiverpacket. getaddress (). equals (serveraddress) {Throw new ioexception ("specified ed packet from an unknown source");} caught edresponse = true;} catch (interruptedioexception e) {tries ++; system. out. println ("timed out," + (maxtries-tries) + "more ties") ;}} while ((! Receivedresponse) & (tries <maxtries); If (receivedresponse) {system. out. println ("Received:" + new string (receiverpacket. getdata ();} else {system. out. println ("no response -- giving up! ");}}}

Execution result

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.