首先 我們說說 網絡 吧,網絡互聯(lián)設備協(xié)議(OIS)參考模型分成了七層,即應用層,表示層,會話層,傳輸層,網絡層,數(shù)據(jù)鏈路層,物理層。。。但是在Java中只注重與應用層..網絡層..與傳輸層,在其他方面不能夠更好的服務與程序的操作和控制。然而在java中式有服務器(Server)和客服(Cliect)組成的,這兩者之間一般情況 是成對出現(xiàn)的。。。對服務器而言如果客服對服務器發(fā)送請求,,則服務器是InputStream數(shù)據(jù),二客服則是OutputStream數(shù)據(jù)。。。兩者之間也是相對的。。。。
我做了個小例子如下:
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server03 {
public static void main(String args[])throws IOException{
ServerSocket ss=new ServerSocket(9999);
while(true){
System.out.println("----server端");
Socket s=ss.accept();
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
dos.writeUTF("客服端"+s.getInetAddress().getHostAddress()+""+s.getPort()+"已連接"+
s.getInetAddress().getHostAddress()+s.getLocalPort());
dos.flush();
dos.close();
s.close();
}
}
}
執(zhí)行結果:
客服端的程序:
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
public class Cliecnt02 {
public static void main(String args[])throws IOException{
for(int i=0;i<20000;i++){
Socket s=new Socket("127.0.0.1",9999);
DataInputStream dis=new DataInputStream(s.getInputStream());
System.out.println("客服端連接的數(shù)據(jù)"+dis.readUTF());
dis.close();
s.close();
}
}
}
執(zhí)行結果:
小結:一般情況下一臺服務器上可以為很多的電腦提供服務。。。。只要改變端口號就可以 了
posted on 2010-11-10 22:28
龍ぜ殘劍 閱讀(877)
評論(0) 編輯 收藏