InetAddress的使用
作用
InetAddress类的一个实例就代表一个具体的ip地址
实例化方式
InetAddress getByName(String host):获取指定ip对应的InetAddress的实例
InetAddress getLocalHost():获取本地ip对应的InetAddress的实例
常用方法
getHostName():
getHostAddress()
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 
 | public class netTest {@Test
 public void test(){
 try{
 
 
 InetAddress inet1 = InetAddress.getByName("192.168.23.31");
 System.out.println(inet1);
 
 InetAddress inet2 = InetAddress.getByName("www.baidu.com");
 System.out.println(inet2);
 
 
 InetAddress inet3 = InetAddress.getLocalHost();
 System.out.println(inet3);
 
 
 System.out.println(inet1.getHostName());
 System.out.println(inet1.getHostAddress());
 
 }catch (UnknownHostException e){
 e.printStackTrace();
 }
 
 }
 }
 
 | 
Socket类
TCP
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 
 | public class TCPTest2 {@Test
 public void client(){
 Socket socket = null;
 FileInputStream fis = null;
 OutputStream os = null;
 try {
 
 InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
 int port = 9090;
 socket = new Socket(inetAddress, port);
 
 File file = new File("xx.jpg");
 fis = new FileInputStream(file);
 
 os = socket.getOutputStream();
 
 byte[] buffer = new byte[1024];
 int len;
 while ((len=fis.read(buffer))!=-1){
 os.write(buffer,0,len);
 }
 
 InputStream is = socket.getInputStream();
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 byte[] buffer1 = new byte[5];
 int len1;
 while((len1 = is.read(buffer))!=-1){
 baos.write(buffer1,0,len);
 }
 System.out.println(baos.toString());
 
 socket.shutdownOutput();
 
 } catch (IOException e) {
 throw new RuntimeException(e);
 } finally {
 
 try {
 os.close();
 } catch (IOException e) {
 throw new RuntimeException(e);
 }
 try {
 fis.close();
 } catch (IOException e) {
 throw new RuntimeException(e);
 }
 try {
 socket.close();
 } catch (IOException e) {
 throw new RuntimeException(e);
 }
 }
 
 }
 
 @Test
 public void server(){
 ServerSocket serverSocket = null;
 Socket socket = null;
 InputStream is = null;
 FileOutputStream fos = null;
 try {
 
 int port = 9090;
 serverSocket = new ServerSocket();
 
 socket = serverSocket.accept();
 
 is = socket.getInputStream();
 
 File file = new File("xxx_copy.jpg");
 fos = new FileOutputStream(file);
 
 byte[] buffer = new byte[1024];
 int len;
 while((len = is.read(buffer))!=-1){
 fos.write(buffer, 0, len);
 }
 
 OutputStream os = socket.getOutputStream();
 os.write("pic recevied".getBytes());
 
 } catch (IOException e) {
 throw new RuntimeException(e);
 } finally {
 
 try {
 fos.close();
 } catch (IOException e) {
 throw new RuntimeException(e);
 }
 try {
 is.close();
 } catch (IOException e) {
 throw new RuntimeException(e);
 }
 try {
 socket.close();
 } catch (IOException e) {
 throw new RuntimeException(e);
 }
 try {
 serverSocket.close();
 } catch (IOException e) {
 throw new RuntimeException(e);
 }
 }
 
 }
 }
 
 | 
URL
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | public class UrlTest {@Test
 public void test(){
 String str = "http://www.google.com";
 try {
 URL url = new URL(str);
 
 System.out.println(url.getProtocol());
 System.out.println(url.getHost());
 System.out.println(url.getPort());
 System.out.println(url.getFile());
 
 } catch (MalformedURLException e) {
 throw new RuntimeException(e);
 }
 }
 }
 
 |