Java網絡編程

網絡編程三要素:
A:IP地址
B:端口
C:協議
 
舉例:
我想和林青霞說話了。腫麼辦?
A:我要找到林青霞。
B:對她說話,要對耳朵說。
C:我說什麼呢?"I Love You"
可是,她沒學過英語,聽不懂。
我不必說英語,說漢語就能夠了:我愛你
 
IP地址:
網絡中計算機的惟一標識。
 
計算機只能識別二進制的數據,因此咱們的IP地址應該是一個二進制的數據。
可是呢,咱們配置的IP地址確不是二進制的,爲何呢?
IP:192.168.1.100
換算:11000000 10101000 00000001 01100100
假如真是:11000000 10101000 00000001 01100100的話。
咱們若是每次再上課的時候要配置該IP地址,記憶起來就比較的麻煩。
因此,爲了方便表示IP地址,咱們就把IP地址的每個字節上的數據換算成十進制,而後用.分開來表示:
"點分十進制"
 
IP地址的組成:網絡號段+主機號段
A類:第一號段爲網絡號段+後三段的主機號段
一個網絡號:256*256*256 = 16777216
B類:前二號段爲網絡號段+後二段的主機號段
一個網絡號:256*256 = 65536
C類:前三號段爲網絡號段+後一段的主機號段
一個網絡號:256
 
IP地址的分類:
A類 1.0.0.1---127.255.255.254 (1)10.X.X.X是私有地址(私有地址就是在互聯網上不使用,而被用在局域網絡中的地址) (2)127.X.X.X是保留地址,用作循環測試用的。
B類 128.0.0.1---191.255.255.254 172.16.0.0---172.31.255.255是私有地址。169.254.X.X是保留地址。
C類 192.0.0.1---223.255.255.254 192.168.X.X是私有地址
D類 224.0.0.1---239.255.255.254
E類 240.0.0.1---247.255.255.254
 
兩個DOS命令:
ipconfig 查看本機ip地址
ping 後面跟ip地址。測試本機與指定的ip地址間的通訊是否有問題
 
特殊的IP地址:
127.0.0.1 迴環地址(表示本機)
x.x.x.255 廣播地址
x.x.x.0 網絡地址
 
端口號:
正在運行的程序的標識。
有效端口:0~65535,其中0~1024系統使用或保留端口。
 
協議:
通訊的規則
 
UDP:
把數據打包
數據有限制
不創建鏈接
速度快
不可靠
 
TCP:
創建鏈接通道
數據無限制
速度慢
可靠
 
舉例:
UDP:發短信
TCP:打電話
===================================
package cn.itcast_01;
 
import java.net.InetAddress;
import java.net.UnknownHostException;
 
/*
* 若是一個類沒有構造方法:
* A:成員所有是靜態的(Math,Arrays,Collections)
* B:單例設計模式(Runtime)
* C:類中有靜態方法返回該類的對象(InetAddress)
* class Demo {
* private Demo(){}
*
* public static Demo getXxx() {
* return new Demo();
* }
* }
*
* 看InetAddress的成員方法:
* public static InetAddress getByName(String host):根據主機名或者IP地址的字符串表示獲得IP地址對象
*/
public class InetAddressDemo {
public static void main(String[] args) throws UnknownHostException {
// public static InetAddress getByName(String host)
InetAddress address = InetAddress.getByName("liuyi");
// InetAddress address = InetAddress.getByName("192.168.12.92");
//InetAddress address = InetAddress.getByName("192.168.12.63");
 
// 獲取兩個東西:主機名,IP地址
// public String getHostName()
String name = address.getHostName();
// public String getHostAddress()
String ip = address.getHostAddress();
System.out.println(name + "---" + ip);
}
}
====================================================
package cn.itcast_02;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
 
/*
* UDP協議接收數據:
* A:建立接收端Socket對象
* B:建立一個數據包(接收容器)
* C:調用Socket對象的接收方法接收數據
* D:解析數據包,並顯示在控制檯
* E:釋放資源
*/
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
// 建立接收端Socket對象
// DatagramSocket(int port)
DatagramSocket ds = new DatagramSocket(10086);
 
// 建立一個數據包(接收容器)
// DatagramPacket(byte[] buf, int length)
byte[] bys = new byte[1024];
int length = bys.length;
DatagramPacket dp = new DatagramPacket(bys, length);
 
// 調用Socket對象的接收方法接收數據
// public void receive(DatagramPacket p)
ds.receive(dp); // 阻塞式
 
// 解析數據包,並顯示在控制檯
// 獲取對方的ip
// public InetAddress getAddress()
InetAddress address = dp.getAddress();
String ip = address.getHostAddress();
// public byte[] getData():獲取數據緩衝區
// public int getLength():獲取數據的實際長度
byte[] bys2 = dp.getData();
int len = dp.getLength();
String s = new String(bys2, 0, len);
System.out.println(ip + "傳遞的數據是:" + s);
 
// 釋放資源
ds.close();
}
}
===============================================================
package cn.itcast_02;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/*
* UDP協議發送數據:
* A:建立發送端Socket對象
* B:建立數據,並把數據打包
* C:調用Socket對象的發送方法發送數據包
* D:釋放資源
*/
public class SendDemo {
public static void main(String[] args) throws IOException {
// 建立發送端Socket對象
// DatagramSocket()
DatagramSocket ds = new DatagramSocket();
 
// 建立數據,並把數據打包
// DatagramPacket(byte[] buf, int length, InetAddress address, int port)
// 建立數據
byte[] bys = "hello,udp,我來了".getBytes();
// 長度
int length = bys.length;
// IP地址對象
InetAddress address = InetAddress.getByName("192.168.12.92");
// 端口
int port = 10086;
DatagramPacket dp = new DatagramPacket(bys, length, address, port);
 
// 調用Socket對象的發送方法發送數據包
// public void send(DatagramPacket p)
ds.send(dp);
 
// 釋放資源
ds.close();
}
}
==========================================================
package cn.itcast_03;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
 
/*
* 屢次啓動接收端:
* java.net.BindException: Address already in use: Cannot bind
* 端口被佔用。
*/
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
// 建立接收端的Socket對象
DatagramSocket ds = new DatagramSocket(12345);
 
// 建立一個包裹
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
 
// 接收數據
ds.receive(dp);
 
// 解析數據
String ip = dp.getAddress().getHostAddress();
String s = new String(dp.getData(), 0, dp.getLength());
System.out.println("from " + ip + " data is : " + s);
 
// 釋放資源
ds.close();
}
}
=====================================================
package cn.itcast_03;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
 
public class SendDemo {
public static void main(String[] args) throws IOException {
// 建立發送端的Socket對象
DatagramSocket ds = new DatagramSocket();
 
// 建立數據並打包
byte[] bys = "helloworld".getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length,
InetAddress.getByName("192.168.12.92"), 12345);
 
// 發送數據
ds.send(dp);
 
// 釋放資源
ds.close();
}
}
=================================================
package cn.itcast_04;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
 
/*
* 屢次啓動接收端:
* java.net.BindException: Address already in use: Cannot bind
* 端口被佔用。
*/
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
// 建立接收端的Socket對象
DatagramSocket ds = new DatagramSocket(12345);
 
while (true) {
// 建立一個包裹
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
 
// 接收數據
ds.receive(dp);
 
// 解析數據
String ip = dp.getAddress().getHostAddress();
String s = new String(dp.getData(), 0, dp.getLength());
System.out.println("from " + ip + " data is : " + s);
}
 
// 釋放資源
// 接收端應該一直開着等待接收數據,是不須要關閉
// ds.close();
}
}
===================================
package cn.itcast_04;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
 
/*
* 數據來自於鍵盤錄入
* 鍵盤錄入數據要本身控制錄入結束。
*/
public class SendDemo {
public static void main(String[] args) throws IOException {
// 建立發送端的Socket對象
DatagramSocket ds = new DatagramSocket();
 
// 封裝鍵盤錄入數據
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = br.readLine()) != null) {
if ("886".equals(line)) {
break;
}
 
// 建立數據並打包
byte[] bys = line.getBytes();
// DatagramPacket dp = new DatagramPacket(bys, bys.length,
// InetAddress.getByName("192.168.12.92"), 12345);
DatagramPacket dp = new DatagramPacket(bys, bys.length,
InetAddress.getByName("192.168.12.255"), 12345);
 
// 發送數據
ds.send(dp);
}
 
// 釋放資源
ds.close();
}
}
===================================================
package cn.itcast_05;
 
import java.io.IOException;
import java.net.DatagramSocket;
 
/*
* 經過多線程改進剛纔的聊天程序,這樣我就能夠實如今一個窗口發送和接收數據了
*/
public class ChatRoom {
public static void main(String[] args) throws IOException {
DatagramSocket dsSend = new DatagramSocket();
DatagramSocket dsReceive = new DatagramSocket(12306);
 
SendThread st = new SendThread(dsSend);
ReceiveThread rt = new ReceiveThread(dsReceive);
 
Thread t1 = new Thread(st);
Thread t2 = new Thread(rt);
 
t1.start();
t2.start();
}
}
 
package cn.itcast_05;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
 
public class ReceiveThread implements Runnable {
private DatagramSocket ds;
 
public ReceiveThread(DatagramSocket ds) {
this.ds = ds;
}
 
@Override
public void run() {
try {
while (true) {
// 建立一個包裹
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
 
// 接收數據
ds.receive(dp);
 
// 解析數據
String ip = dp.getAddress().getHostAddress();
String s = new String(dp.getData(), 0, dp.getLength());
System.out.println("from " + ip + " data is : " + s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
 
}
package cn.itcast_05;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
 
public class SendThread implements Runnable {
 
private DatagramSocket ds;
 
public SendThread(DatagramSocket ds) {
this.ds = ds;
}
 
@Override
public void run() {
try {
// 封裝鍵盤錄入數據
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
while ((line = br.readLine()) != null) {
if ("886".equals(line)) {
break;
}
 
// 建立數據並打包
byte[] bys = line.getBytes();
// DatagramPacket dp = new DatagramPacket(bys, bys.length,
// InetAddress.getByName("192.168.12.92"), 12345);
DatagramPacket dp = new DatagramPacket(bys, bys.length,
InetAddress.getByName("192.168.12.255"), 12306);
 
// 發送數據
ds.send(dp);
}
 
// 釋放資源
ds.close();
} catch (IOException e) {
e.printStackTrace();
}
}
 
}
=====================================================================
package cn.itcast_06;
 
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
 
/*
* TCP協議發送數據:
* A:建立發送端的Socket對象
* 這一步若是成功,就說明鏈接已經創建成功了。
* B:獲取輸出流,寫數據
* C:釋放資源
*
* 鏈接被拒絕。TCP協議必定要先看服務器。
* java.net.ConnectException: Connection refused: connect
*/
public class ClientDemo {
public static void main(String[] args) throws IOException {
// 建立發送端的Socket對象
// Socket(InetAddress address, int port)
// Socket(String host, int port)
// Socket s = new Socket(InetAddress.getByName("192.168.12.92"), 8888);
Socket s = new Socket("192.168.12.92", 8888);
 
// 獲取輸出流,寫數據
// public OutputStream getOutputStream()
OutputStream os = s.getOutputStream();
os.write("hello,tcp,我來了".getBytes());
 
// 釋放資源
s.close();
}
}
=====================================================================package cn.itcast_06;
 
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
/*
* TCP協議接收數據:
* A:建立接收端的Socket對象
* B:監聽客戶端鏈接。返回一個對應的Socket對象
* C:獲取輸入流,讀取數據顯示在控制檯
* D:釋放資源
*/
public class ServerDemo {
public static void main(String[] args) throws IOException {
// 建立接收端的Socket對象
// ServerSocket(int port)
ServerSocket ss = new ServerSocket(8888);
 
// 監聽客戶端鏈接。返回一個對應的Socket對象
// public Socket accept()
Socket s = ss.accept(); // 偵聽並接受到此套接字的鏈接。此方法在鏈接傳入以前一直阻塞。
 
// 獲取輸入流,讀取數據顯示在控制檯
InputStream is = s.getInputStream();
 
byte[] bys = new byte[1024];
int len = is.read(bys); // 阻塞式方法
String str = new String(bys, 0, len);
 
String ip = s.getInetAddress().getHostAddress();
 
System.out.println(ip + "---" + str);
 
// 釋放資源
s.close();
// ss.close(); //這個不該該關閉
}
}
=====================================================================package cn.itcast_0 7;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
 
public class ClientDemo {
public static void main(String[] args) throws IOException {
// 建立客戶端Socket對象
Socket s = new Socket("192.168.12.92", 9999);
 
// 獲取輸出流
OutputStream os = s.getOutputStream();
os.write("今每天氣很好,適合睡覺".getBytes());
 
// 獲取輸入流
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);// 阻塞
String client = new String(bys, 0, len);
System.out.println("client:" + client);
 
// 釋放資源
s.close();
}
}
===================================================
package cn.itcast_07;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
public class ServerDemo {
public static void main(String[] args) throws IOException {
// 建立服務器Socket對象
ServerSocket ss = new ServerSocket(9999);
 
// 監聽客戶端的鏈接
Socket s = ss.accept(); // 阻塞
 
// 獲取輸入流
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys); // 阻塞
String server = new String(bys, 0, len);
System.out.println("server:" + server);
 
// 獲取輸出流
OutputStream os = s.getOutputStream();
os.write("數據已經收到".getBytes());
 
// 釋放資源
s.close();
// ss.close();
}
}
=====================================================================package cn.itcast_08;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
 
/*
* 客戶端鍵盤錄入,服務器輸出到控制檯
*/
public class ClientDemo {
public static void main(String[] args) throws IOException {
// 建立客戶端Socket對象
Socket s = new Socket("192.168.12.92", 22222);
 
// 鍵盤錄入數據
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 把通道內的流給包裝一下
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
 
String line = null;
while ((line = br.readLine()) != null) {
// 鍵盤錄入數據要自定義結束標記
if ("886".equals(line)) {
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
 
// 釋放資源
// bw.close();
// br.close();
s.close();
}
}
 
================================================================================================
 
package cn.itcast_08;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
 
public class ServerDemo {
public static void main(String[] args) throws IOException {
// 建立服務器Socket對象
ServerSocket ss = new ServerSocket(22222);
 
// 監聽客戶端鏈接
Socket s = ss.accept();
 
// 包裝通道內容的流
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
 
// br.close();
s.close();
// ss.close();
}
}
 
========================================
package cn.itcast_09;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
 
public class ServerDemo {
public static void main(String[] args) throws IOException {
// 建立服務器Socket對象
ServerSocket ss = new ServerSocket(23456);
 
// 監聽客戶端鏈接
Socket s = ss.accept();
 
// 封裝通道內的數據
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
// 封裝文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
 
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
 
bw.close();
// br.close();
s.close();
// ss.close();
}
}
 
==============================================
package cn.itcast_10;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
 
/*
* 客戶端文本文件,服務器輸出到控制檯
*/
public class ClientDemo {
public static void main(String[] args) throws IOException {
// 建立Socket對象
Socket s = new Socket("192.168.12.92", 34567);
 
// 封裝文本文件
BufferedReader br = new BufferedReader(new FileReader(
"InetAddressDemo.java"));
// 封裝通道內的流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
 
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
 
br.close();
s.close();
}
}
 
====================================================
package cn.itcast_10;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
 
public class ServerDemo {
public static void main(String[] args) throws IOException {
// 建立服務器Socket對象
ServerSocket ss = new ServerSocket(34567);
 
// 監聽客戶端鏈接
Socket s = ss.accept();
 
// 封裝通道內的流
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
 
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
 
 
s.close();
}
}
============================================================
package cn.itcast_11;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
 
public class UploadClient {
public static void main(String[] args) throws IOException {
// 建立客戶端Socket對象
Socket s = new Socket("192.168.12.92", 11111);
 
// 封裝文本文件
BufferedReader br = new BufferedReader(new FileReader(
"InetAddressDemo.java"));
// 封裝通道內流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
 
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
 
// 釋放資源
br.close();
s.close();
}
}
==============================================
package cn.itcast_11;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
 
public class UploadServer {
public static void main(String[] args) throws IOException {
// 建立服務器端的Socket對象
ServerSocket ss = new ServerSocket(11111);
 
// 監聽客戶端鏈接
Socket s = ss.accept();
 
// 封裝通道內的流
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
// 封裝文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java"));
 
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
 
bw.close();
s.close();
}
}
===================================================
package cn.itcast_12;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
 
/*
* 按照咱們正常的思路加入反饋信息,結果卻沒反應。爲何呢?
* 讀取文本文件是能夠以null做爲結束信息的,可是呢,通道內是不能這樣結束信息的。
* 因此,服務器根本就不知道你結束了。而你還想服務器給你反饋。因此,就相互等待了。
*
* 如何解決呢?
* A:在多寫一條數據,告訴服務器,讀取到這條數聽說明我就結束,你也結束吧。
* 這樣作能夠解決問題,可是很差。
* B:Socket對象提供了一種解決方案
* public void shutdownOutput()
*/
 
public class UploadClient {
public static void main(String[] args) throws IOException {
// 建立客戶端Socket對象
Socket s = new Socket("192.168.12.92", 11111);
 
// 封裝文本文件
BufferedReader br = new BufferedReader(new FileReader(
"InetAddressDemo.java"));
// 封裝通道內流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
 
String line = null;
while ((line = br.readLine()) != null) { // 阻塞
bw.write(line);
bw.newLine();
bw.flush();
}
 
//自定義一個結束標記
// bw.write("over");
// bw.newLine();
// bw.flush();
 
//Socket提供了一個終止,它會通知服務器你別等了,我沒有數據過來了
s.shutdownOutput();
 
// 接收反饋
BufferedReader brClient = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String client = brClient.readLine(); // 阻塞
System.out.println(client);
 
// 釋放資源
br.close();
s.close();
}
}
=====================================================================
package cn.itcast_12;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
 
public class UploadServer {
public static void main(String[] args) throws IOException {
// 建立服務器端的Socket對象
ServerSocket ss = new ServerSocket(11111);
 
// 監聽客戶端鏈接
Socket s = ss.accept();// 阻塞
 
// 封裝通道內的流
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
// 封裝文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java"));
 
String line = null;
while ((line = br.readLine()) != null) { // 阻塞
// if("over".equals(line)){
// break;
// }
bw.write(line);
bw.newLine();
bw.flush();
}
 
// 給出反饋
BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
bwServer.write("文件上傳成功");
bwServer.newLine();
bwServer.flush();
 
// 釋放資源
bw.close();
s.close();
}
}
======================================================
package cn.itcast_13;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
 
public class UploadClient {
public static void main(String[] args) throws IOException {
// 建立客戶端Socket對象
Socket s = new Socket("192.168.12.92", 19191);
 
// 封裝圖片文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
"林青霞.jpg"));
// 封裝通道內的流
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
 
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
bos.flush();
}
 
s.shutdownOutput();
 
// 讀取反饋
InputStream is = s.getInputStream();
byte[] bys2 = new byte[1024];
int len2 = is.read(bys2);
String client = new String(bys2, 0, len2);
System.out.println(client);
 
// 釋放資源
bis.close();
s.close();
}
}
===================================================
package cn.itcast_13;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
public class UploadServer {
public static void main(String[] args) throws IOException {
// 建立服務器Socket對象
ServerSocket ss = new ServerSocket(19191);
 
// 監聽客戶端鏈接
Socket s = ss.accept();
 
// 封裝通道內流
BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
// 封裝圖片文件
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("mn.jpg"));
 
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
bos.flush();
}
 
// 給一個反饋
OutputStream os = s.getOutputStream();
os.write("圖片上傳成功".getBytes());
 
bos.close();
s.close();
}
}
 
====================================
package cn.itcast_14;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
 
public class UploadClient {
public static void main(String[] args) throws IOException {
// 建立客戶端Socket對象
Socket s = new Socket("192.168.12.92", 11111);
 
// 封裝文本文件
BufferedReader br = new BufferedReader(new FileReader(
"InetAddressDemo.java"));
// 封裝通道內流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
 
String line = null;
while ((line = br.readLine()) != null) { // 阻塞
bw.write(line);
bw.newLine();
bw.flush();
}
 
// Socket提供了一個終止,它會通知服務器你別等了,我沒有數據過來了
s.shutdownOutput();
 
// 接收反饋
BufferedReader brClient = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String client = brClient.readLine(); // 阻塞
System.out.println(client);
 
// 釋放資源
br.close();
s.close();
}
}
=============================================================
package cn.itcast_14;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
/*
* 經過while循環能夠改進一個服務器接收多個客戶端。
* 可是這個是有問題的。
* 若是是這種狀況,假設我還有張三,李四,王五這三我的分別執行客戶端
* 張三:好好學習.avi(100M) 256k
* 李四:每天向上.mp3(3M) 1M
* 王五:ILoveJava.txt(1k) 100M
*/
public class UploadServer {
public static void main(String[] args) throws IOException {
// 建立服務器端的Socket對象
ServerSocket ss = new ServerSocket(11111);
 
while (true) {
// 監聽客戶端鏈接
Socket s = ss.accept();// 阻塞
 
// 封裝通道內的流
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
// 封裝文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java"));
 
String line = null;
while ((line = br.readLine()) != null) { // 阻塞
bw.write(line);
bw.newLine();
bw.flush();
}
 
// 給出反饋
BufferedWriter bwServer = new BufferedWriter(
new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件上傳成功");
bwServer.newLine();
bwServer.flush();
 
// 釋放資源
bw.close();
s.close();
}
}
}
=============================================
package cn.itcast_15;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
 
public class UploadClient {
public static void main(String[] args) throws IOException {
// 建立客戶端Socket對象
Socket s = new Socket("192.168.12.92", 11111);
 
// 封裝文本文件
// BufferedReader br = new BufferedReader(new FileReader(
// "InetAddressDemo.java"));
BufferedReader br = new BufferedReader(new FileReader(
"ReceiveDemo.java"));
// 封裝通道內流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
 
String line = null;
while ((line = br.readLine()) != null) { // 阻塞
bw.write(line);
bw.newLine();
bw.flush();
}
 
// Socket提供了一個終止,它會通知服務器你別等了,我沒有數據過來了
s.shutdownOutput();
 
// 接收反饋
BufferedReader brClient = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String client = brClient.readLine(); // 阻塞
System.out.println(client);
 
// 釋放資源
br.close();
s.close();
}
}
=========================================================
 
package cn.itcast_15;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
 
public class UserThread implements Runnable {
private Socket s;
 
public UserThread(Socket s) {
this.s = s;
}
 
@Override
public void run() {
try {
// 封裝通道內的流
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
// 封裝文本文件
// BufferedWriter bw = new BufferedWriter(new
// FileWriter("Copy.java"));
 
// 爲了防止名稱衝突
String newName = System.currentTimeMillis() + ".java";
BufferedWriter bw = new BufferedWriter(new FileWriter(newName));
 
String line = null;
while ((line = br.readLine()) != null) { // 阻塞
bw.write(line);
bw.newLine();
bw.flush();
}
 
// 給出反饋
BufferedWriter bwServer = new BufferedWriter(
new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件上傳成功");
bwServer.newLine();
bwServer.flush();
 
// 釋放資源
bw.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
 
}
==================================
相關文章
相關標籤/搜索