?? 63.txt
字號:
//
Socket
兩個Java應(yīng)用程序可通過一個雙向的網(wǎng)絡(luò)通信連接實現(xiàn)數(shù)據(jù)交換,這個雙向鏈路的一端稱為一個socket。
socket通常用來實現(xiàn)client-server連接。
java.net包中定義的兩個類Socket和ServerSocket,分別用來實現(xiàn)雙向連接的client和server端
建立連接時所需的尋址信息
遠程計算機的機器名或IP地址
試圖連接的端口號(Port number)
//
網(wǎng)絡(luò)編程的四個基本步驟
創(chuàng)建socket;
打開連接到socket的輸入/輸出流;
按照一定的協(xié)議對socket進行讀/寫操作;
關(guān)閉socket;
//
創(chuàng)建socket
Socket/ServerSocket類的構(gòu)造方法
Socket(InetAddress address, int port);
Socket(InetAddress address, int port, boolean stream);
Socket(String host, int port);
Socket(String host, int port, boolean stream);
ServerSocket(int port);
ServerSocket(int port, int count);
客戶端Socket的建立
try{
Socket socket=new Socket(”127.0.0.1",2000);
}catch(IOException e){
System.out.println("Error:"+e);
}
服務(wù)器端Socket的建立
ServerSocket server=null;
try {
server=new ServerSocket(2000);
}catch(IOException e){
System.out.println("can not listen to :"+e);
}
Socket socket=null;
try {
socket=server.accept();
}catch(IOException e){
System.out.println("Error:"+e);
}
打開輸入/出流
PrintStream os=new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
DataInputStream is=new DataInputStream(socket.getInputStream());
關(guān)閉Socket
os.close();
is.close();
socket.close();
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -