listen方法用來監(jiān)聽socket 。 int aByte; while ((aByte = in.read()) 〉= 0) { system.out.PRintln((char)aByte); }
Socket 客戶
現(xiàn)在我們來看看客戶端。見 表 G ??蛻舳说墓ぷ骱头掌鞫撕芟嗨疲皇欠催^來了。首先,我們創(chuàng)立一個套接字連接到服務器。使用KeyGen 找到要害字,創(chuàng)立一個安全套接字(SecretSocket)。然后我們利用它的OutputStream給服務器發(fā)送數(shù)據(jù): Key key = KeyGen.getSecretKey(); Socket s = new Socket("localhost", 4444); SecretSocket ss = new SecretSocket(s, key); OutputStream os = ss.getOutputStream(); os.write("Hello World!".getBytes()); os.flush(); os.close(); s.close();