java基础:网络编程TCP,URL

获取域名的两种方法:

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");//通过域名获取

System.out.println(inet);//获取域名和ip
System.out.println(inet.getHostName());//获取ip地址对应的域名
System.out.println(inet.getHostAddress());//获取ip地址
System.out.println();

InetAddress inet1=InetAddress.getLocalHost();//获取本机的ip,域名
System.out.println(inet1);
System.out.println(inet1.getHostName());//获取本机域名
System.out.println(inet1.getHostAddress());//获取本机的ip地址
}

}

-------------------------------------------------------------------------------------------

TCP编程实现客户端和服务器端的互动

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;

/*
* 实现一个基本的客户端/服务器端程序。主要实现服务器端一直监听某个端口,等待客户端连接请求。
* 客户端根据IP地址和端口号连接服务器端,从键盘上输入一行信息,发送到服务器端,
* 然后接收服务器端返回的信息,最后结束会话。这个程序一次只能接受一个客户连接。
* 本机域名和ip地址:DADI/10.5.78.19
* 客户端会向服务器端发送
* */
public class TestTCP {
@Test
//客户端:写入数据
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
//服务器端,读取数据
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:统一资源定位符,一个url的对象,对应着互联网上一个资源,

可以通过url的对象调用其相对应的方法,将此资源读取;

执行时必须在联网的情况下练习:

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");//获取url链接
is=url.openStream();
//一字节的方法读取
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();
}
}
}
}

原文地址:https://www.cnblogs.com/lxnlxn/p/5763821.html