1. 程式人生 > >簡單的通訊(三)----使用Socket實現TCP協議

簡單的通訊(三)----使用Socket實現TCP協議

功能

客戶端向伺服器端傳送一張檔案(這裡以圖片為例),伺服器發反饋訊息給客戶端。

程式碼

package com.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Test;

/**
 * 功能:客戶端向伺服器端傳送一張檔案(這裡以圖片為例),伺服器發反饋訊息給客戶端。
 * 
 * @author Lynn
 *
 */
public class Demo05 {
    // 客戶端;
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        InputStream is = null;
        // 先將檔案讀入;
        FileInputStream fis = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 6868);
            os = socket.getOutputStream();
            is = socket.getInputStream();
            fis = new FileInputStream(new File("1.jpg"));
            int len;
            byte[] content = new byte[156704];
            while ((len = fis.read(content)) != -1) {
                // 再將檔案寫出;
                os.write(content, 0, len);
            }
            // 關閉客戶端的輸出流;
            socket.shutdownOutput();

            int len1;
            byte[] content1 = new byte[20];
            while ((len1 = is.read(content1)) != -1) {
                String str = new String(content1, 0, len1);
                System.out.println(str);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

    }

    // 伺服器端;
    @Test
    public void server() {
        ServerSocket serverSocket =null;
        Socket socket = null;
        InputStream is = null;
        OutputStream os = null;
        FileOutputStream  fos = null;
        try {
            serverSocket = new ServerSocket(6868);
            socket = serverSocket.accept();
            is = socket.getInputStream();
            os = socket.getOutputStream();
            fos = new FileOutputStream("2.jpg");
            int len;
            byte[] content = new byte[156704];
            while((len=is.read(content))!=-1) {
                fos.write(content,0,len);
            }
            
            os.write("成功接收檔案!".getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(os!=null) {
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            if(fos!=null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            if(is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            if(socket!=null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(serverSocket!=null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        
    }

}

注意

1、這裡的路徑是整個工程的路徑,不是當前檔案包的路徑。
2、使用JUit測試的時候需要將方法的修飾符設為public,不能使用預設的,否則會出錯。