There are two ways to get a domain name:
Package com.lanqiao.java.test;
Import java.net.InetAddress;
Import java.net.UnknownHostException;
public class Testinetaddress {
public static void Main (string[] args) throws Exception {
InetAddress inet=inetaddress.getbyname ("www.atguigu.com");//access by domain name
System.out.println (inet);//Get Domain and IP
System.out.println (Inet.gethostname ());//Get the domain name corresponding to the IP address
System.out.println (Inet.gethostaddress ());//Get IP Address
System.out.println ();
InetAddress inet1=inetaddress.getlocalhost ();//Get the IP of this machine, domain name
System.out.println (INET1);
System.out.println (Inet1.gethostname ());//Get the native domain name
System.out.println (Inet1.gethostaddress ());//Get the IP address of this machine
}
}
-------------------------------------------------------------------------------------------
TCP Programming for client and server-side interaction
Package com.lanqiao.java.test;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import java.net.UnknownHostException;
Import Org.junit.Test;
/*
* Implement a basic client/server-side program. The main implementation of the server side has been listening to a port, waiting for client connection request.
* The client connects to the server based on the IP address and port number, enters a line of information from the keyboard, sends to the server side,
* Then receives the information returned by the server side and ends the session. This program can only accept one client connection at a time.
* Native domain name and IP address: dadi/10.5.78.19
* Client will send to server side
* */
public class Testtcp {
@Test
Client: Write Data
public void Writertcp () throws ioexception{
Socket S=null;
OutputStream Os=null;
try {
S=new Socket ("10.5.78.19", 8080);
Os=s.getoutputstream ();
Os.write ("HELLO.AFASDFASD". GetBytes ());
} catch (Unknownhostexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
if (os!=null) {
Os.close ();
}
if (s!=null) {
S.close ();
}
}
}
@Test
Server-side, read data
public void Readtcp () throws exception{
ServerSocket Ss=null;
InputStream Is=null;
try {
Ss=new ServerSocket (8080);
Socket s1=ss.accept ();
Is=s1.getinputstream ();
byte [] b=new byte[10];
int Len;
while ((Len=is.read (b))!=-1) {
String Str=new string (b, 0, Len);
System.out.println (str);
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
if (is!=null) {
Is.close ();
}
if (ss!=null) {
Ss.close ();
}
}
}
}
-----------------------------------------------------------------------------------------
URL: Uniform Resource Locator, a URL object that corresponds to a resource on the Internet,
This resource can be read by invoking its corresponding method through the object of the URL;
The execution must be practiced in a networked environment:
Package com.lanqiao.java.test;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.net.MalformedURLException;
Import Java.net.URL;
public class Test {
public static void Main (string[] args) throws IOException {
URL Url=null;
InputStream Is=null;
try {
Url=new URL ("Http://127.0.0.1:8080/examples/HelloWorld.txt");//Get URL link
Is=url.openstream ();
One-byte method reads
Byte[] B=new byte[20];
int Len;
while ((Len=is.read (b))!=-1) {
String Str=new string (B,0,len);
System.out.println (str);
}
} catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
if (is!=null) {
Is.close ();
}
}
}
}
Java Fundamentals: Network Programming Tcp,url