第一部分:作业要求
【作业的目标】
目标1:Socket简单应用。 把教材P238-230中例11-5代码输入到我们的代码编辑器,调试并运行(80分)。
目标2:博文统计程序。 对“liem烧心”同学的博文(网址链接,网页源代码链接)进行统计,得到 博文总数、每篇博文(标题、网址、撰写时间、阅读数、评论数),输出到文本文件,或者是数据库中(需要思考我们的数据表Table,应该包括哪些字段)(90分)。
第二部分:程序代码及运行结果
【例11-1】
import java.net.*;
public class GetLocalHostIp {
public static void main (String args[]){
InetAddress localHost=null;
try{
localHost=InetAddress.getLocalHost();
}catch(UnknownHostException ex){}
System.out.println(localHost);
}
}
【例11-2】
import java.net.*;
import java.io.*;
public class GetURL {
public static void main (String args[]) throws Exception{
URL myURL=new URL("http://www.163.com/");
URL mto=new URL(myURL,"tutorial.intro.html#DOWNLOADING");
System.out.println("protocol="+mto.getProtocol());
System.out.println("host="+mto.getHost());
System.out.println("port="+mto.getHost());
System.out.println("path="+mto.getPath());
System.out.println("filename="+mto.getFile());
System.out.println("ref="+mto.getRef());
System.out.println("query="+mto.getQuery());
System.out.println("UserInfo="+mto.getUserInfo());
System.out.println("Authority="+mto.getAuthority());
}
}
【例11-3】
import java.net.*;
import java.io.*;
public class ReadURL {
public static void main(String args[])throws Exception{
URL objURL=new URL("http://www.baidu.com.cn");
BufferedReader inURL=new BufferedReader(new InputStreamReader(objURL.openStream()));
String htmlLine;
while((htmlLine=inURL.readLine())!=null)
System.out.println(htmlLine);
inURL.close();
}
}
【例11-4】
import java.net.*;
import java.io.*;
import java.util.Date;
class URLCtest {
public static void main (String args[])throws Exception{
int c;
URL urlobj=new URL("http://www.baidu.com/index.html");
URLConnection urlCon=urlobj.openConnection();
System.out.println("Data:"+new Date (urlCon.getDate()));
System.out.println("Content-Type:"+urlCon.getDate());
System.out.println("Expires:"+urlCon.getDate());
System.out.println("Last-Modified:"+new Date (urlCon.getDate()));
int len=urlCon.getContentLength();
System.out.println("Content-Length:"+len);
if(len>0){
System.out.println("===网页内容===");
InputStream input=urlCon.getInputStream();
int i=len;
while(((c=input.read())!=-1)&&(--i>0)){
System.out.print((char)c);
}
input.close();
}else{
System.out.println("No Content Available");
}
}
}
【例11-5】
import java.io.*;
import java.net.*;
import java.applet.*;
public class ChatServer {
public static void main(String[] args) {
try {
ServerSocket server = null;
try {
server = new ServerSocket(4000);
System.out.println("准备好了,退出输入bye");
} catch (Exception e) {
System.out.println("can't listen to: " + e);
}
Socket socket = null;
try {
socket = server.accept();
} catch (Exception e) {
System.out.println("Error." + e);
}
String line;
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client: " + is.readLine());
line = sin.readLine();
while (!line.equals("bye")) {
os.println(line);
os.flush();
System.out.println("Server:" + line);
System.out.println("Client:" + is.readLine());
line = sin.readLine();
}
os.close();
is.close();
socket.close();
server.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
import java.io.*;
import java.net.*;
public class ChatClient {
public static void main (String arfs[]){
try{
Socket socket=new Socket("127.0.0.1",4000);
System.out.println("输入你要说的话,如果要退出输入bye");
BufferedReader sin=new BufferedReader (new InputStreamReader(System.in));
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readline;
readline=sin.readLine();
while(!readline.equals("bye")){
os.println(readline);
os.flush();
System.out.println("Client:"+readline);
System.out.println("Server:"+is.readLine());
readline=sin.readLine();
}
os.close();
is.close();
socket.close();
} catch(Exception e){
System.out.println("Error"+e);
}
}
}
【例11-6】
import java.io.*;
import java.util.*;
import java.net.*;
public class TelnetServer {
final int RECEIVE_PORT = 9090;
public TelnetServer() {
ServerSocket rServer = null;
Socket request = null;
Thread receiveThread = null;
try {
rServer = new ServerSocket(RECEIVE_PORT);
System.out.println("Welcome to the server!");
System.out.println(new Date());
System.out.println("The server is ready!");
System.out.println("Port: " + RECEIVE_PORT);
while (true) {
request = rServer.accept();
receiveThread = new ServerThread(request);
receiveThread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new TelnetServer();
}
}
import java.io.*;
import java.net.*;
public class ServerThread extends Thread {
Socket clientRequest;
BufferedReader input;
PrintWriter output;
public ServerThread(Socket s) {
this.clientRequest = s;
InputStreamReader r;
OutputStreamWriter w;
try {
r = new InputStreamReader(clientRequest.getInputStream());
w = new OutputStreamWriter(clientRequest.getOutputStream());
input = new BufferedReader(r);
output = new PrintWriter(w, true);
} catch (IOException e) {
e.printStackTrace();
}
output.println("Welcome to the server!");
output.println("Now is:" + new java.util.Date() + " " + "Port:" + clientRequest.getLocalPort());
output.println("What can i do for you?");
}
public void run() {
String command = null;
String str = null;
boolean done = false;
while (!done) {
try {
str = input.readLine();
} catch (IOException e) {
e.printStackTrace();
}
command = str.trim().toUpperCase();
if (str == null || command.equals("QUIT"))
done = true;
else if (command.equals("HELP")) {
output.println("query");
output.println("quit");
output.println("help");
} else if (command.startsWith("QUERY")) {
output.println("OK to query something!");
} else if (!command.startsWith("HELP") && !command.startsWith("QUIT") && !command.startsWith("QUERY")) {
output.println("Command not found!Please refer to the HELP!");
}
}
try {
clientRequest.close();
} catch (IOException e) {
e.printStackTrace();
}
command = null;
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class FileClient {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage:java fileClient <hostname>");
return;
}
DatagramSocket udpSocket = new DatagramSocket();
byte buf[] = new byte[256];
InetAddress address = InetAddress.getByName(args[0]);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4000);
udpSocket.send(packet);
packet = new DatagramPacket(buf, buf.length);
udpSocket.receive(packet);
String received = new String(packet.getData());
System.out.println("文件内容: " + received.trim());
udpSocket.close();
}
}
public class FileServer {
public static void main(String[] args) throws Exception {
new FileServerThread().start();
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class FileServerThread extends Thread {
protected DatagramSocket ssocket = null;
protected BufferedReader in = null;
protected boolean moreline = true;
public FileServerThread() throws IOException {
this("fileServerThread");
}
public FileServerThread(String name) throws IOException {
super(name);
ssocket = new DatagramSocket(4000);
try {
File f = new File(".");
String path = f.getAbsolutePath();
path = path.substring(0, path.length()-1) + "test.txt";
in = new BufferedReader(new FileReader(path));
} catch (IOException e) {
System.err.println("Could not open file.Serving time instead");
}
}
public void run() {
while (moreline) {
try {
byte[] buf =new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
ssocket.receive(packet);
String dString = null;
if (in == null) dString = new Date().toString();
else dString = getNextLine();
buf = dString.getBytes();
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buf, buf.length, address, port);
ssocket.send(packet);
} catch (IOException e) {
e.printStackTrace();
moreline = false;
}
}
ssocket.close();
}
protected String getNextLine() {
String returnValue = null;
try {
if ((returnValue = in.readLine()) == null) {
in.close();
moreline = false;
returnValue = "No more line.Goodbye.";
}
} catch (IOException e) {
returnValue = "IOException occurred in server";
}
return returnValue;
}
}
第四部分:心得体会
第三次作业已经完成了,渐渐的发现我这两次的作业好像都只是停留在第一个目标那里,不知道是不是自己害怕做不好,还是不敢去尝试,因为不敢尝试,所以现在还是停留在这个状况,一直进步不了。我这几天去看了看同学们的作业,发现他们做得好认真,他们都在很认真的做作业,有好多的同学都会选择目标2去完成它,这个瞬间我会觉得自己好像真的好像很失败,我都没有尝试去试一下,就放弃了。因为第二次作业的目标2我做了很久都没成功,所以我就直接放弃这一次的目标2了,我觉得其实这都只是我自己的借口而已。不要再去逃避任何你应该要面对的事情了,像老师说的,遇到困难就要尝试用各种办法去克服它,我也可以做到的,加油!!