inetAdress类:
1.没有提供公共的构造器只提供了几个静态的方法
静态的实例:
getlocalhost() geybyname(string host)
inet提供了几个常用的方法:
gethostadress() gethostname() isreachable(int timeout)
public class test {
public static void main(String[] args)throws Exception {
InetAddress byName1 = InetAddress.getByName("www.atguigu.com");
System.out.println(byName1);//获得域名和ip地址
System.out.println(byName1.getHostName());//获得域名
System.out.println(byName1.getHostAddress());//获得ip地址
}
}
socket分类
1.流套接字 stream sockt
2.数据报套接字 datagram socket
socker常用的构造器
1.socket(inetadress adress,int port)
2.socket(string host ,int port)
socket类常用的方法
1.getinputstream()
2.getoutputstream()
3.getinetadress()查看套接字是否连接 未连接返回null
4.getlocaladress() 获取套接字绑定的本地地址
5.close()
6.shutdowinput()
7.shutdoutput()
基于socket的tcp连接
客户端socket分为4个步骤
1.创建socket
2..打开链接socket的输入输出流
3.按照一定的协议对socket进行读写操作
4.关闭socket
public void testclient()throws Exception{
Socket sk = new Socket("localhost",123456789);//创建socket
OutputStream os = sk.getOutputStream();//连接输入或者输出流
os.write("nihao".getBytes(StandardCharsets.UTF_8));//进行读写
sk.close();//关闭socket
}
服务端socket
调用servesocket(int port)
调用accept()
调用socket类对象的getinputstream()、getoutputstream()
关闭servesocket和socket对象
public void testserve()throws Exception{
ServerSocket ss=new ServerSocket(123456789);
Socket s=ss.accept();
InputStream is=s.getInputStream();
byte[] bytes=new byte[1024];
int num=is.read(bytes);
String str=new String(bytes,0,num);
System.out.println(str);
s.close();
ss.close();
}



