1.Socket是代表兩臺機器之間網(wǎng)絡(luò)連接的對象(java.net.Socket)。
Socket的建立如下,參數(shù)分別是服務(wù)器端的IP地址和端口號: Socket socket = new Socket(“167.5.75.1”,5000);
2. 客戶端(Client)Socket的使用
2.1從Socket讀出數(shù)據(jù)步驟:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// 1.創(chuàng)建Socket連接,告知Server的IP地址以及端口號 Socket socket =newSocket(“127.0.0.1”,4242); // 2.創(chuàng)建InputStreamReader,用于讀取socket輸入流 InputStreamReader stream =newInputStreamReader(socket.getInputStream()); // 3.使用BufferedReader鏈接輸入流 BufferedReader br =newBufferedReader(stream);
// 4.讀出數(shù)據(jù) String line =null; while((line = br.readLine()) !=null) { System.out.println(“Today’s advice is: “+ line); }
// 5. 關(guān)閉輸入流BufferedReader br.close(); |
2.2向Scoket寫入數(shù)據(jù)步驟:
?
1 2 3 4 5 6 7 8 9 |
// 1.創(chuàng)建Socket連接,告知Server的IP地址以及端口號 Socket socket =newSocket(“127.0.0.1”,4242); // 2.創(chuàng)建PrintWriter對象,用以接收socket輸出流 PrintWriter writer =newPrintWriter(socket.getOutputStream()); // 3.使用PrintWriter對象寫出輸出數(shù)據(jù) String advice =”Today’s advice”; writer.println(advice); // 4. 關(guān)閉連接 writer.close(); |
3. 服務(wù)器端(Server)Socket的使用
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// 1.創(chuàng)建一個SercerSocket,使用4242端口監(jiān)聽客戶端請求 ServerSocket serverSocket =newServerSocket(4242); System.out.println(“The server is started, listening on port 4242”);
while(true) { // 2.ServerSocket的accept()在等待用戶連接的時候閑置;在用戶連接上來的時候,返回一個Socket來與客戶端通信 Socket socket = serverSocket.accept(); // 3.創(chuàng)建PrintWriter對象,用以接收socket輸出流 PrintWriter writer =newPrintWriter(socket.getOutputStream()); // 4.使用PrintWriter對象寫出輸出數(shù)據(jù) String advice =”notifier’s blog”; writer.println(advice);
// 5. 關(guān)閉連接 writer.close(); } |
4. 線程的狀態(tài)
線程總共有5種狀態(tài):
1.新建 (Thread t = new Thread())
2.就緒 (t.start())
3.運行
4.堵塞
線程被block的原因很多,比如:等待IO操作, sleep(),等待被占用對象釋放
5.死亡
5. 解決線程同步化問題的方法是: 對使用到共享對象的方法使用synchronized
需要注意的是:
雖說是方法進行了synchronized,但鎖不是加在方法上的而是對象上的,也就是說,是synchronized方法獲取對象鎖。如果對象(類)有兩個或者多個synchronized方法,就表示兩個線程不能同時進入同一個方法,也不能同時進入不同的方法。 因為同一時間,只有一個方法在占有對象鎖。
6. synchronized代碼塊
有時候在一個方法中做了很多事情,但只有一部分邏輯是需要synchronized的,這時候我們可以使用synchronized代碼塊。如下,其中this表示當前對象:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
publicvoidfunction() { doSomething();
//以下方法需要同步化 synchronized(this) { doCriticalStuff(); moreCriticalStuff(); }
doSomeOtherThing(); } |
7. 以下是一個Socket簡單的例子:
客戶端代碼及詳細注釋:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
/** * @author notifier * @create 2010-9-25 上午10:12:10 * @version 1.0 */ publicclassDailyAdviceClient { publicstaticvoidmain(String[] args) { DailyAdviceClient client =newDailyAdviceClient(); client.receiveMsg(); }
publicvoidreceiveMsg() { try { // 1.創(chuàng)建Socket連接,告知Server的IP地址以及端口號 Socket socket =newSocket(“127.0.0.1”,4242); // 2.創(chuàng)建InputStreamReader,用于讀取socket輸入流 InputStreamReader stream =newInputStreamReader(socket .getInputStream()); // 3.使用BufferedReader鏈接輸入流 BufferedReader br =newBufferedReader(stream);
// 4.讀出數(shù)據(jù) String line =null; while((line = br.readLine()) !=null) { System.out.println(“Today’s advice is: “+ line); }
// 5. 關(guān)閉輸入流BufferedReader br.close(); }catch(UnknownHostException e) { e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } } } |
服務(wù)器端代碼及詳細注釋:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
/** * @author notifier * @create 2010-9-25 下午07:06:54 * @version 1.0 */ publicclassSimpleChatServer { // 保存客戶端列表 privateArrayList clientList =newArrayList();;
publicstaticvoidmain(String[] args) { newSimpleChatServer().startUp(); }
/** * 負責(zé)服務(wù)器端的啟動 * */ publicvoidstartUp() { try { // 創(chuàng)建服務(wù)器端ServerSocket連接,監(jiān)聽端口號5000 ServerSocket serverSocket =newServerSocket(5000);
// 輪詢等待客戶端請求 while(true) { // 等待客戶端請求,無請求則閑置;有請求到來時,返回一個對該請求的socket連接 Socket clientSocket = serverSocket.accept();
// 將該客戶端加入到列表中 PrintWriter writer =newPrintWriter(clientSocket.getOutputStream()); clientList.add(writer);
// 創(chuàng)建ClientHandler對象,通過socket連接通信 Thread t =newThread(newClientHandler(clientSocket)); t.start();
System.out.println(“有Client連進來“); } }catch(Exception e) { e.printStackTrace(); } }
/** * 客戶端處理類, 主要負責(zé): * 1.接收客戶端發(fā)來的消息 * 2.將消息轉(zhuǎn)發(fā)其他客戶端 * @author sdniu * @create 2010-9-26 上午10:00:18 * @version 1.0 */ publicclassClientHandlerimplementsRunnable { privateBufferedReader reader; privateSocket socket;
/** * ClientHandler的構(gòu)造函數(shù) * @param clientSocket */ publicClientHandler(Socket clientSocket) {
try { // 得到socket連接 socket = clientSocket;
// 得到客戶端發(fā)來的消息 InputStreamReader isReader =newInputStreamReader(socket.getInputStream()); reader =newBufferedReader(isReader); }catch(IOException e) { e.printStackTrace(); } }
publicvoidrun() { String message; try { while((message = reader.readLine()) !=null) { System.out.println(“客戶端消息: “+ message); // 將客戶端發(fā)來的消息轉(zhuǎn)發(fā)所有客戶端 notifyAllClients(message); } }catch(IOException e) { e.printStackTrace(); } } }
/** * * @param message */ publicvoidnotifyAllClients(String message) { // 得到客戶端列表的迭代器,語法格式為 Iterator it = clientList.iterator(); Iterator it = clientList.iterator();
while(it.hasNext()) { try { // 得到的Iterator別忘了強制轉(zhuǎn)換回PrintWriter PrintWriter writer = (PrintWriter) it.next(); writer.println(message); writer.flush(); }catch(Exception e) { e.printStackTrace(); } } } } |