Java網(wǎng)絡(luò)編程之入門(mén)篇
一、網(wǎng)絡(luò)基礎(chǔ)


二、網(wǎng)絡(luò)協(xié)議



實(shí)現(xiàn)TCP的網(wǎng)絡(luò)編程
例子1:客戶端發(fā)送信息給服務(wù)端,服務(wù)端將數(shù)據(jù)顯示在控制臺(tái)上
public class TCPTest1 {
//客戶端
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
try {
//1.創(chuàng)建Socket對(duì)象,指明服務(wù)器端的ip和端口號(hào)
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 8899);
//2.獲取一個(gè)輸出流,用于輸出數(shù)據(jù)
os = socket.getOutputStream();
//3.寫(xiě)出數(shù)據(jù)的操作
os.write("你好,我是客戶端mm".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.資源的關(guān)閉
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服務(wù)端
@Test
public void server(){
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1.創(chuàng)建服務(wù)器的ServerSocket,指明自己的端口號(hào)
ss = new ServerSocket(8899);
//2.調(diào)用accept()表示接收來(lái)自于客戶端的socket
socket = ss.accept();
//3.獲取輸入流
is = socket.getInputStream();
//不建議這樣寫(xiě),可能會(huì)有亂碼
// byte[] buffer = new byte[1024];
// int len;
// while((len = is.read(buffer)) != -1){
// String str = new String(buffer,0,len);
// System.out.println(str);
// }
//4.讀取輸入流中的數(shù)據(jù)
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[5];
int len;
while((len = is.read(buffer)) != -1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.關(guān)閉資源
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(ss != null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
實(shí)現(xiàn)TCP的網(wǎng)絡(luò)編程
例題2:客戶端發(fā)送文件給服務(wù)端,服務(wù)端將文件保存在本地。
public class TCPTest2 {
//這里異常處理的方式應(yīng)該使用try-catch-finally
@Test
public void client() throws IOException {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("beauty.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
fis.close();
os.close();
socket.close();
}
//這里異常處理的方式應(yīng)該使用try-catch-finally
@Test
public void server() throws IOException {
ServerSocket ss = new ServerSocket(9090);
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("beauty1.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
fos.close();
is.close();
socket.close();
ss.close();
}
}
實(shí)現(xiàn)TCP的網(wǎng)絡(luò)編程
例題3:從客戶端發(fā)送文件給服務(wù)端,服務(wù)端保存到本地,并返回"發(fā)送成功"給客戶端。并關(guān)閉相應(yīng)的連接
public class TCPTest3 {
@Test
public void client() throws IOException {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("beauty.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
//服務(wù)區(qū)端給予客戶端反饋
OutputStream os1 = socket.getOutputStream();
os.write("你好,美女,照片我以收到,非常漂亮!".getBytes());
fis.close();
os.close();
socket.close();
os1.close();
}
//這里異常處理的方式應(yīng)該使用try-catch-finally
@Test
public void server() throws IOException {
ServerSocket ss = new ServerSocket(9090);
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("beauty2.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
//接受來(lái)自于服務(wù)器端的數(shù)據(jù),并顯示到控制臺(tái)上
InputStream is1 = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bufferr = new byte[20];
int len1;
while((len1 = is1.read(buffer)) != -1){
baos.write(buffer,0,len1);
}
System.out.println(baos.toString());
fos.close();
is.close();
socket.close();
ss.close();
baos.close();
}
}

UDP協(xié)議的網(wǎng)絡(luò)編程
public class UDPTest {
@Test
public void sender() throws IOException {
DatagramSocket socket = new DatagramSocket();
String str = "我是UDP方式發(fā)送的導(dǎo)彈";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
socket.send(packet);
socket.close();
}
@Test
public void receiver() throws IOException {
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
}


URL類(lèi)



URL網(wǎng)絡(luò)編程
1.URL:統(tǒng)一資源定位符,對(duì)應(yīng)著互聯(lián)網(wǎng)的某一資源地址
2.格式:
http://localhost:8080/examples/beauty.jpg?username=Tom
協(xié)議 主機(jī)名 端口號(hào) 資源地址 參數(shù)列表
public class URLTest {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/examples/beauty.jpg?username=Tom");
// public String getProtocol() 獲取該URL的協(xié)議名
System.out.println(url.getProtocol());// http
// public String getHost() 獲取該URL的主機(jī)名
System.out.println(url.getHost());//localhost
// public String getPort() 獲取該URL的端口號(hào)
System.out.println(url.getPort());// 8080
// public String getPath() 獲取該URL的文件路徑
System.out.println(url.getPath());//examples/beauty.jpg
// public String getFile() 獲取該URL的文件名
System.out.println(url.getFile());//examples/beauty.jpg?username=Tom
// public String getQuery() 獲取該URL的查詢(xún)名
System.out.println(url.getQuery());//username=Tom
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
到此這篇關(guān)于Java網(wǎng)絡(luò)編程之入門(mén)篇的文章就介紹到這了,更多相關(guān)Java網(wǎng)絡(luò)編程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot帶你實(shí)現(xiàn)一個(gè)點(diǎn)餐小程序
有個(gè)小伙伴臨時(shí)找到我,要開(kāi)發(fā)一個(gè)點(diǎn)餐的系統(tǒng),時(shí)間比較著急,給了2天的時(shí)間。馬馬虎虎的搞出來(lái)了,頭發(fā)掉了一撮!下面介紹下本系統(tǒng),感興趣的小伙伴,可以參考開(kāi)發(fā)下2022-07-07
java虛擬機(jī)學(xué)習(xí)筆記基礎(chǔ)篇
在本篇文章里小編給大家整理的是關(guān)于java虛擬機(jī)學(xué)習(xí)筆記的相關(guān)內(nèi)容,分享了一些基礎(chǔ)知識(shí)點(diǎn),需要的朋友們參考下。2019-06-06
JAVAEE中用Session簡(jiǎn)單實(shí)現(xiàn)購(gòu)物車(chē)功能示例代碼
本篇文章主要介紹了JAVAEE中用Session簡(jiǎn)單實(shí)現(xiàn)購(gòu)物車(chē)功能示例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03
Java多線程Thread基礎(chǔ)學(xué)習(xí)
每一個(gè)正在執(zhí)行的程序都是一個(gè)進(jìn)程,資源只有一塊,所以在同一時(shí)間段會(huì)有多個(gè)程序同時(shí)執(zhí)行,但是在一個(gè)時(shí)間點(diǎn)上,只能由一個(gè)程序執(zhí)行,多線程是在一個(gè)進(jìn)程的基礎(chǔ)之上的進(jìn)一步劃分,需要的朋友可以參考下2023-04-04
SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁(yè)的項(xiàng)目實(shí)踐
MyBatis-Plus是基于MyBatis的持久層增強(qiáng)工具,提供簡(jiǎn)化CRUD、代碼生成器、條件構(gòu)造器、分頁(yè)及樂(lè)觀鎖等功能,極大簡(jiǎn)化了開(kāi)發(fā)工作量并提高了開(kāi)發(fā)效率,本文就來(lái)介紹一下SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁(yè)的項(xiàng)目實(shí)踐,感興趣的可以了解一下2024-11-11
Java 數(shù)據(jù)結(jié)構(gòu)算法Collection接口迭代器示例詳解
這篇文章主要為大家介紹了Java 數(shù)據(jù)結(jié)構(gòu)算法Collection接口迭代器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

