網(wǎng)絡(luò)編程<TCP>

要想實(shí)現(xiàn)網(wǎng)絡(luò)傳輸,需要考慮的問題有哪些?

1.1 如何才能準(zhǔn)確的定位網(wǎng)絡(luò)上的一臺(tái)主機(jī)?
1.2 如何才能進(jìn)行可靠的、高效的數(shù)據(jù)傳輸?

簡(jiǎn)單的說,你要知道數(shù)據(jù)的目的地(IP),你要用什么渠道傳遞數(shù)據(jù)(TCP/UDP);

java如何實(shí)現(xiàn)的網(wǎng)絡(luò)通信

使用IP地址---定位一臺(tái)主機(jī) 使用端口號(hào)---定位一個(gè)應(yīng)用

1).如何創(chuàng)建一個(gè)InetAddress的對(duì)象?getByName(""); 比如:InetAddress inet = InetAddress.getByName("192.168.10.165");
2).如何獲取本機(jī)的一個(gè)InetAddress的對(duì)象?getLocalHost()
域名:getHostName() ip:getHostAddress()

對(duì)應(yīng)有協(xié)議

對(duì)于傳輸層而言:分為TCP UDP (了解)

TCP的編程:  Socket ServerSocket
例子:
1.客戶端發(fā)送內(nèi)容給服務(wù)端,服務(wù)端將內(nèi)容打印到控制臺(tái)上。

2.客戶端發(fā)送內(nèi)容給服務(wù)端,服務(wù)端給予反饋。

3.從客戶端發(fā)送文件給服務(wù)端,服務(wù)端保存到本地。并返回“發(fā)送成功”給客戶端。并關(guān)閉相應(yīng)的連接。
代碼實(shí)現(xiàn):客戶端

public class TestTCP{
    @Test
    public void client () {
        Socket socket = null;
        OutputStream outputSteam  = null;
        FileINputStream fileInputStream  = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"),9999);
            fileInputStream  = new FileInputStream(new File("1.jpg"));
            byte[] b =new byte[1024];
            int len;
            outputSream  = socket.getOutputStream();
            while ((len = fileInputStream.read(b)) != -1){
                    outputStream.write(b,0,len);
            }
            socket.shutdownOutput();//停止輸出
            inputStream = socket.getInputStream();
            byte[] b1 = new byte[1024];
            int len1;
            while ((len1 =inputStream.read(b1) ) != -1){
                    String str = new String(b,0,len1);
                    System.out.println(str);
            }
        }catch(Exception e){
              e.printStackTrace();
        }finally{
           if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
} 

代碼實(shí)現(xiàn):服務(wù)端

@Test
public void server(){
 ServerSocket socket = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;
        FileOutputStream fileOutputStream = null;
        Socket s = null;
        try {
            socket = new ServerSocket(9991);
            s = socket.accept();
            fileOutputStream = new FileOutputStream(new File("2.jpg"));
            byte[] b = new byte[1024];
            int len;
            inputStream = s.getInputStream();
            while ((len = inputStream.read(b)) != -1){
               fileOutputStream.write(b,0,len);
            }

            outputStream = s.getOutputStream();
            outputStream.write("我已經(jīng)收到你發(fā)的文件了".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (s != null){
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容