网络编程中两个主要的问题
- 如何准确地定位网络上一台或多态主机;
- 找到主机后如何可靠高效的进行数据传输
要素
IP端口号
网络通信协议
- TCP
- 使用TCP协议,先建立TCP连接,形成传输数据通道
- 传输前,采用"三次握手"方式,点对点通信,是可靠的
- TCP协议进行通信的两个应用进程: 客户端、服务端
- 在连接中可进行大数据量的传输
- 传输完毕,需要释放已建立的连接,效率低
public class TCPTest1 {
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
try {
socket = new Socket(InetAddress.getLocalHost(), 8888);
os = socket.getOutputStream();
os.write("您好,服务器".getBytes());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server() {
ServerSocket socket = null;
Socket accept = null;
InputStream iStream = null;
ByteArrayOutputStream baoStream = null;
try {
socket = new ServerSocket(8888);
accept = socket.accept();
iStream = accept.getInputStream();
baoStream = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int len;
while ((len = iStream.read(buff)) != -1) {
baoStream.write(buff, 0, len);
}
System.out.println(baoStream.toString());
System.out.println(accept.getInetAddress().getHostAddress());
System.out.println();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baoStream != null) {
baoStream.close();
}
if (iStream != null) {
iStream.close();
}
if (accept != null) {
accept.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class TCPTest2 {
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
BufferedInputStream bufferedInputStream = null;
try {
socket = new Socket(InetAddress.getLocalHost(), 8888);
os = socket.getOutputStream();
bufferedInputStream = new BufferedInputStream(new FileInputStream("1.jpg"));
byte[] buff = new byte[1024];
int len;
while ((len = bufferedInputStream.read(buff)) != -1) {
os.write(buff, 0, len);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server() {
ServerSocket socket = null;
Socket accept = null;
InputStream iStream = null;
BufferedOutputStream baoStream = null;
try {
socket = new ServerSocket(8888);
accept = socket.accept();
iStream = accept.getInputStream();
baoStream = new BufferedOutputStream(new FileOutputStream(new File("2.jpg")));
byte[] buff = new byte[1024];
int len;
while ((len = iStream.read(buff)) != -1) {
baoStream.write(buff, 0, len);
}
System.out.println(accept.getInetAddress().getHostAddress());
System.out.println();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baoStream != null) {
baoStream.close();
}
if (iStream != null) {
iStream.close();
}
if (accept != null) {
accept.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class TCPTest2 {
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
BufferedInputStream bufferedInputStream = null;
ByteArrayOutputStream baoStream = null;
try {
socket = new Socket(InetAddress.getLocalHost(), 8888);
os = socket.getOutputStream();
bufferedInputStream = new BufferedInputStream(new FileInputStream("1.jpg"));
byte[] buff = new byte[1024];
int len;
while ((len = bufferedInputStream.read(buff)) != -1) {
os.write(buff, 0, len);
}
socket.shutdownOutput();
InputStream is = socket.getInputStream();
baoStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int lens;
while ((lens = is.read(buffer)) != -1) {
baoStream.write(buffer, 0, lens);
}
System.out.println(baoStream.toString());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
if (socket != null) {
socket.close();
}
if (baoStream != null) {
baoStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server() {
ServerSocket socket = null;
Socket accept = null;
InputStream iStream = null;
BufferedOutputStream baoStream = null;
try {
socket = new ServerSocket(8888);
accept = socket.accept();
iStream = accept.getInputStream();
baoStream = new BufferedOutputStream(new FileOutputStream(new File("2.jpg")));
byte[] buff = new byte[1024];
int len;
while ((len = iStream.read(buff)) != -1) {
baoStream.write(buff, 0, len);
}
OutputStream oStream = accept.getOutputStream();
oStream.write("发送成功".getBytes());
System.out.println(accept.getInetAddress().getHostAddress());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baoStream != null) {
baoStream.close();
}
if (iStream != null) {
iStream.close();
}
if (accept != null) {
accept.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- UDP
- 将数据、源、目的封装成数据包,不需要建立连接
- 每个数据包的大小限制在64K内
- 发送不管对方是否准备好,接收方收到也不确认
- 可以广播发送
- 发送数据结束时无需释放资源,开销小,速度快
public class UDPTest {
@Test
public void sender() {
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
String str = "UDP";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data, 0, data.length, inet, 8888);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
@Test
public void receiver() {
DatagramSocket socket = null;
try {
socket = new DatagramSocket(8888);
byte[] buff = new byte[1024];
DatagramPacket packet = new DatagramPacket(buff, 0, buff.length);
socket.receive(packet);
System.out.println(new String(packet.getData(), 0, packet.getLength()));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
}
URL
- 简介: 统一资源定位符,表示Internet上某一资源的地址
- 使用实例:
@Test
public void test() {
try {
URL url = new URL("https://www.baidu.com/img/bd_logo1.png?where=super");
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());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Test
public void test() {
BufferedOutputStream bufferedOutputStream = null;
InputStream stream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2157737149,3401931137&fm=26&gp=0.jpg");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
stream = urlConnection.getInputStream();
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File("3.jpg")));
byte[] buffer = new byte[1024];
int len;
while ((len = stream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (stream != null) {
stream.close();
}
if (bufferedOutputStream != null) {
bufferedOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}