两种传输协议:TCP,UDP。
JDK中的网络类:通过java.net包中的类,可以使用TCP,UDP协议来在互联网上通信。
URL包括两个主要部分:协议标识符:—HTTP,FTP,File等。—资源名字:主机名,文件名,端口号,引用。
public class UrlTest {
public static void main(String[] args) throws Exception{
URL url=new URL("https://www.baidu.com");
URLConnection connection=url.openConnection();
InputStream is=connection.getInputStream();
OutputStream os=new FileOutputStream("C:\\infoq.txt");
byte[] buffer=new byte[2048];
int length=0;
while ((length=is.read(buffer,0,buffer.length))!=-1){
os.write(buffer,0,length);
}
is.close();
os.close();
}
}
通过这段代码我们可以获取到百度首页的源代码。
InetAddress类:
public class InetAddressTest {
public static void main(String[] args)throws Exception {
InetAddress address=InetAddress.getLocalHost();
System.out.println(address);
address=InetAddress.getByName("www.sohu.com");
System.out.println(address);
}
}
得到搜狐的地址和本机地址。
利用套接字(socket)进行通信
使用ServerSocket和Socket实现服务器端和客户端的Socket通信。
(1)建立Socket连接
(2)获取输入/输出流
(3)读/写数据
(4)关闭输入/输出流
(5)关闭Socket
接下来我们展示一个可以互相通信的一段代码:
首先是服务器端:
import java.net.ServerSocket;
import java.net.Socket;
public class MainServer {
public static void main(String[] args) throws Exception{
ServerSocket serverSocket=new ServerSocket(4000);
//启动读写线程
while (true){
Socket socket=serverSocket.accept();
new ServerInputThread(socket).start();
new ServerOutputThread(socket).start();
}
}
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class ServerInputThread extends Thread {
private Socket socket;
public ServerInputThread(Socket socket) {
this.socket=socket;
}
@Override
public void run() {
try {
InputStream is = socket.getInputStream();
while (true) {
byte[] buffuer = new byte[1024];
int length = is.read(buffuer);
String str = new String(buffuer, 0, length);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class ServerOutputThread extends Thread {
private Socket socket;
public ServerOutputThread(Socket socket){
this.socket=socket;
}
@Override
public void run() {
try {
OutputStream os=socket.getOutputStream();
while (true) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();
os.write(line.getBytes());
}
}catch (IOException e){
e.printStackTrace();
}
}
}
接下来是用户端:
import java.net.Socket;
public class MainClient {
public static void main(String[] args) throws Exception{
Socket socket=new Socket("localhost",4000);
new ClientInputThread(socket).start();
new ClientOutputThread(socket).start();
}
}
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class ClientInputThread extends Thread{
private Socket socket;
public ClientInputThread(Socket socket) {
this.socket=socket;
}
@Override
public void run() {
try {
InputStream is = socket.getInputStream();
while (true) {
byte[] buffuer = new byte[1024];
int length = is.read(buffuer);
String str = new String(buffuer, 0, length);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ClientOutputThread extends Thread{
private Socket socket;
public ClientOutputThread(Socket socket){
this.socket=socket;
}
@Override
public void run() {
try {
OutputStream os=socket.getOutputStream();
while (true) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();
os.write(line.getBytes());
}
}catch (IOException e){
e.printStackTrace();
}
}
}
使用 无连接的数据报(UDP)进行通信
Datagram-----数据报是网上传输的独立数据包,数据报是否能正确地达到目的地,达到的时间,顺序,内容的正确性均没有保障。
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpServer {
public static void main(String[] args) throws Exception {
DatagramSocket socket=new DatagramSocket();
String string="hello world";
DatagramPacket packet=new DatagramPacket(string.getBytes(),string.length(),
InetAddress.getByName("localhost"),7000);
socket.send(packet);
byte[]buffur=new byte[1024];
DatagramPacket packet1=new DatagramPacket(buffur,100);
socket.receive(packet1);
System.out.println(new String (buffur,0,packet1.getLength()));
socket.close();
}
}
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpTest {
public static void main(String[] args) throws Exception {
DatagramSocket socket=new DatagramSocket(7000);
byte [] buffur=new byte[1024];
DatagramPacket packet=new DatagramPacket(buffur,1024);
socket.receive(packet);
System.out.println(new String(buffur,0,packet.getLength()));
String str="welcome";
DatagramPacket packet1=new DatagramPacket(str.getBytes(),str.length(),packet.getAddress(),packet.getPort());
socket.send(packet1);
socket.close();
}
}