JAVA網絡編程複習

一 網絡基本知識
1.計算機網絡OSI分層:物,數,網,傳,會,表,應 七層
TCP/IP協議 分爲四層 1-2 網絡接口層 3 網絡層 4 傳輸層 5-7 應用層

2.網絡通訊三要素:
(1) ip地址:主機的唯一標識 佔4個字節 例如本機127.0.0.1 對應獲取的類InetAddress
(2) 端口號: 0-65535
(3) 傳輸協議: 是計算機網絡進行數據交換建立的規則
TCP (Tranfer Control Protocol) 面向連接
UDP(User Datagram Protocol) 面向無連接

3 實例1: TCP 客戶端-服務端。
客戶端:
public class ClientDemo {
public static void main(String[] args) throws Exception, Exception {
//1.創建連接
Socket scoket=new Socket(“127.0.0.1”, 8088);
//2.從鍵盤接收
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//3讀取發送過來的數據
BufferedReader reader=new BufferedReader(new InputStreamReader(scoket.getInputStream()));
//4.寫出去
PrintWriter pw=new PrintWriter(scoket.getOutputStream(),true);
String len=null;
while((len=br.readLine())!=null){
if(len.equals(“By”)){
break;
}
pw.println(len);
System.out.println(reader.readLine());
}
System.out.println(“已斷開”);
pw.close();
reader.close();
scoket.close();
br.close();
}
}
服務端:
public class ServerDemo {
public static void main(String[] args) throws Exception {
//1.創建連接
ServerSocket ssoket=new ServerSocket(8088);
//serversocket接收成socket
Socket socket = ssoket.accept();
System.out.println(InetAddress.getLocalHost().getHostAddress()+”連接成功”);
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);
String len=null;
while((len=br.readLine())!=null){
if(“By”.equals(len)){
break;
}
System.out.println(len);//接收客戶端消息打印到控制檯
pw.println(len.toUpperCase());//變成大寫返回給客戶端
}
pw.close();
br.close();
socket.close();
ssoket.close();
}
}
運行時 先運行服務端,在開客戶端

實例二: UDP 發送端到接收端
發送端:
public class Client1Demo {
public static void main(String[] args) throws SocketException, Exception {
//1.創建數據連接
DatagramSocket ds=new DatagramSocket();
//2.發送的類容
String str=”nihao”;
byte[] buf=str.getBytes();
//3.發送的類容打包 四參數 數組 長度 主機號 端口號
DatagramPacket dp=new DatagramPacket(buf,buf.length, InetAddress.getByName(“localhost”), 9898);
//4.發送
ds.send(dp);
//1.數組接收
byte[] buf1=new byte[20];
//數據包接收返回的內容
DatagramPacket dp1=new DatagramPacket(buf1, buf1.length);
//接收
ds.receive(dp1);
System.out.println(new String(buf1,0,dp1.getLength()));
ds.close();
}
}

接收端:
public class Server1 {
public static void main(String[] args) throws Exception {
//接收
DatagramSocket ds=new DatagramSocket(9898);
byte[] buf1=new byte[20];
DatagramPacket dp1=new DatagramPacket(buf1, buf1.length);
ds.receive(dp1);
System.out.println(new String(buf1,0,dp1.getLength()));

//返回
String str=”wobuhao”;
byte[] buf=str.getBytes();
//接收方發送內容 可根據接收時的的數據包拿到對應發送方的端口號 主機號
DatagramPacket dp=new DatagramPacket(buf,buf.length,dp1.getAddress(),dp1.getPort());
ds.send(dp);
ds.close();
}
}
先開接收端,再開發送端
只有TCP才涉及IO流的操作,UDP不涉及它有數據包封裝

4.URL編程
URL:統一資源定位符 —->協議名:\機器名+端口號+文件名+內部引用 http://www.tomcat.com:80/1.html#BOTTOM

實例:從網上下載照片
public class URLDemo {
public static void main(String[] args) {
try {
DownloadUtil.dowmLoad(“http://preview.quanjing.com/mf101/mf700-08697971.jpg“, “1.jpg”, “e:\a”);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class DownloadUtil{
public static void dowmLoad(String urlstr,String filename,String fileroad) throws IOException{
URL url=new URL(urlstr);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();//抽象類不能實例化,
File file=new File(filename);
if(!file.exists()){
file.mkdirs();
}
byte[] buff=new byte[1024];
int len=0;
OutputStream os=new FileOutputStream(file.getAbsolutePath()+”\”+filename);
while((len=is.read(buff))!=-1){
System.out.println(len);
os.write(buff,0,len);
}
os.close();
is.close();
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章