1、什么是计算机网络
什么是计算及网络? 计算机网络系统就是利用通信设备和线路将地理位置不同、功能独立的多个计算机系统互联起来,以功能完善的网络软件实现网络中资源共享和信息传递的系统。 网络编程的目的 数据交换----通信
2、网络通信的两个要素
小结: 1.网络编程中有两个主要的问题
- 如何准确的定位到网络.上的一台或者多台主机
- 找到主机之后如何进行通信
2.网络编程中的要素
3.万物皆对象
3、IP地址
java.net.InetAddress
package com.yangbocsu.lesson1;
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:"+inetAddress1);
InetAddress inetAddress2 = InetAddress.getByName("localhost");
System.out.println("inetAddress2:"+inetAddress2);
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println("inetAddress3:"+inetAddress3);
InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
System.out.println("inetAddress1:"+inetAddress4);
System.out.println(inetAddress4.getAddress());
System.out.println(inetAddress4.getHostAddress());
System.out.println(inetAddress4.getCanonicalHostName());
System.out.println(inetAddress4.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
4、端口Port
package com.yangbocsu.lesson1;
import java.net.InetSocketAddress;
public class TestInetSocketAddress {
public static void main(String[] args) {
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",8080);
System.out.println(socketAddress);
InetSocketAddress socketAddress2 = new InetSocketAddress("localhost",8080);
System.out.println(socketAddress2);
System.out.println(socketAddress.getAddress());
System.out.println(socketAddress.getHostName());
System.out.println(socketAddress.getPort());
}
}
5、通信协议
协议:约定,就好比我们现在说的普通话。 网络通信协议: 速率、传输码率、代码结构、传输控制。。。。 **问题:**非常复杂? 大事化小:分层! TCP/IP协议簇 重要:
- TCP(Transmission Control Protocol,传输控制协议)
- UDP(User Data Protocol,用户数据报协议)
出名的协议:
6、TCP实现聊天
服务端 - 客户端
服务端
package com.yangbocsu.lesson2;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is =null;
ByteArrayOutputStream baos = null;
try {
serverSocket = new ServerSocket(9999);
socket = serverSocket.accept();
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while (( (len = is.read(buffer))!= -1))
{
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is!=null)
{
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null)
{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket!=null)
{
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
客户端
package com.yangbocsu.lesson2;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os =null;
try {
InetAddress ServerIP= InetAddress.getByName("127.0.0.1");
int port= 9999;
socket = new Socket(ServerIP, port);
os = socket.getOutputStream();
os.write("hello world!".getBytes());
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (os!=null)
{
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null)
{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务端一直监听:
package com.yangbocsu.lesson2;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is =null;
ByteArrayOutputStream baos = null;
try {
serverSocket = new ServerSocket(9999);
while (true)
{
socket = serverSocket.accept();
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while (( (len = is.read(buffer))!= -1))
{
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is!=null)
{
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null)
{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket!=null)
{
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
7、TCP文件上传实现
服务端
package com.yangbocsu.lesson2;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo02 {
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(9000);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("receive1.png"));
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer))!=-1)
{
fos.write(buffer,0,len);
}
OutputStream os = socket.getOutputStream();
os.write("Over! Over!".getBytes());
os.close();
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
客户端
package com.yangbocsu.lesson2;
import java.io.*;
import java.net.InetAddress;
import java.net.Proxy;
import java.net.Socket;
import static java.net.InetAddress.*;
public class TcpClientDemo02 {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("img.png"));
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) !=-1)
{
os.write(buffer,0,len);
}
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while((len2 = inputStream.read(buffer2))!=-1)
{
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
baos.close();
inputStream.close();
fis.close();
os.close();
socket.close();
}
}
8、初识Tomcat
9、UDP消息发送
UDP
package com.yangbocsu.lesson03;
import sun.security.krb5.internal.PAData;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpServerDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(), 0, packet.getLength()));
socket.close();
}
}
package com.yangbocsu.lesson03;
import com.yangbocsu.lesson1.TestInetAddress;
import java.net.*;
import java.nio.charset.StandardCharsets;
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String msg = "你好呀,服务器!";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
socket.send(packet);
socket.close();
}
}
10、UDP聊天实现
package com.yangbocsu.chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while (true) {
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(receivedata);
if (receivedata.equals("bye")) {
break;
}
}
socket.close();
}
}
package com.yangbocsu.chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class UdpSendDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
while (true) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
socket.send(packet);
if (data.equals("bye")) {
break;
}
}
socket.close();
}
}
11、UDP多线程在线咨询
12、URL下载网络资源
|