网络编程
- 计算机网络:将地理位置不同的具有独立功能的多台计算计算及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
- 网络编程的目的:数据交换,通信
- 需要解决的问题:
- 如何准确定位网络上的一台主机和这台主机的某个资源(ip + 端口号)
- 找到主机后,如何传输数据?(网络通信协议(主要TCP,UDP))
1.1网络通信要素
-
IP类:InetAddress
- 本机ip(localhost):127.0.0.1
- ip地址分类:IPV4,4个字节组成。IPV6:128位。8个无符号整数;
-
域命:记忆IP问题
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestInetAddress {
public static void main(String[] args) {
try {
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress2 = InetAddress.getByName("localhost");
System.out.println(inetAddress2);
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println(inetAddress3);
System.out.println(inetAddress1.getAddress());
System.out.println(inetAddress1.getCanonicalHostName());
System.out.println(inetAddress1.getHostAddress());
System.out.println(inetAddress1.getHostName());
InetAddress inetAddress11 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress11);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
import java.net.InetSocketAddress;
public class TestInetSocketAddress {
public static void main(String[] args) {
InetSocketAddress inetSocketAddress1 = new InetSocketAddress("127.0.0.1",8080);
System.out.println(inetSocketAddress1);
InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost",8080);
System.out.println(inetSocketAddress2);
System.out.println(inetSocketAddress1.getAddress());
System.out.println(inetSocketAddress1.getHostName());
System.out.println(inetSocketAddress1.getHostString());
System.out.println(inetSocketAddress1.getPort());
}
}
public class TalkSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private String toIP;
private int toPort;
private int fromPort;
public TalkSend(String toIP, int toPort, int fromPort) {
this.toIP = toIP;
this.toPort = toPort;
this.fromPort = fromPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
public TalkSend() {
}
@Override
public void run() {
while (true) {
String data = null;
try {
data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet);
if(data.equals("bye")) {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
socket.close();
}
}
public class TalkReceive implements Runnable{
private int port;
DatagramSocket socket = null;
private String msgFrom;
public TalkReceive(int port, String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);
byte[] data = packet.getData();
String receiveData = new String(data,0,data.length);
System.out.println(msgFrom +" :"+receiveData);
if(receiveData.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend("localhost",9999,7777)).start();
new Thread(new TalkReceive(8888,"teacher")).start();
}
}
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend("localhost",8888,5555)).start();
new Thread(new TalkReceive(9999,"student")).start();
}
}
URL
URL:统一资源定位符,定位互联网上的某一个资源
1 协议: //ip地址:端口/项目名/资源
URL下载网络资源示例:下载网易云上的一副图片
import java.net.MalformedURLException;
import java.net.URL;
public class URLDemo {
public static void main(String[] args) {
URL url = null;
try {
url = new URL("http://localhost:8080/helloworld/index.jsp?username=jay&password=123");
} catch (MalformedURLException e) {
e.printStackTrace();
}
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println(url.getQuery());
}
}
public class UrlDown {
public static void main(String[] args) throws IOException {
URL url = new URL("https://p4.music.126.net/Nl4mFBPeN4Lqtqn3KOAvXQ==/109951166171044898.jpg?param=200y200");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("test.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len=inputStream.read(buffer)) != -1) {
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}
|