(C/S program: the indicated client/server program, two sets of programs are written each time
-MSN, QQ: There are usually two sets of programs, one is the server side and the other is the client side
B/S program: indicated browser/server, can be understood as dynamic WEB, forum
Program this season
redking.blog.51cto/attachment/200903/5/27212_1236255657HPox.png
Sock: TCP communication
Datagram: UDP communication
Classes required by the Socket program:
1. All users are a Socket client for the server.
2. Use the ServerSocket class on the server to receive the client's Socket
Socket communication model
redking.blog.51cto/attachment/200903/5/27212_1236255662getr.png
Four basic processes of Socket programming
redking.blog.51cto/attachment/200903/5/27212_1236255664kZYR.png
Write a server program: ServerSocket
redking.blog.51cto/attachment/200903/5/27212_12362556724yft.png
As long as the network connection requests a port
publicServerSocket(intport)
publicSocketaccept() throwsIOException
This method waits for the client's socket to access.
ServerSocket01 server-side code:
redking.blog.51cto/attachment/200903/5/27212_1236255681FmiW.png
This server-side function is very simple, that is, to receive the client's request, and then output it on the screen~
redking.blog.51cto/attachment/200903/5/27212_1236255683DzcJ.png
There is a telnet command in Windows, through which you can directly connect to the server.
redking.blog.51cto/attachment/200903/5/27212_1236255688dGxc.png
The client receives the redking.blog.51cto string, and the server displays "End of client answer~~~"
redking.blog.51cto/attachment/200903/5/27212_12362556918za9.png
As long as it conforms to the protocol standard of the network, all clients can connect to this server.
But under normal circumstances, it is rarely said to use the telnet connection directly, and a client is often written.
Socket
publicSocket(StringHOST,intport)throwsUnknownHOSTException,IOException
Specify a host's IP address and a port
ClientSocket01 client code:
redking.blog.51cto/attachment/200903/5/27212_1236255697YBa2.png
Verify the effect, the same as telnet~
redking.blog.51cto/attachment/200903/5/27212_1236255700tGRN.png
The above code verifies that the program needs to write two sets of programs:
One is the server side
·The other one is the client
Now that all the code is found to be executed only once, can it be said that it is executed multiple times? My users can issue the order to stop by themselves.
Such behavior we can make a Socket classic - Echo program
What content the user sends, the server will answer what content:
EchoServer code:
importjava.io.
;
importjava.
;
publicclassEchoServer{
//In order to manage try...catch throws an exception directly
publicstaticvoidmain(Stringargs)throwsException{
ServerSocketserver=null;
//The output must use the print stream
PrintStreamout=null;
//The server must also receive input
BufferedReaderbuf=null;
//1. Instantiate the listener of a server
server=newServerSocket(9999);
//You can use an infinite loop method to receive content
Socketclient=null;
while(true){
// keep receiving content
client=server.accept();
//Ready to output content to the client
out=newPrintStream(client.getOutputStream());
//And the client has input to the server
buf=newBufferedReader(newInputStreamReader(client.getInputStream()));
//The following first gives a complete information prompt
out.println(Hello! Welcome: redking.blog.51cto);
out.println(enter bye to indicate exit~);
//A user has to send a lot of information
while(true){
//Receive the content sent by the client
Stringstr=buf.readLine();
if(str==null){
//If str is empty, it means exit
break;
}else{
//If the input is bye, it means that the system exits
if(bye.equals(str)){
break;
}
// can answer the information sent by the user
out.println(ECHO: str);
}
}
// do the finishing touches
out.close();
buf.close();
client.close();
//If you want to close the server, you can set the symbol
//server.close();
}
}
}
Take a look at the effect:
redking.blog.51cto/attachment/200903/5/27212_12362557017PQk.png
Now we use telnet to connect to see the effect
redking.blog.51cto/attachment/200903/5/27212_1236255704K06q.png
Enter bye to exit the connection
redking.blog.51cto/attachment/200903/5/27212_1236255722ITF0.png
Can be connected multiple times together
redking.blog.51cto/attachment/200903/5/27212_1236255726D6kt.png
Typically includes input and output
EchoClient code:
importjava.io.
;
importjava.
;
publicclassEchoClient{
publicstaticvoidmain(Stringargs)throwsException{
Socketclient=null;
BufferedReaderbuf=null;
PrintStreamout=null;
//1. Connect to the server
client=newSocket(localHOST,9999);
//Receive input from the server
buf=newBufferedReader(newInputStreamReader(client.getInputStream()));
System.out.println(buf.readLine());
System.out.println(buf.readLine());
// prepare to receive data from the keyboard later
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));
StringuserInput=null;
out=newPrintStr
[Zero Basic Learning JAVA] JavaSE Use Part-32.Java Network Programming.doc)