编程知识 cdmana.com

Java --- network programming (4) - C / S-B / S


C/S And B/S

* C/S ( Client/Server ) Characteristics of client and server

1、 Both client and server software need to be written by programmers .

2、 The maintenance of the client is troublesome .( defects )

3、 The existence of the client can separate some operations from the client to run , Reduce the pressure on the server side .( advantage )

* B/S ( Browse/Server ) Features of browser and server

1、 The client does not need to be written by programmers , Directly use the browser software in the system as the client . Programmers only need to write server-side OK 了 .

2、 It's also easy to maintain , Because as long as you maintain the server .( advantage )

3、 All operations are on the server side , Relatively high pressure .( defects )

Java--- Network programming (4)-C/S-B/S_ browser

Programming practice 1

1、 Customize a server , Receive messages from the browser . Show what information the browser sent , And send simple web information to the browser .

Only a single thread is implemented ~~~ Multithreading I wrote ~ Just for unknown reasons ~ There will be Socket Abnormal shutdown ~! So it didn't work ~

Single thread : success ~

      
      
package cn.hncu.bs;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
* @author Chen Haoxiang
*
* @version 1.0 2016-5-11
*/
public class MyServer {
public static void main(String[] args) {
try {
while (true) {
ServerSocket server = new ServerSocket(80);
Socket s = server.accept();
System.out.println(s.getInetAddress().getHostAddress()
+ "... Visit ");
InputStream in = s.getInputStream();
byte buf[] = new byte[1024];
int len = in.read(buf);
String text = new String(buf, 0, len);
System.out.println(text);

BufferedReader bin = new BufferedReader(new FileReader(
"chx.html"));
// To add true Refresh PrintWriter flow
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
String line = null;
while ((line = bin.readLine()) != null) {
pw.println(line);
}
in.close();
pw.close();
s.close();
server.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.

Multithreading : Examples of failure :

solve :

      
      
package cn.hncu.bs;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
* @author Chen Haoxiang
*
* 2016-5-10
*/
public class MyServer {

public static void main(String[] args) {
try {
while(true){
ServerSocket server = new ServerSocket(80);
// Multiple customers visit at the same time
Socket s = server.accept();
new Thread(new ServerThread(s)).start();

}
} catch (IOException e) {
e.printStackTrace();
}
}
}

class ServerThread implements Runnable{
Socket s = null;
public ServerThread(Socket s) {
this.s = s;
}
@Override
public void run() {
try {
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"... Came to visit. ~");
InputStream in = s.getInputStream();
byte buf[] = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println(text);

BufferedReader bin = new BufferedReader(new FileReader("chx.html"));

// To add true Refresh PrintWriter flow
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
String line=null;

while((line=bin.readLine())!=null){
System.out.println(line);
pw.println(line);
}
in.close();
pw.close();
s.close();

} catch (IOException e) {
e.printStackTrace();
}


}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.

My teacher explained that :

      
      
In practice MyServer When using multithreading, there are multiple requests and responses , This is probably TCP Three handshakes , That is, the client browser communicates with the server several times , And the server in the second time ( The... In the same request 2 The second handshake ) When you receive a message, you use another thread to pick up , So the handshake can't be realized , The server outputs and receives messages many times
  • 1.

Programming practice 2

2、 Simulate a browser client to send a request to the server , Receive and display the response message .

      
      
package cn.hncu.bs;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
* @author Chen Haoxiang
*
* @version 1.0 2016-5-11
*/
public class MyBrowse {

public static void main(String[] args) {
String ipAdd = "www.hncu.net";
try {
Socket s = new Socket(ipAdd, 80);
// Send to server http Protocol request header , To let the server know us , Give us the right response
OutputStream out = s.getOutputStream();
System.out.println(s.getPort());// port
PrintWriter pw = new PrintWriter(out,true);

pw.println("GET / HTTP/1.1");// The request line contains : Request mode (GET POST) Space The requested resource path Space http Agreement version of
pw.println("Accept: */*;");
pw.println("Host: "+ipAdd);// Below these key-value It's the request header
//out.println("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
//out.println("Accept-Encoding: gzip, deflate");
pw.println("Connection: close");// If there is no such sentence , The program won't end by itself
pw.println();// Blank line ---http After the end of the protocol request header , There must be an empty line
System.out.println(" End of request !");

// Receive response from server
InputStream in = s.getInputStream();
int len=0;
byte b[] = new byte[1024];
while((len=in.read(b))!=-1){
String str = new String(b,0,len,"UTF-8");
System.out.println(str);
}
s.close();

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.

The response message is :

      
      
HTTP/1.1 200 OK
Date: Wed, 11 May 2016 10:56:05 GMT
Server: VWebServer
X-Frame-Options: SAMEORIGIN
Last-Modified: Wed, 11 May 2016 06:04:55 GMT
ETag: "6060-5328ad214afc0"
Accept-Ranges: bytes
Content-Length: 24672
Vary: User-Agent,Accept-Encoding
Cache-Control: private, max-age=600
Expires: Wed, 11 May 2016 11:06:05 GMT
Connection: close
Content-Type: text/html
Content-Language: zh-CN

// The back is the website html Code. ~~~
?<!DOCTYPE html><html><head><title> Hunan City College </title>
...
...
...
...

</body></html>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

Programming practice 3:

3、 Web spider , Collect email address information in web pages .

      
      
package cn.hncu.bs;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;

/**
* @author Chen Haoxiang
*
* @version 1.0 2016-5-11
*/
public class SpiderDemo {

// Read the mailbox of the web page
public static void main(String[] args) {
try {
URL url = new URL("http://www.sina.com.cn/");
System.out.println(url.getHost());
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String regex = "\\[email protected]\\w+(\\.\\w+)+";

Pattern p = Pattern.compile(regex);

String line = null;

while((line=br.readLine())!=null){

Matcher m = p.matcher(line);
while(m.find()){
System.out.println(m.group());
}
}

System.out.println(" Finished reading !");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* First test the local email address to see if it can be read --- success
*/
@Test
public void Test() {
try {
BufferedReader br = new BufferedReader(new FileReader("mail.html"));

// Regular expressions
String regex= "\\[email protected]\\w+(\\.\\w+)+";

Pattern p = Pattern.compile(regex);
String line=null;
while((line=br.readLine())!=null){
Matcher m = p.matcher(line);
while(m.find()){
System.out.println(m.group());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.



版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://cdmana.com/2022/134/202205141204236766.html

Scroll to Top