第三次作业:网络编程

第一部分:作业要求

 【作业的目标】

目标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了,我觉得其实这都只是我自己的借口而已。不要再去逃避任何你应该要面对的事情了,像老师说的,遇到困难就要尝试用各种办法去克服它,我也可以做到的,加油!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在风能领域,准确预测风速对于风电场的运行与管理至关重要。Matlab作为一个强大的数学计算和数据分析平台,被广泛应用于风速预测模型的构建。本文将深入探讨基于四种风速——随机风、基本风、阵风和渐变风的组合风速预测技术。 我们来理解这四种风速类型: 1. **随机风**:随机风是指风速呈现出随机性的变化,通常由大气湍流引起。在建模中,通常通过统计方法如高斯分布或Weibull分布来模拟这种不确定性。 2. **基本风**:基本风速是指在无特定扰动条件下的平均风速,它是长期观测结果的平均值,通常用于结构设计和风能评估。 3. **阵风**:阵风是短时间内风速显著增强的现象,对建筑物和风力发电机造成的主要威胁之一。阵风的预测涉及到风的脉动特性分析。 4. **渐变风**:渐变风是指风速随时间和空间逐渐变化的过程,常见于风向转变或地形影响下的风场变化。 在Matlab中,利用这四种风速类型进行组合预测,可以提高预测的准确性。预测模型可能包括以下几个步骤: 1. **数据收集与预处理**:收集历史风速数据,包括随机风、基本风、阵风和渐变风的数据,进行异常值检测、缺失值填充以及数据标准化。 2. **特征工程**:提取风速变化的相关特征,如平均值、标准差、极值、频率分布等,这些特征可能对预测有重要影响。 3. **模型选择**:可以选择多种预测模型,如时间序列分析(ARIMA、状态空间模型等)、机器学习算法(线性回归、决策树、支持向量机、神经网络等)或深度学习模型(LSTM、GRU等)。 4. **模型训练**:利用历史数据训练选定的模型,调整模型参数以优化性能,例如通过交叉验证来避免过拟合。 5. **模型验证与评估**:使用独立的测试集验证模型预测效果,常见的评估指标有均方误差(MSE)、平均绝对误差(MAE)和决定系数(R²)。 6. **组合预测**:结合四种风速的不同模型预测结果,可以采用加权平均、集成学习(如bagging、boosting)等方式,以提升整体预测精度。 7. **实时更新与动态调整**:实际应用中,模型需要不断接收新的风速数据并进行在线更新,以适应风场环境的变化。 通过以上步骤,可以构建一个综合考虑各种风速特性的预测系统,这对于风电场的功率输出预测、风电设备的维护计划以及电网调度都具有重要价值。然而,需要注意的是,每个风场的地理环境、气候条件和设备状况都有所不同,因此模型的建立应根据实际情况进行定制和优
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值