Java基礎<十四>_網絡編程

網絡編程

一、網絡編程概述

    網絡編程:可以在不同網絡終端上通信的編程。

    網絡通訊三要素:IP地址、端口號、傳輸協議

    IP地址:標識通信雙方計算機,簡單說:標識對方,找到對方;在java中,IP封裝成爲InetAddress類;本地迴環地址:127.0.0.1,主機名爲localhost。

    端口號:用於標識進程的邏輯地址,即不同進程的標識;有效端口號:0—65535,其中0—1024系統使用或者爲保留端口,Tomcat服務器默認端口爲8080,Web服務爲80。

    傳輸協議:通信雙方的數據遵循規則必須一致,否則無法通信;網絡上用TCP/IP協議。

    網絡之間的通信是一個很複雜的過程,爲了把複雜事物簡單化,網絡可以理論分爲七個參考模型(OSI):物理層、數據鏈路層、網絡層、傳輸層、會話層、表示層、應用層;也可以按TCP/IP(實際應用的模型)分爲四層:網絡接口層、網絡層、傳輸層、應用層。

    常見的協議:網絡層:IP;傳輸層:TCP、UDP;應用層:HTTP、FTP等。

    總之:發送方:是一個數據封包的過程;接收方:是一個數據解包的過程。

    UDP和TCP是運輸層的兩個不同協議,UDP面向無連接,TCP面向連接;區別如下:

    UDP:1、將數據及源和目的封裝成數據包,不需要建立連接;2、每個數據包的大小限制在64K內;3、因爲無連接,數據通信不可靠;4.因爲不需要建立連接,所以速度快。

    TCP:1、需要建立連接,形成傳輸數據的通道;2、在通道中進行大數據的傳輸;3、通過三次握手完成連接,數據通信可靠;4、因爲需要建立連接,所以效率稍低。

    Socket:socket是爲網絡服務提供的一種機制。好比水運的碼頭,想要運輸貨物,必須先建立好碼頭,同理,想要進行網絡傳輸,必須先建立Socket接口;碼頭有專門爲客船服務的,也有專門爲軍事服用的,同理,Socket有專門爲UDP類型傳輸的,也有專門爲TCP類型傳輸的;所以,根據不同的需要建立不同的Socket接口。

二、UDP傳輸

    UDP傳輸:面向無連接的傳輸,傳輸數據的兩端分爲發送端和接收端,“碼頭”爲DatagramSocket,在“碼頭”上運送的數據封裝對象Datagrampacket,想獲取具體的數據,可以通過DatagramPacket類中的相應方法獲取。

    注意:要發送的數據包必須帶上地址,即:明確目的地IP、端口;接收端必須指明端口,發送端可以不指定端口,系統會分配默認的端口。

    思路:網路編程重要的就是思路,具體對象可以查閱API

    UDP發送端:

    1、建立udpsocket服務;

    2、提供數據,並將數據封裝到數據包中;

    3、通過socket服務的發送功能,將數據包發出去;

    4、關閉資源。

    UDP接收端:

    1、定義udpsocket服務;通常會監聽一個端口,其實就是給這個接收網絡應用程序定義數字標識,方便於明確哪些數據過來可以處理;

    2、定義一個數據包,存儲接收到的字節數據;

    3、通過socket服務的receive方法接收到的數據存入指定的數據包中;

    4、通過數據包對象的特有功能,將這些不同的數據取出,打印到控制檯上;

    5、關閉資源。

下面代碼演示:

1、發送端發送數據到接收端

發送端:

package itheima.day23;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class udpSend2 {

	public static void main(String[] args) throws IOException {
		
		DatagramSocket ds = new DatagramSocket();
		BufferedReader bufr =
				new BufferedReader(new InputStreamReader(System.in));
		
		String line = null;
		while((line = bufr.readLine())!=null){
			if("886".equals(line))
				break;
			byte[] buf = line.getBytes();
			
			DatagramPacket dp = 
					new DatagramPacket(buf,buf.length,InetAddress.getByName("100.64.37.118"),10017);
			ds.send(dp);
		}
		ds.close();
	}
}

接收端:
package itheima.day23;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpRece2 {

	public static void main(String[] args) throws IOException {
		
		DatagramSocket ds = new DatagramSocket(10017);
		
		while(true){
			byte[] buf = new byte[1024];
			DatagramPacket dp = new DatagramPacket(buf,buf.length);
			ds.receive(dp);//阻塞式方法
			String ip = dp.getAddress().getHostAddress();
			String data = new String(dp.getData(),0,dp.getLength());
			int port = dp.getPort();
			System.out.println(ip+":::"+data+":::"+port);
		}
	}
}

2、簡單聊天程序:

package itheima.day23;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

//需求:編寫一個聊天程序。
public class ChatDemo {

	public static void main(String[] args) throws SocketException {
		
		DatagramSocket sendSocket = new DatagramSocket();
		DatagramSocket receSocket = new DatagramSocket(10005);
		
		new Thread(new Send(sendSocket)).start();
		new Thread(new Recv(receSocket)).start();
	}
}

class Send implements Runnable{
	
	private DatagramSocket ds;
	Send(DatagramSocket ds){
		this.ds = ds;
	}
	
	@Override
	public void run() {
		try {	
			BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
			String line = null;
			while((line = bufr.readLine())!=null){
				if("886".equals(line))
					break;
				byte[] buf = line.getBytes();
				DatagramPacket dp = 
						new DatagramPacket(buf,buf.length,InetAddress.getByName("100.64.37.118"),10005);
				ds.send(dp);
			}
		} catch (IOException e) {
			throw new RuntimeException("發送端失敗");
		}
	}
}

//接收端
class Recv implements Runnable{

	private DatagramSocket ds;
	Recv(DatagramSocket ds){
		this.ds = ds;
	}
	
	@Override
	public void run() {
		while(true){
			byte[] buf = new byte[1024];
			DatagramPacket dp = new DatagramPacket(buf,buf.length);//接收數據的包
			try {
				ds.receive(dp);
				String ip = dp.getAddress().getHostAddress();
				String data = new String(dp.getData(),0,dp.getLength());
				
				System.out.println(ip+"::"+data);
				
			} catch (IOException e) {
				throw new RuntimeException("接收端失敗");
			}
		}
	}	
}

三、TCP傳輸

    TCP傳輸:TCP是面向連接的傳輸,分爲客戶端和服務端,客戶端對應的對象是Socket,服務端對應的對象是ServerSocket

    客戶端:socket對象在建立時,就可以去連接指定的主機,因爲tcp是面向連接的,所以建立socket服務時,就要有服務端存在,並連接成功,形成通路後,在通道上進行數據的傳輸。

    服務端:專門給客戶端提供服務,採用多線程技術同時給多個客戶請求提供服務,大型的服務器一般不關閉,例如:新浪。

    思路:

    TCP客戶端:

    1、創建socket服務,並指定要鏈接的主機和端口;

    2、獲取socket流中的輸出流,將數據寫到該流中,通過網絡發送給服務端;

    3、獲取socket流中的輸入流,將服務端返回的數據獲取到,並打印;

    4、關閉客戶端。

    TCP服務端:

    1、建立服務端的socket服務,ServerSocket(),並監聽一個端口;

    2、獲取鏈接過來的客戶端的對象,通過ServerSocket的accept方法,阻塞式方法 ;

    3、客戶端如果發過來數據,那麼服務端要使用對應的客戶端對象,獲取數據;

    4、關閉服務端(可選)。

    注意:客戶端和服務端都莫名的等待,那是因爲客戶端和服務端都有阻塞式方法,這些方法沒有讀到結束標記,一直等待的緣故。

    域名解析原理:當在瀏覽器中輸入一個域名時,瀏覽器會先去C:\Windows\System32\drivers\etc hosts 文件查找是否有對應的ip地址;若沒有,再去公網的DNS服務器去查詢,當獲得了域名對應的ip地址後,再根據ip地址去訪問具體的網頁,最終還是用ip是訪問網頁。其中DNS服務器我們可以自行設置,默認時,走的是最近的DNS服務器。

下面代碼體現:

3、演示客戶端與服務端

客戶端:

package itheima.day23;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

//演示tcp傳輸的客戶端和服務端的互訪
// 需求:客戶端給服務端發送數據,服務端收到後,給客戶端反饋信息。
public class TCPClient2 {

	public static void main(String[] args) throws UnknownHostException, IOException {
		
		Socket s = new Socket("100.64.86.48",10012);
		OutputStream out = s.getOutputStream();
		out.write("服務端,服務端,我是客戶端".getBytes());
		InputStream in = s.getInputStream();
		
		byte[] buf = new byte[1024];
		int len = in.read(buf);//阻塞式方法
		System.out.println(new String(buf,0,len));
		s.close();
	}
}

服務端:

package itheima.day23;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer2 {

	public static void main(String[] args) throws IOException {
		
		ServerSocket ss = new ServerSocket(10012);
		Socket s = ss.accept();
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+".....connected");
		InputStream in = s.getInputStream();
		
		byte[] buf = new byte[1024];
		int len = in.read(buf);
		System.out.println(new String(buf,0,len));
		
		OutputStream out = s.getOutputStream();
		out.write("客戶端,客戶端,服務端收到".getBytes());
		s.close();
		ss.close();
	}

}

4、建立一個文本轉換服務器,服務端將客戶端發送來的文本轉成大寫回給客戶端

客戶端:

package itheima.day23;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

//需求:建立一個文本轉換服務器,服務端將客戶端發送來的文本轉成大寫回給客戶端。
// 	    客戶端可以不斷的進行文本轉換,當客戶端輸入over時,轉換結束
//步驟:1、建立服務,2、獲取鍵盤錄入,3、將數據發給服務端,
// 4、獲取服務端回給的大寫數據,5.結束,關閉資源。
 
public  class TransClient {

	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket s = new Socket("100.64.86.48",10021);
		BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
//		打印流,打印的目的:Socket對象的輸出流,自動刷新
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);
//		BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
		BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
		String line = null;
		while((line = bufr.readLine())!=null){
			if("over".equals(line))
				break;	
			out.println(line);
			
//			bufOut.write(line);//寫到輸出流中
//			bufOut.newLine();//結束標記
//			bufOut.flush();
			String str = bufIn.readLine();
			System.out.println("Server:"+str);
		}
	}
}

服務端:

package itheima.day23;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

//服務端:源:socket讀取流;目的:socket輸出流
public class TransServer {

	public static void main(String[] args) throws IOException  {
		
	ServerSocket ss = new ServerSocket(10021);
	Socket s = ss.accept();
	String ip = s.getInetAddress().getHostAddress();
	System.out.println(ip+"....connected");
	BufferedReader bufIn = 
			new BufferedReader(new InputStreamReader(s.getInputStream()));
	PrintWriter out = new PrintWriter(s.getOutputStream(),true);
//	BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
	
	String line = null;
	while((line = bufIn.readLine())!=null){
		System.out.println("client:"+line);
		
		out.println(line.toUpperCase());
		
//		bufOut.write(line.toUpperCase());
//		bufOut.newLine();
//		bufOut.flush();		
	}
	s.close();
	ss.close();
	
	}
}

5、將一個文本上傳給服務器:

客戶端:

package itheima.day23;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class TextClient {

	public static void main(String[] args) throws UnknownHostException, IOException {
		
<span style="white-space:pre">	</span>	Socket s = new Socket("100.64.86.48",10007);
		BufferedReader bufr = 
				new BufferedReader(new FileReader("4.txt"));
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);
		
		String line = null;
		while((line = bufr.readLine())!=null){
			out.println(line);
		}
		s.shutdownOutput();
		BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
		String str = bufIn.readLine();
		System.out.println("server:"+str);
		bufr.close();
		bufIn.close();
		out.close();
		s.close();	
	}
}

服務端:
package itheima.day23;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TextServer {

	public static void main(String[] args) throws IOException {
		
		ServerSocket ss = new ServerSocket(10007);
		Socket s = ss.accept();
		
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+".....connected");
		BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
		PrintWriter out = new PrintWriter(new FileWriter("Server.txt"),true);
	
		String line = null;
		while((line = bufIn.readLine())!=null){	
			out.println(line);
		}
		
		PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
		pw.println("恭喜。上傳成功!");
		s.close();
		ss.close();
	}
}
6、給服務器上傳圖片

客戶端:

package itheima.day24;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/*客戶端:
 * 1、服務端點
 * 2、讀取客戶端已有的圖片數據
 * 3、通過socket 輸出流將數據發送給服務端
 * 4、讀取服務端反饋信息
 * 5、關閉資源
 * */
public class PictureClient {

	public static void main(String[] args) throws UnknownHostException, IOException {
		/*
		if(args.length != 1){
			System.out.println("請選擇一個jpg格式的圖片");
			return ;
		}
		
		File file = new File(args[0]);
		
		if(!(file.exists() && file.isFile())){
			System.out.println("該文件有問題");
			return ;
		}
		
		if(!file.getName().endsWith(".jpg")){
			System.out.println("圖片格式有誤,請重新選擇");
			return ;
		}
		
		if(file.length() > 5*1024*1024){
			System.out.println("文件過大,不安好心");
		}
		*/
		
		Socket s = new Socket("100.64.15.72",10007);
		File file = new File("1.jpg");
		
		FileInputStream fis = new FileInputStream(file);
		OutputStream out = s.getOutputStream();
		
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = fis.read(buf))!=-1){
			out.write(buf,0,len);
		}
//		關閉Socket服務的輸出流,結束標記
		s.shutdownOutput();
		
		InputStream in = s.getInputStream();
		byte[] bufIn = new byte[1024];
		
		int num = in.read(bufIn);
		System.out.println(new String(bufIn,0,num));
		
		fis.close();
		s.close();
	}
}

服務端:
package itheima.day24;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

//所有的服務器都是這個原理
public class PicServer {

	public static void main(String[] args) {
		ServerSocket ss = null;
		try {
			ss = new ServerSocket(10007);
		} catch (IOException e) {
			e.printStackTrace();
		}
		while(true){
			Socket s = null;
			try {
				if(ss!=null)
					s = ss.accept();//阻塞式方法
			} catch (IOException e) {
				e.printStackTrace();
			}
			new Thread(new PicThread(s)).start();
		}
	}
}

//其實實現Runnable接口的子類,就是線程要執行的目標
//只是作爲構造參數傳給Thread類罷了
//每一個任務裏面都有一個Socket服務爲連接進來的客戶服務
class PicThread implements Runnable{
	
	private Socket s;
	PicThread(Socket s){
		this.s = s;
	}
	
	@Override
	public void run() {
		
		int count = 1;
		String ip = s.getInetAddress().getHostAddress();
		try{
			System.out.println(ip+"....connected");
			
			InputStream in = s.getInputStream();
			
			File file = new File(ip+"("+count+")"+".jpg");
			
			while(file.exists()){
				file = new  File(ip+"("+(count++)+")"+".jpg");
			}
			
			FileOutputStream fos = new FileOutputStream(file);
			
			byte[] buf = new byte[1024];
			int len = 0;
			while((len = in.read(buf))!=-1){
				fos.write(buf, 0, len);
			}
			OutputStream out = s.getOutputStream();
			
			out.write("恭喜,上傳成功".getBytes());
			
			fos.close();
			s.close();
		}catch(Exception e){
			throw new RuntimeException(ip+"上傳失敗");
		}
	}
}
7、模擬用戶的登陸

客戶端:

package itheima.day24;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

//客戶端通過鍵盤錄入用戶名,服務端對這個用戶進行校驗
// 如果該用戶存在,在服務端顯示xxx,已登錄;並在客戶端顯示xxx,歡迎光臨
// 如果該用戶存在,在服務端顯示xxx,嘗試登陸;並在客戶端顯示xxx,用戶不存在。
// 最多三次
public class LoginClient {

	public static void main(String[] args) throws UnknownHostException, IOException {
		
		Socket s = new Socket("100.64.15.72",10008);
		BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);
		BufferedReader bufIn = 
				new BufferedReader(new InputStreamReader(s.getInputStream()));
		
		for(int x = 0;x<3;x++){
			String line = bufr.readLine();
			if(line == null)
				break;
			out.println(line);
		
			String info = bufIn.readLine();
			System.out.println("info::"+info);
			
			if(info.contains("歡迎"))
				break;
		}
		bufr.close();
		s.close();
	}
}

服務端:
package itheima.day24;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class LoginServer {

	public static void main(String[] args) throws IOException {
		
		ServerSocket ss = new ServerSocket(10008);
		while(true){
			Socket s = ss.accept();	
			new Thread(new UserThread(s)).start();
		}
	}
}

class UserThread implements Runnable
{
	private Socket s;
	UserThread(Socket s){
		this.s = s;
	}
	
	@Override
	public void run() {
		
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+"....connected");
		
		try {
			for(int x = 0;x<3;x++){
				
				BufferedReader bufIn = 
						new BufferedReader(new InputStreamReader(s.getInputStream()));
				String name = bufIn.readLine();
				if(name == null)
					break;
				BufferedReader bufr = 
						new BufferedReader(new FileReader("user.txt"));
				PrintWriter out = new PrintWriter(s.getOutputStream(),true);
				
				String line = null;
				boolean flag = false;
				while((line = bufr.readLine())!=null){
				if(line.equals(name)){
					flag = true;
					break;
				}	
			}
			if(flag){
				System.out.println(name+",已登錄");
				out.println(name+",歡迎光臨");
				break;
			}else{
				System.out.println(name+"嘗試登陸");
				out.println(name+"用戶名不存在");
				}
			} 
			s.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}	
}
8、定義一個服務器給瀏覽器提供服務:
package itheima.day24;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/*
 演示客戶端和服務端
 1、
  客戶端:瀏覽器
  服務端:自定義
 2、
  客戶端:瀏覽器
  服務端:Tomcat服務器
 */
public class ServerDemo {

	public static void main(String[] args) throws IOException {
		
		ServerSocket ss = new ServerSocket(11000);
		
		Socket s = ss.accept();
		
		System.out.println(s.getInetAddress().getHostAddress());
		
		InputStream in = s.getInputStream();
		
		byte[] buf = new byte[1024*1024];
		int len = in.read(buf);
		
		System.out.println(new String(buf,0,len));
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);
		out.println("客戶端你好");
		
		s.close();
		ss.close();
		
	}

}
/*
  HTTP的請求消息頭
GET / HTTP/1.1    請求方式; 1.1版本
Accept: text/html, application/xhtml+xml,等等; 接收的類型
Accept-Language: zh-CN 支持的語言
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)
Accept-Encoding: gzip, deflate 可接受的編碼
Host: 100.64.130.205:11000
DNT: 1
Connection: Keep-Alive
 */
9、socket走的都是傳輸層,URLConnection中封裝了socket流,走的是應用層:
package itheima.day24;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLConnectionDemo {

	public static void main(String[] args) throws IOException {
		
		URL url = new URL("http://www.baidu.com/s?wd=java%E5%9F%B9%E8%AE%AD&ie=utf-8");
//		走的是應用層,傳輸層把消息頭拆包了,應用層收到的只是數據體
		URLConnection conn =url.openConnection();
		System.out.println(conn);

		InputStream in = conn.getInputStream();
		byte[]   buf = new  byte[1024*1024];
		int len = in.read(buf);
//		接收到的數據沒有消息頭
		System.out.println(new String(buf,0,len));
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章