Creating a Web server with the TCP communication model
??
The main use of ServerSocket has been listening, here to put it in the while loop, in the loop to open a single thread
??
public class MyWebServer {
public static void Main (string[] args) throws IOException {
ServerSocket ss=new ServerSocket (80);
Socket Socket=null;
while ((Socket=ss.accept ())!=null) {
New Httpthread (socket). Start ();
}
Ss.close ();
}
}
??
Write what you want to do in the run method in the thread, and in the constructor, pass the Socket object in.
??
public class Httpthread extends Thread {
private socket socket;
Httpthread (socket socket) {
This.socket=socket;
}
public void Run () {
OutputStream Outputstream=socket.getoutputstream ();
PrintWriter pwriter=new PrintWriter (outputstream);
Pwriter.println ("
Pwriter.println ("<body>");
Pwriter.println ("hello! This is my page ");
Pwriter.println ("</body>");
Pwriter.println ("
Pwriter.flush ();
Pwriter.close ();
Socket.close ();
}
}
??
Create an instant chat software with UDP communication model
??
The main is to write a receive thread and a send thread, and then start the two threads in the main function OK
??
Class Sendthread extends Thread {
Private Datagramsocket Datagramsocket;
private int sendport;
Sendthread (int port, int sendport) {
Super ();
This.sendport = SendPort;
This.datagramsocket = new Datagramsocket (port);
}
public void Run () {
BufferedReader breader = new BufferedReader (new InputStreamReader (system.in));
string string = null;
while ((string = Breader.readline ()) = null) {
Datagrampacket DP = new Datagrampacket (String.getbytes (), 0,string.length (), Inetaddress.getbyname ("localhost"), SendPort);
Datagramsocket.send (DP);
System.out.println ("Send Message:" + string);
}
}
}
Class Receivethread extends Thread {
Private Datagramsocket Datagramsocket;
Receivethread (int port) {
Super ();
This.datagramsocket = new Datagramsocket (port);
}
public void Run () {
byte[] buff = new byte[1024];
Datagrampacket DP = new Datagrampacket (buff, 1024);
while (true) {
Datagramsocket.receive (DP);
String string = new String (Dp.getdata (), 0, Dp.getlength ());
System.out.println ("Receive Message:" + string);
}
}
}
public static void Main (string[] args) throws SocketException {
New Receivethread (Integer.valueof (Args[0])). Start ();
New Sendthread (Integer.valueof (args[1]), integer.valueof (Args[2]). Start ();
}
??
Accessing the Web site in Java
??
First, a URL object is created from a path string as a parameter, and the openconnection method of the URL object is called to get a httpurlconnection References to Objects
??
Then call httpurlconnection 's Connect method to establish a connection, and after the connection is established, you can call httpurlconnection 's The Getheaderfields method gets a map object that stores the key-value pairs in the Header .
??
In addition to returning the Header , the server returns the response content, reads the response with a BufferedReader , and, of course, uses InputStreamReader as the parameter, and the input source of the InputStreamReader is the getinputstream method that calls HttpURLConnection
??
public static void Main (string[] args) throws IOException {
String Path = "http://www.baidu.com";
URL url = new URL (path);
HttpURLConnection httpurlconnection = (httpurlconnection) url.openconnection ();
Httpurlconnection.connect ();
map<string, list<string>> headermap = Httpurlconnection.getheaderfields ();
For (String Key:headerMap.keySet ()) {
SYSTEM.OUT.PRINTLN (key + ":" + headermap.get (key));
}
BufferedReader br = new BufferedReader (New InputStreamReader (
Httpurlconnection.getinputstream (), "Utf-8"));
String String=null;
while ((String=br.readline ())!=null) {
System.out.println (string);
}
Httpurlconnection.disconnect ();
}
Fundamentals: Java Network programming