網絡編程概述
網絡模型
IP地址
一、代碼
InetAddress類使用方法
import java.net.*;
class IPDemo
{
public static void main(String[] args) throws Exception
{
InetAddress i=InetAddress.getLocalHost();
System.out.println(i.toString());
System.out.println("address:"+i.getHostAddress());
System.out.println("name"+i.getHostName());
InetAddress ia=InetAddress.getByName("www.baidu.com");//或者是IP地址
System.out.println("address:"+ia.getHostAddress());
System.out.println("name:"+ia.getHostName());
}
}
TCP和UDP介紹
一、TCP和UDP介紹
Socket介紹
UDP傳輸
UDP發送端
1.建立udpsocket服務。
2.提供數據,並將數據封裝到數據包中
3.通過socket服務的發送功能,將數據包發出去
4.關閉資源
class UdpSend
{
public static void main(String[] args) throws Exception
{
//1.創建udp服務。通過DatagramSocket對象。
DatagramSocket ds=new DatagramSocket();
//2.確定數據,並封裝成包。通過DatagramPacket對象
byte[] data="udp ge men lai la".getBytes();
DatagramPacket dp=new DatagramPacket(data,data.length,InetAddress.getByName("192.168.1.100"),10000);
//3.通過socket服務,將已有的數據包發送出去。通過send方法。
ds.send(dp);
//4.關閉資源
ds.close();
}
}
UDP接收端
1.定義udpsocket服務。通常會監聽一個端口。
其實就是給這個接收網絡應用程序定義一個數字標示。方便於明確哪些數據過來改應用程序處理
2.定義一個數據包,因爲要存儲要接收到的字節數據。因爲數據包對象中有更多功能,可以提取字節數據中的不同信息
3.通過scoket服務的receive方法將收到的數據存入到已定義好的數據包中。
4.通過數據包對象的特有功能,將這些不同的數據取出,打印到控制檯上。
5.關閉資源
class UdpReceive
{
public static void main(String[] args) throws Exception
{
//1.創建udp socket服務,建立端點。
DatagramSocket ds=new DatagramSocket(10000);//不能把創建socket服務放入下面的while循環,因爲會反覆創建調用同一端口的服務。
while(true)//爲了保持接收端始終開放,所以用while循環。
{
//2.定義一個數據包。
byte[] data=new byte[1024];
DatagramPacket dp=new DatagramPacket(data,data.length);
//3.通過服務的receive方法將數據存入到數據包中
ds.receive(dp);
//4.通過數據包的方法獲取其中的數據
String ip=dp.getAddress().getHostAddress();
new String(dp.getData(),0,dp.getLength());
int port=dp.getPort();
System.out.println(ip+"::"+data+"::"+port);
}
//5.關閉資源
//ds.close();
}
}
UDP聊天
class Send implements Runnable
{
private DatagramSocket ds=null;
Send(DatagramSocket ds)
{
this.ds=ds;
}
public void run()
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String len=null;
while((len=br.readLine())!=null)
{
if("8888".equals(len))
break;
DatagramPacket dp=new DatagramPacket(len.getBytes(),len.getBytes().length,InetAddress.getByName("192.168.1.255"),10010);
ds.send(dp);
}
}
catch (Exception e)
{
throw new RuntimeException("數據發送端出現錯誤");
}
}
}
二、接收方法class Receive implements Runnable
{
private DatagramSocket ds=null;
Receive(DatagramSocket ds)
{
this.ds=ds;
}
public void run()
{
try
{
while(true)
{
byte[] by=new byte[1024*64];
DatagramPacket dp=new DatagramPacket(by,by.length);
ds.receive(dp);
String address=dp.getAddress().getHostAddress();
String data=new String(dp.getData(),0,dp.getLength());
System.out.println(address+":"+data);
}
}
catch (Exception e)
{
throw new RuntimeException("數據接收端出現錯誤");
}
}
}
三、主函數public static void main(String[] args) throws Exception
{
DatagramSocket sendSocket=new DatagramSocket();
DatagramSocket receiveSocket=new DatagramSocket(10010);
new Thread(new Send(sendSocket)).start();
new Thread(new Receive(receiveSocket)).start();
}
TCP傳輸
2.客戶端對應的對象是socket.
客戶端對應的對象是serversocket
TCP客戶端
客戶端:
通過查閱socket對象,發現在該對象建立時,就可以去連接指定的主機。
因爲tcp是面向連接的。所以在建立socket服務時,就要有服務端存在,並連接成功。形成通路後,在該通道進行數據傳輸。
步驟:
1.創建Socket服務。並指定要連接的主機和端口
2.爲了發送數據,應該獲取socket流中的輸出流。
class TcpClient
{
public static void main(String[] args) throws Exception
{
//創建客戶端的socket服務。指定目的主機和端口。
Socket s=new Socket("192.168.1.100",10003);
//爲了發送數據,應該獲取socket流中的輸出流。
OutputStream out=s.getOutputStream();
out.write("tcp lai la".getBytes());
s.close();
}
}
TCP服務端
二、代碼
import java.net.*;
class TcpServer
{
public static void main(String[] args)
{
//建立服務端的socket服務。並監聽一個端口。
ServerSocket ss=new ServerSocket(10003);
//通過accept方法獲取連接過來的客戶端對象。
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));
s.close();//關閉客戶端
ss.close();//可選。如果關閉,那麼對外只服務一次。
}
}
TCP客戶端和服務端的互訪
需求:客戶端給服務端發送數據,服務端收到後,給客戶端反饋信息。
1.建立socket服務。指定要連接的主機和端口。
2.獲取socket流中的輸出流。將數據寫入該流中,通過網絡發送給服務端。
3.接收socket流中的輸入流。將服務端反饋的數據獲取到,並打印。
4.關閉客戶端資源。
class TcpClient2
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.1.101",10004);
OutputStream out =s.getOutputStream();
out.write("tcp lai la".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();
}
}
三、服務端代碼class TcpServer2
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10004);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();//獲取IP地址
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();
Thread.sleep(10000);//休眠10秒
out.write("wo shou dao ni de xin xi le".getBytes());
s.close();
}
}
TCP練習
一、需求客戶端給服務端發送文本,服務端會將文本轉成大寫在返回給客戶端,
而且客戶端可以不斷的進行文本轉換。當客戶端輸入over時,轉換結束。
既然是操作設備上的數據,那麼就可以使用io技術,並按照io的操作規律來思考。
源:鍵盤錄入。
目的:網絡設備,即網絡輸出流。
而且操作的是文本數據。可以選擇字符流。
步驟:
1.建立服務。
2.獲取鍵盤錄入。
3.將數據發給服務端。
4.獲取服務端返回的大寫數據。
5.結束,關閉資源。
class TransClient
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.1.101",10005);
//定義讀取鍵盤數據的流對象。
BufferedReader br=
new BufferedReader(new InputStreamReader(System.in));
//定義目的,將數據寫入到socket輸出流,發送給服務端。
BufferedWriter bwOut=
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//定義一個socket讀取流,讀取服務端返回的大寫信息。
BufferedReader brIn=
new BufferedReader(new InputStreamReader(s.getInputStream()));
String line =null;
while((line=br.readLine())!=null)
{
if("over".equals(line))
break;
bwOut.write(line);
bwOut.newLine();
bwOut.flush();
String str=brIn.readLine();
System.out.println("server:"+str);
}
br.close();
s.close();
}
}
源:socket讀取流。
目的:socket輸出流。
class TransServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10005);
//服務器端獲取socket
Socket s=ss.accept();
//獲取連接到服務器的客戶端的ip地址。
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"connected...");
//讀取socket讀取流中的數據。
BufferedReader br=
new BufferedReader(new InputStreamReader(s.getInputStream()));
//將大寫數據寫入到socket輸出流。
BufferedWriter bw=
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
/*上面的一行代碼可以簡化。
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
*/
String str=null;
while((str=br.readLine())!=null)
{
bw.write(str.toUpperCase());
bw.newLine();
bw.flush();
/*
以上三行可以簡化爲
pw.println(str.toUpperCase());
*/
}
ss.close();
}
}
爲什麼呢?
因爲客戶端和服務端都有阻塞式方法。這些方法沒有讀到結束標記,
那麼兩端就會一直等待。
TCP文件複製——客戶端把文件放到服務端存儲
import java.io.*;
import java.net.*;
class TcpCopyFileClient
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.1.101",10006);
BufferedReader br=
new BufferedReader(new FileReader("IPDemo.java"));
BufferedReader brs=
new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
String len=null;
while((len=br.readLine())!=null)
{
pw.println(len);
}
s.shutdownOutput();//如果不添加結束標示,那麼會進入阻塞狀態。
String str=brs.readLine();
System.out.println("server:"+str);
br.close();
s.close();
}
}
二、服務端代碼
class TcpCopyFileServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10006);
Socket s=ss.accept();
PrintWriter pw=new PrintWriter(new FileWriter("server.txt"));
BufferedReader br=
new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pws=new PrintWriter(s.getOutputStream(),true);
String len=null;
while((len=br.readLine())!=null)
{
pw.println(len);
}
pws.println("文件已經收到");
pw.close();
s.close();
ss.close();
}
}
TCP—客戶端併發上傳圖片
一、客戶端代碼
class PicClient
{
public static void main(String[] args) throws Exception
{
if(args.length!=1)
{
System.out.println("請輸入一個jpg文件");
return;//return 代表結束該函數。
}
File f=new File(args[0]);
if((!f.exists())&&(!f.isFile()))
{
System.out.println("文件要麼不存在,要麼不是文件");
return;
}
if(!f.getName().endsWith(".jpg"))
{
System.out.println("文件不是jpg文件");
return;
}
if(f.length()>1024*1024*5)
{
System.out.println("文件過大沒安好心");
return;
}
Socket s=new Socket("192.168.1.101",10008);
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(f));
BufferedOutputStream sbos=new BufferedOutputStream(s.getOutputStream());
BufferedInputStream sbis=new BufferedInputStream(s.getInputStream());
byte[] buf=new byte[1024];
int len=0;
while((len=bis.read(buf))!=-1)
{
sbos.write(buf,0,len);
}
s.shutdownOutput();
len=sbis.read(buf);
System.out.println(new String(buf,0,len));
s.close();
bis.close();
}
}
二、服務器端代碼
1.線程類
class PicThread implements Runnable
{
private Socket s;
PicThread(Socket s)
{
this.s=s;
}
public void run()
{
String ip=s.getInetAddress().getHostAddress();
int count=0;
try
{
System.out.println(ip+"connected...");
File f=new File(ip+".jpg");
while(f.exists())
f=new File(ip+"("+(count++)+")"+".jpg");
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(f));
BufferedInputStream sbis=new BufferedInputStream(s.getInputStream());
BufferedOutputStream sbos=new BufferedOutputStream(s.getOutputStream());
byte[] buf=new byte[1024];
int len=0;
while((len=sbis.read(buf))!=-1)
{
bos.write(buf,0,len);
}
sbos.write("圖片已經收到".getBytes());
sbos.flush();//用緩衝區輸出流一定要記得刷新,否則會報異常。
s.close();
bos.close();
}
catch (Exception e)
{
throw new RuntimeException(ip+"文件上傳失敗");
}
}
}
2.服務器端主函數類
class PicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10008);
while(true)
{
Socket s=ss.accept();
new Thread(new PicThread(s)).start();
}
//ss.close();
}
}
TCP客戶端併發登陸
class LoginClient
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.1.101",10009);
BufferedReader br=
new BufferedReader(new InputStreamReader(System.in));
BufferedReader sbr=
new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
for(int i=0;i<3;i++)
{
String name=br.readLine();
if(name==null)
break;
pw.println(name);
String info=sbr.readLine();
System.out.println("服務器:"+info);
if(info.contains("歡迎"))
break;
}
br.close();
s.close();
}
}
三、服務端代碼class UserLogin implements Runnable
{
private Socket s=null;
UserLogin(Socket s)
{
this.s=s;
}
public void run()
{
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"...connected");
BufferedReader sbr=null;
PrintWriter spw=null;
try
{
sbr=
new BufferedReader(new InputStreamReader(s.getInputStream()));
spw=
new PrintWriter(s.getOutputStream(),true);
}
catch (Exception e)
{
throw new RuntimeException(ip+"Socket輸入輸出流出現問題");
}
for(int i=0;i<3;i++)
{
BufferedReader br=null;
try
{
br=new BufferedReader(new FileReader("UserName.txt"));//分別補貨異常後發現,該代碼需要放在for循環內。因爲要循環讀取文件內容,如果不放在for內,會導致下次循環讀取數據返回null,即已經訪問到文件末尾。
}
catch (Exception e)
{
}
String name=null;
try
{
name=sbr.readLine();//該行在用戶在客戶端按下ctrl+c時,有時會報出異常,有時會正常返回null,具體異常原因不明,可能跟jdk1.7新特性有關。
if(s.isClosed())
break;
}
catch (Exception e)
{
throw new RuntimeException("輸入流讀取出現問題1");
}
if(name==null)
break;
String line=null;
boolean flag=false;
try
{
while((line=br.readLine())!=null)
{
System.out.println(line);
if(line.equals(name))
{
flag=true;
break;
}
}
}
catch (Exception e)
{
throw new RuntimeException("輸入流讀取出現問題2");
}
if(flag)
{
System.out.println(name+"登陸成功");
spw.println("登陸成功--歡迎");
break;
}
else
System.out.println(name+"嘗試登陸");
spw.println(name+"登陸失敗");
try
{
br.close();
}
catch (Exception e)
{
throw new RuntimeException("關閉異常1");
}
}
try
{
s.close();
}
catch (Exception e)
{
throw new RuntimeException("關閉異常2");
}
}
}
自定義瀏覽器客戶端——Tomcat服務端
Connection: Keep-Alive
Accept:
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2
.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; SE 2.X MetaSr 1.0)
Accept-Encoding: gzip, deflate
以上爲請求消息頭,空一行之後的數據爲:請求數據體
import java.io.*;
import java.net.*;
class MyIE
{
public static void main(String[] args) throws IOException
{
Socket s=new Socket("192.168.1.101",8080);
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("GET /myweb/demo.html HTTP/1.1");
pw.println("Accept: */*");
pw.println("Accept-Language: zh-cn");
pw.println("Host: 192.168.1.101:10001");
pw.println("Connection: Keep-Alive");
pw.println();//一定要輸入一個空行,來區分消息頭和消息體
pw.println();
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=null;
while((str=br.readLine())!=null)
{
System.out.println(str);
}
s.close();
}
}
二、Tomcat6.0服務器<html>
<body>
<h1>歡迎進入我的網站</h1>
<font size=5 color=blue>歡迎光臨</font>
<div>
撒旦啊實打實大師阿斯頓阿斯頓愛死</br>
撒大聲地阿斯頓阿斯頓愛死大聲道愛</br>
颯颯大師的阿斯頓阿斯頓愛死愛死阿</br>
</div>
</body>
</html>
自定義圖形界面瀏覽器——Tomcat服務器
一、代碼<pre name="code" class="java">import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class MyIEGUI
{
private Frame f;
private Button b;
private TextField tf;
private TextArea ta;
MyIEGUI()
{
init();
}
private void init()
{
f=new Frame("my frame");
f.setBounds(300,200,500,600);
f.setLayout(new FlowLayout());
tf=new TextField(40);
b=new Button("轉到");
ta=new TextArea(30,50);
f.add(tf);
f.add(b);
f.add(ta);
DemoEvent();
f.setVisible(true);
}
public void DemoEvent()
{
/*1*/f.addWindowListener(new WindowAdapter()//監聽f的WindowEvent e
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
/*4*/b.addActionListener(new ActionListener()//監聽b的ActionEvent
{
public void actionPerformed(ActionEvent e)
{
show();
}
});
/*5*/tf.addActionListener(new ActionListener()//監聽tf的ActionEvent
{
public void actionPerformed(ActionEvent e)
{
show();
}
});
}
public void show() //將在TextField按下回車和點擊Button做出的相同的事件響應進行封裝
{
ta.setText("");
String url=tf.getText();
int index1=url.indexOf("//")+2;
int index2=url.indexOf("/",index1);
String host=url.substring(index1,index2);
String path=url.substring(index2);
String[] arr=host.split(":");
String ip=arr[0];
int port =Integer.parseInt(arr[1]);
socket(ip,port,path);
}
public void socket(String ip,int port,String path)
{
try
{
Socket s=new Socket(ip,port);
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("GET "+path+" HTTP/1.1");
pw.println("Accept: */*");
pw.println("Accept-Language: zh-cn");
pw.println("Host: "+ip+":"+port);
pw.println("Connection: Keep-Alive");
pw.println();//一定要輸入一個空行,來區分消息頭和消息體
pw.println();
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=null;
while((str=br.readLine())!=null)
{
ta.append(str+"\r\n");
}
s.close();
}
catch (Exception e)
{
throw new RuntimeException("出現異常");
}
}
public static void main(String[] args)
{
new MyIEGUI();
}
}
URL——URLConnection
URL
代表一個統一資源定位符,它是指向互聯網“資源”的指針。資源可以是簡單的文件或目錄,也可以是對更爲複雜的對象的引用,例如對數據庫或搜索引擎的查詢。URL
的文件名。2.獲取此 URL
的主機名(如果適用)。
3.獲取此 URL
的路徑部分。
int getPort()
URL
的協議名稱。6.獲取此 URL
的查詢部分
其中,getFile()方法等於getPath()+getQuery();
public void show()
{
try
{
URL url=new URL(tf.getText());
URLConnection uc=url.openConnection();
BufferedReader is=new BufferedReader(new InputStreamReader(uc.getInputStream(),"utf-8"));
ta.setText("");
String str=null;
while((str=is.readLine())!=null)
{
ta.append(str+"\r\n");
}
}
catch (Exception e)
{
throw new RuntimeException("出現異常");
}
}