Java网络编程实战
1、网络编程基本概念
- 计算机网络:计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统、网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递**的计算机系统。
- 网络编程的目的:直接或间接地通过网络协议与其他计算机进行·通讯。
- 网络编程达到的效果:
- 如何准确的定位网络上的一台主机 IP地址 : 端口,定位到这个计算机的某个资源
- 找到主机以后如何传输数据?
- 网络编程与网页编程区别
- 网页编程:javaweb (B/S架构)
- 网络编程:TCP/IP (C/S架构)
2、网络通信的要素
如何实现网络通信?
3、IP
4、端口
表示计算机上的一个程序的进程;
5、通信协议
1、网络通信协议
? 网络通信协议是一种网络通用语言,为连接不同操作系统和不同硬件体系结构和互联网络提供通信支持,是一种网络通用语言。
- 三个要素
- 语义:接式控制信息每个部分的意义。它规定了需要发出何种控制信息,以及完成的动作及作出什么样的反应;
- 语法:用户数据与控制信息的结构与格式,以及数据出现的顺序;
- 时序:对事件发生顺序的详细说明
- 重要协议:TCP/IP协议
- TCP:用户传输协议
- 连接,稳定
- 三次握手,四次挥手
- 客户端、服务端
- 传输完成,释放连接,效率低
- UDP:用户数据报协议
- 不连接,不稳定
- 客户端、服务端:没有明确的界限
- 不管有没有准备好都发给你:导弹、DDOS:洪水攻击(饱和攻击)
6、TCP
-
客户端
public class TcpClientDemo1 {
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,netprogram!!".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();
}}
}
}
}
-
服务器
- 建立服务的端口 ServerSocket
- 等待用户的连接 accept
- 接收消息
/服务端
public class TcpServerDemo1 {
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();
}}
}
}
}
7、文件上传
public class TcpServerDemo2 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8900);
Socket sc = serverSocket.accept();
InputStream is = sc.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));
byte[] bytes = new byte[1024];
int len = -1 ;
while ((len=is.read(bytes))!=-1){
fos.write(bytes,0,len);
}
OutputStream os = sc.getOutputStream();
os.write("我已经接收完比,请求断开连接".getBytes());
fos.close();
is.close();
sc.close();
serverSocket.close();
}
}
public class TCPClientDemo2 {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 8900);
OutputStream out = socket.getOutputStream();
FileInputStream fis = new FileInputStream("E:\\美食网\\美食图\\法国.jpg");
byte[] bytes = new byte[1024];
int len = -1;
while ((len=fis.read(bytes))!=-1){
out.write(bytes,0,len);
}
socket.shutdownOutput();
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes1 = new byte[2048];
int len1 = 1 ;
while ((len=is.read(bytes1))!=-1){
baos.write(bytes1,0,len1);
}
System.out.println(baos.toString());
baos.close();
fis.close();
out.close();
socket.close();
}
}
8、初识Tomcat
服务端
客户端
9、UDP消息发送
发送消息不用建立连接,需要知道对方的地址;
public class UDPServerDemo1 {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9090);
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes, 0, bytes.length);
socket.receive(dp);
System.out.println(dp.getAddress().getHostAddress());
System.out.println(new String(dp.getData()));
socket.close();
}
}
public class UDPClientDemo1 {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
String info = "Hello!!!";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(info.getBytes(),0,
info.getBytes().length,localhost,port);
ds.send(packet);
ds.close();
}
}
public class UdpSender {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8001);
BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
while (true) {
String data = reader.readLine();
byte[] bytes = data.getBytes();
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length,
new InetSocketAddress("localhost", 8000));
socket.send(packet);
if(data=="bye")
break;
}
socket.close();
}
}
public class UdpReceiver {
public static void main(String[] args) throws Exception {
DatagramSocket socket =new DatagramSocket(8000);
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=="bye")
break;
}
socket.close();
}
}
10、UDP多线程实现在线咨询
public class Takler2Demo implements Runnable{
DatagramSocket socket=null;
private int myport;
public Takler2Demo(int myport){
this.myport = myport;
try {
socket = new DatagramSocket(myport);
} 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);
System.out.println(myport+":"+receiveData);
if(receiveData.equals("bye"))
break;
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
public class Takler2Demo implements Runnable{
DatagramSocket socket=null;
private int myport;
public Takler2Demo(int myport){
this.myport = myport;
try {
socket = new DatagramSocket(myport);
} 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);
System.out.println(myport+":"+receiveData);
if(receiveData.equals("bye"))
break;
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
public class Talker1 {
public static void main(String[] args) {
Talker1Demo t1 =new Talker1Demo(8000,"localhost",6666);
Thread thread = new Thread(t1);
thread.start();
Takler2Demo t2 = null;
t2 = new Takler2Demo(9999);
Thread thread1 =new Thread(t2);
thread1.start();
}
}
public class Talker2 {
public static void main(String[] args) {
Talker1Demo t1 =new Talker1Demo(8888,"localhost",9999);
Thread thread = new Thread(t1);
thread.start();
Takler2Demo t2 = null;
t2 = new Takler2Demo(6666);
Thread thread1 =new Thread(t2);
thread1.start();
}
}
11、URL
统一资源定位符:定位互联网上的某一个资源;
格式:
协议:
public class URLDemo1 {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/" +
"hellworld/index.jsp?username=lengzher&password=147");
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 URLDemo2 {
public static void main(String[] args) throws Exception {
//下载地址
URL url = new URL("http://localhost:8080/Lengzher/confidenceFIle.txt");
//连接到这个资源 HTTP
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
InputStream is =uc.getInputStream();
FileOutputStream fos = new FileOutputStream("confidenceFIle.txt");
byte[] bytes =new byte[1024];
int len =-1;
while ((len=is.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fos.close();
is.close();
uc.disconnect();
}
}
|