公告版位
需要找什麼嗎? 左邊分類或許可以幫助你...

目前分類:[程式語言] Java (91)

瀏覽方式: 標題列表 簡短摘要

Server :

 package javaapplication2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer {

    public static int onlineCount = 0;

    MyServer() {
        new Thread() {

            public void run() {
                while (true) {
                    try {
                        System.err.println("目前線上人數:" + onlineCount);
                        Thread.sleep(1000);
                    } catch (Exception e) {
                    }
                }
            }
        }.start();
    }

    public static void main(String[] args) throws Exception {
        new MyServer();

        ServerSocket ss = new ServerSocket(80);
        System.out.println("伺服器已啟動...");

        while (true) {
            Socket s = ss.accept();
            Thread t = new Thread(new MyThread(s));
            t.start();
        }
    }
}

class MyThread extends Thread {

    Socket s;

    MyThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        MyServer.onlineCount++;
        try {
            System.out.println(s.getRemoteSocketAddress() + " 已連線...");
            BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));
            PrintWriter w = new PrintWriter(new OutputStreamWriter(s.getOutputStream()), true);

            while (true) {
                w.println("你好...我是伺服器,這是伺服器的訊息");
                System.out.println(s.getRemoteSocketAddress() + " 傳遞的訊息是:" + r.readLine());
            }


        } catch (Exception e) {
            System.err.println(s.getRemoteSocketAddress() + " 已離線");
        }
        MyServer.onlineCount--;
    }
}

Client

package javaapplication2;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class Client {

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread() {

                public void run() {
                    try {
                        Socket s = new Socket(InetAddress.getByName("163.15.202.74"), 1024);
                        BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));
                        PrintWriter w = new PrintWriter(new OutputStreamWriter(s.getOutputStream()), true);

                        w.println("你好...我是客戶端");
                        System.out.println("從伺服器傳來的資料:" + r.readLine());

                        while (true) {
                        }
                    } catch (Exception e) {
                    }
                }
            }.start();
        }

    }
}

黃彥霖 發表在 痞客邦 留言(0) 人氣()

ServerSocket ss = new ServerSocket(1024);
System.out.println("伺服器啟動於 : " + ss.getInetAddress().getHostAddress());

 

輸出結果:

伺服器啟動於 : 61.227.71.237:5555


黃彥霖 發表在 痞客邦 留言(0) 人氣()

取得 客戶端 IP:Port

String ipPort = clientSocket.getRemoteSocketAddress().toString().split("/")[1];

取得客戶端 IP

String ip = clientSocket.getInetAddress().toString().split("/")[1];

 

//以上的 clientSocket 你必須先建立 Socket 物件才可以使用。

 

解說:

JAVA 取得IP時都會自動加上斜線,此程式碼使用 split() 來把斜線消除

如1 : /127.0.0.1:8080 消除後會變成 127.0.0.1:8080

黃彥霖 發表在 痞客邦 留言(2) 人氣()

    傳統 HashMap 必須輸入Key才能取得Value,但是常常會遇到沒有 Key 但是卻想要得到 Value ,這時可以考慮使用 HashMap 中的 keySet() 方法,要注意的是這種輸出結果的順序並不是依造放入(put)的順序,所以無法跟 ArrayList 一樣有順序性的,他只是把 HashMap 裡的 put 資料全部取出而已,這部分必須要注意。可以參考以下程式碼並比對輸出結果:

 

 import java.util.HashMap;

public class NewClass {

    public static void main(String[] args) {
        HashMap map = new HashMap();

        map.put("A", "111");
        map.put("B", "222");
        map.put("C", "333");
        map.put("D", "444");
        map.put("E", "555");
        map.put("F", "666");
        map.put("G", "777");
        map.put("H", "888");
        map.put("I", "999");


        for (Object key : map.keySet()) {
            System.out.println(key + " : " + map.get(key));
        }
    }
}


輸出結果如下:

AA11

 

 

 

 


黃彥霖 發表在 痞客邦 留言(1) 人氣()

// 精簡版
 
public class JPA03 {
    public static void main(String[] args) {
        java.util.Scanner s = new java.util.Scanner(System.in);
        int a,b,i;
        while(true){
            System.out.println("Input:");  
            if((i = (a = s.nextInt()))==999) break;            
            b = s.nextInt();            

黃彥霖 發表在 痞客邦 留言(2) 人氣()

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
 
/**
 *
 * @author Athena
 */
public class ThreadTest {
 

黃彥霖 發表在 痞客邦 留言(0) 人氣()

    /**
     * 陣列差集運算。( 回傳值 = A - B )
     */
    public String[] except(String[] A, String[] B) {
        String[] C = new String[A.length - B.length];
        int count=0;
        for (int i = 0; i < A.length; i++) {
            for (int j = 0; j < B.length; j++) {
                if (A[i].equals(B[j])) {
                    A[i] = "";

黃彥霖 發表在 痞客邦 留言(2) 人氣()

new File("H:/A").renameTo(new File("H:/B"));


黃彥霖 發表在 痞客邦 留言(0) 人氣()

for (File f : new File("H:/").listFiles()) {
        System.out.println(f);
}


黃彥霖 發表在 痞客邦 留言(0) 人氣()

    public void deleteAll(File path) {
        if (!path.exists()) {
            return;
        }
        if (path.isFile()) {
            path.delete();
            return;
        }
        File[] files = path.listFiles();
        for (int i = 0; i < files.length; i++) {

黃彥霖 發表在 痞客邦 留言(1) 人氣()

假日空閒時間,不小心蹦出來的軟體...也就是今天...

測試的方式很簡單,只有測程式迴圈執行速度

初始值為零,每執行一次迴圈就加一,0+1+1+1+1+1= JMark出現的數字

以我的電腦為例,AMD 651K 大概可以執行5億多次(單一執行緒),可能會隨JVM版本而有所不同,所以也可以算是評估JVM效能啦XD

此 JMark 檔案可以在 Windows、Linux、MAC OSX、FreeBSD、Unix ...,不過在那之前你要保證你有安裝JVM(以下第二個網址)

黃彥霖 發表在 痞客邦 留言(0) 人氣()

public class Test implements Runnable {
 
    int flag = 0;
    long i = 0;
    long tempStart = 0;
    long tempEnd = 0;
 
    public Test() {
        new Thread(this).start();
        while (true) {

黃彥霖 發表在 痞客邦 留言(0) 人氣()

要被傳送的類別物件 MyClass : -------------------------------------------

import java.io.Serializable;
 
class MyClass implements Serializable {
 
    String s;
    int i;
    double d;
 
    public MyClass(String s, int i, double d) {

黃彥霖 發表在 痞客邦 留言(0) 人氣()

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
 
public class FileTest {
 
    public static void main(String[] args) throws Exception {
        BufferedInputStream in = new BufferedInputStream(new URL("http://tw.yahoo.com/").openStream());

黃彥霖 發表在 痞客邦 留言(0) 人氣()

 類別  說明
 File  存取檔案相關資訊,如新建/刪除 檔案/資料夾、取得最後修改時間,但不能存取檔案內容
 FileInputStream  讀取byte檔案內容
 FileOutputStream  寫入byte檔案內容
 FileReader  讀取純文字檔內容
 FileWriter  寫入純文字檔內容
 Buffered  緩衝讀取byte檔案內容
 BufferedInputStream  緩衝寫入byte檔案內容
 BufferedOutputStream  緩衝讀取純文字檔內容
 BufferedReaderWriter  緩衝寫入純文字檔內容
   
   
   
   
   
   
   
   
   
   

黃彥霖 發表在 痞客邦 留言(0) 人氣()

package test;
 
import java.io.File;
 
public class FileTest {
 
    public static void main(String[] args) throws Exception {
        File f = new File("C:/A");
        if(f.createNewFile()){
            System.out.println("建立檔案成功");

黃彥霖 發表在 痞客邦 留言(0) 人氣()

package test;
 
import java.io.File;
 
public class FileTest {
 
    public static void main(String[] args) throws Exception {
        File f = new File("C:/A");
        if (f.mkdir()) {
            System.out.println("建立成功");

黃彥霖 發表在 痞客邦 留言(0) 人氣()

物件序列化簡單來說就是將物件變成資料流,可以將物件儲存成檔案,可以永久儲存在硬碟中,而且可以在2台電腦之間傳遞物件,是非常實用的功能。
物件反序列化就是將檔案還原成物件,並拿來使用。

重點 : 要被虛列化的物件必須加入Serializable介面,如果該類別也使用到其他類別的物件,那麼其他類別也必須使用Serializable介面,否則執行時期會發生錯誤。

如果序列化的檔案室在A資料夾底下,A資料夾必須先行建立好,否則會一直卡住...程式不會停...。

import
java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;

黃彥霖 發表在 痞客邦 留言(3) 人氣()

// 伺服器端程式碼 ----------------------------------------------------------------------------------------
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
 
public class UDPServer {
 
    public static void main(String[] args) throws Exception {
        byte[] buffer = new byte[65507];
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);

黃彥霖 發表在 痞客邦 留言(0) 人氣()

// 伺服器同一時間只能處理一個客戶端資料,下一個客戶端要處理必須等待第一個客戶端完成才能夠傳送資料

// 伺服器程式碼----------------------------------------------------------------------------------------------------------------------

 
import java.io.DataInputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
 
public class Server {
 

黃彥霖 發表在 痞客邦 留言(1) 人氣()