Java編程實現多線程TCP服務器完整實例

 

Socketjava

·功能:TCP客戶端套接字服務器

·構造方法:
   Socket(InetAddress address, int port)
   建立一個流套接字並將其鏈接到指定 IP 地址的指定端口號
·經常使用方法:
   1.getInetAddress
   得到InetAddress的相關信息
   2.getInputStream
   得到此TCP鏈接的輸入流
   3.getOutPutStream
   得到此TCP鏈接的輸出流
ServerSocket
·功能: TCP服務端套接字
·構造方法:
   ServerSocket(int port)
   建立綁定到特定端口的服務器套接字。
·經常使用方法:
   1.accept
   得到TCP鏈接的客戶端的socket
   2.isClosed
   得到ServerSocket的關閉狀態

TCP服務器端多線程

TcpServer.javasocket

服務器端採用多線程的方式,每創建一個鏈接就啓動一個java線程,發送圖片給客戶端,以後關閉此TCP鏈接tcp

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer extends Thread{
    Socket clientSocket;
    public TcpServer(Socket clientSocket) {
        super();
        this.clientSocket = clientSocket;
    }
    @Override
      public void run() {
        try {
            //得到客戶端的ip地址和主機名
            String clientAddress = clientSocket.getInetAddress().getHostAddress();
            String clientHostName = clientSocket.getInetAddress().getHostName();
            System.out.println(clientHostName + "(" + clientAddress + ")" + " 鏈接成功!");
            System.out.println("Now, 傳輸圖片數據...........");
            long startTime = System.currentTimeMillis();
            //獲取客戶端的OutputStream
            OutputStream out = clientSocket.getOutputStream();
            //傳出圖片數據
            FileInputStream in = new FileInputStream(new File("/home/gavinzhou/test.jpg"));
            byte[] data = new byte[4096];
            int length = 0;
            while((length = in.read(data)) != -1){
                out.write(data, 0, length);
                //寫出數據
            }
            long endTime = System.currentTimeMillis();
            //提示信息
            System.out.println(clientHostName + "(" + clientAddress + ")" + " 圖片傳輸成功," + "用時:" + ((endTime-startTime)) + "ms");
            //關閉資源
            in.close();
            clientSocket.close();
            System.out.println("鏈接關閉!");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        //創建TCP鏈接服務,綁定端口
        ServerSocket tcpServer = new ServerSocket(9090);
        //接受鏈接,傳圖片給鏈接的客戶端,每一個TCP鏈接都是一個java線程
        while(true){
            Socket clientSocket = tcpServer.accept();
            new TcpServer(clientSocket).start();
        }
    }
}

  

TCP客戶端ide

TcpClientthis

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClient {
    public static void main(String[] args) throws IOException {
        // 創建TCP服務
        // 鏈接本機的TCP服務器
        Socket socket = new Socket(InetAddress.getLocalHost(), 9090);
        // 得到輸入流
        InputStream inputStream = socket.getInputStream();
        // 寫入數據
        FileOutputStream out = new FileOutputStream(new File("../save.jpg"));
        byte[] data = new byte[4096];
        int length = 0;
        while((length = inputStream.read(data)) != -1){
            out.write(data, 0, length);
        }
        //關閉資源
        out.close();
        socket.close();
    }
}

  

結果spa

首先,命令行啓動服務器端,以後啓動客戶端,結果以下:.net

圖片比較小,速度很快!命令行

相關文章
相關標籤/搜索