code example Download: Http://pan.baidu.com/s/1jGuSdyY
Let's start with a simple example:
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
public class Echoplayer {public
string Echo (String msg) {return
"echo:" + msg;
}
public void Talk () throws IOException {
BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
string msg = null;
while (msg = Br.readline ())!= null) {
System.out.println (Echo (msg));
if (Msg.equals ("Bye")) break
;
}
public static void Main (String arg[]) throws ioexception{
new Echoplayer (). talk ();
}
The following code is a server example: the client message to the server, the server received a message to the client after the same message.
Echoserver
Import Java.awt.Stroke;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.PrintWriter;
Import Java.net.ServerSocket;
Import Java.net.Socket;
public class Echoserver {private int port = 8000;
Private ServerSocket ServerSocket;
Public Echoserver () throws IOException {ServerSocket = new ServerSocket (port);
SYSTEM.OUT.PRINTLN ("Server Start");
public string Echo (String msg) {return "echo:" + msg; Private PrintWriter getwriter (socket socket) throws IOException {OutputStream socketout = Socket.getoutputstream (
);
return new PrintWriter (Socketout, true); Private BufferedReader Getreader (socket socket) throws IOException {InputStream socketin = Socket.getinputstream ()
;
return new BufferedReader (new InputStreamReader (Socketin));
The public void service () {while (true) {socket socket = NULL; try {socket = ServerSocket. Accept ();
SYSTEM.OUT.PRINTLN ("New connection accepted" + socket.getinetaddress () + ":" + socket.getport ());
BufferedReader br = getreader (socket);
PrintWriter pw = getwriter (socket);
String msg = NULL;
while (msg = Br.readline ())!= null) {System.out.println (msg);
Pw.println (Echo (msg));
if (Msg.equals ("Bye")) {break;
(IOException e) {//Todo:handle exception e.printstacktrace ();
}finally {try {if (socket!= null) {socket.close ();
} catch (IOException E2) {//Todo:handle exception e2.printstacktrace ();
public static void Main (String args[]) throws IOException {new Echoserver (). Service ();
}
}
In the service () method of the Echoserver program, whenever the Serversocket.accept () method returns a socket object, the link to a customer is established, which contains the customer's address and port information. You can obtain this information separately only by calling the Socket.getinetaddress () and Socket.getport () methods of the socket object.
Related basic knowledge:
1, the Java Network program is located in the TCP/IP reference Model of the network interconnect layer
2, TCP protocol and IP protocol are located at the transport layer.
3, assuming that a process has occupied TCP 80 port, it can also occupy UDP 80 port. No
4, a customer process to execute the following code;
Socket SOCKET1 = new socket (host,port);
Socket Socket2 = new socket (host,port);
Here's the correct statement:
A, Socket1 and Socket2 occupy different local ports
B, when the socket construction method returns successfully, it indicates that a TCP link is established with the server
5, there is a protocol that if the client sends a line of "date", the server side will return the current date information: If the client sends a line of "exit", the server end communication with the client. Which layer of agreement should this agreement be with.
Answer: Application layer
6. The HTTP protocol stipulates the TCP port number that the HTTP server occupies by default. 80
7. In the client/server communication mode, what is the main task of client program and server program?
A, the client sends the request and receives the response from the server
B, the server program receives and processes the customer request, and then sends the response results to the customer
8. Where to find specific documents describing the TCP/IP protocol. The official website of the RFC
9. A server process executes the following code:
ServerSocket serversocket = new ServerSocket (80);
Socket socket = serversocket.accept ();
int port = Socket.getpot ();
A, the server process occupies 80 ports
B, when the Serversocket.accept () method returns successfully, it indicates that the server process has received a client connection request
The C, Socket.getport () method returns the local port occupied by the client socket