04747_Java语言程序设计(一)_第10章_网络与数据库编程基础
例10.1说明InetAddress类的用法的应用程序。
public class Example10_1 {
public static void main(String args[]) {
try {// 以下代码通过域名建立InetAddress对象:
InetAddress addr = InetAddress.getByName("www.fudan.edu.cn");
String domainName = addr.getHostName();// 获得主机名
String IPName = addr.getHostAddress();// 获得IP地址
System.out.println(domainName);
System.out.println(IPName);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
例10.2以数据流方式读取网页内容的应用程序。
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*; public class Example10_2 {
public static void main(String args[]) {
new DownNetFile();
}
} class DownNetFile extends JFrame implements ActionListener {
JTextField inField = new JTextField(30);
JTextArea showArea = new JTextArea();
JButton b = new JButton("下载");
JPanel p = new JPanel(); DownNetFile() {
super("读取网络文本文件示意程序");
Container con = this.getContentPane();
p.add(inField);
p.add(p);
JScrollPane jsp = new JScrollPane(showArea);
b.addActionListener(this);
con.add(p, "North");
con.add(jsp, "Center");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400);
setVisible(true);
} public void actionPerformed(ActionEvent e) {
readByURL(inField.getText());
} public void readByURL(String urlName) {
try {
URL url = new URL(urlName);// 由网址创建URL对象
URLConnection tc = url.openConnection();// 获得URLConnection对象
tc.connect();// 设置网络连接
InputStreamReader in = new InputStreamReader(tc.getInputStream());
BufferedReader dis = new BufferedReader(in);// 采用缓冲式输入
String inLine;
while ((inLine = dis.readLine()) != null) {
showArea.append(inLine + "\n");
}
dis.close();// 网上资源使用结束后,数据流及时关闭
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/* 访问网上资源可能产生MalformedURLException和IOException异常 */
}
}
例10.3C/S模式中的Client端应用程序。
import java.io.*;
import java.net.*; public class Client {
public static void main(String args[]) {
String s = null;
Socket mySocket;
DataInputStream in = null;
DataOutputStream out = null;
try {
mySocket = new Socket("localhost", 4441);// 本地机IP地址
in = new DataInputStream(mySocket.getInputStream());
out = new DataOutputStream(mySocket.getOutputStream());
out.writeUTF("服务器,你好");// 通过out向“线路”写入信息
while (true) {
s = in.readUTF();// 通过使用in读取服务器放入“线路”里的信息
if (s == null) {
break;// 输入无信息结束输入
} else {
System.out.print(s);
}
}
mySocket.close();// 关闭Socket
} catch (IOException e) {
System.out.print("无法连接");
}
}
}
例10.4与例10.3Client端应用程序对应的Server端应用程序。
import java.io.*;
import java.net.*; public class Server {
public static void main(String args[]) {
ServerSocket server = null;
Socket you = null;
String s = null;
DataOutputStream out = null;
DataInputStream in = null;
try {
server = new ServerSocket(4441);
} catch (IOException e1) {
System.out.print("ERROR:" + e1);
}
try {
you = server.accept();
in = new DataInputStream(you.getInputStream());
out = new DataOutputStream(you.getOutputStream());
while (true) {
s = in.readUTF();// 通过使用in读取客户放入“线路”里的信息
if (s != null) {
break;
}
}
out.writeUTF("客户,你好,我是服务器");// 通过out向“线路”写入信息
out.close();
} catch (IOException e) {
System.out.print("ERRO:" + e);
}
}
}
例10.5将套接字连接工作置于线程的客户端小应用程序。
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*; public class Aclient extends Applet implements Runnable, ActionListener {
JButton button;
JTextField textF;
JTextArea textA;
Socket socket;
Thread thread;
DataInputStream in;
DataOutputStream out; public void init() {
setBackground(new Color(120, 153, 137));
setLayout(new BorderLayout());
button = new JButton("发送消息");
textF = new JTextField(20);
textA = new JTextArea(20, 30);
setSize(450, 350);
JPanel p = new JPanel();
p.add(textF);
p.add(button);
add(textA, "Center");
add(p, "South");
button.addActionListener(this);
} public void start() {
try {
socket = new Socket(this.getCodeBase().getHost(), 4441);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
}
if (thread == null) {
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
} public void run() {
String s = null;
while (true) {
try {
s = in.readUTF();/* 通过in读取服务器放入“线路”里的信息 */
} catch (IOException e) {
}
if (s.equals("结束")) {
try {
socket.close();
break;
} catch (IOException e) {
}
} else {
textA.append(s + "\n");
}
}
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
String s = textF.getText();
if (s != null) {
try {
out.writeUTF(s);
} catch (IOException e1) {
}
} else {
try {
out.writeUTF("请说话");
} catch (IOException e1) {
}
}
}
}
}
例10.6对应例10.5客户端小应用程序的服务器端小应用程序。
import java.io.*;
import java.net.*;
import java.util.*; public class Aserver {
public static void main(String args[]) {
ServerSocket server = null;
ServerThread thread;
Socket client = null;
while (true) {
try {
server = new ServerSocket(4331);
} catch (IOException e1) {
System.out.println("监听时发现错误" + "ERROR:" + e1);
}
try {
client = server.accept();
} catch (IOException e1) {
System.out.println("正在等待客户时,出错!");
}
if (client != null) {
new ServerThread(client).start();
} else {
continue;// 继续等待客户呼叫
}
}
}
} class ServerThread extends Thread {
Socket socket;
String s = null;
DataOutputStream out = null;
DataInputStream in = null; ServerThread(Socket t) {
socket = t;// 参照t创建输入流和输出流
try {
in = new DataInputStream(t.getInputStream());
out = new DataOutputStream(t.getOutputStream());
} catch (IOException e) {
}
} public void run() {
while (true) {
try {
s = in.readUTF();// 通过in读取客户放入“线路”里的信息
} catch (IOException e) {
System.out.println("ERROR:" + e);
}
try {
if (s.equals("结束"))// 客户离开,服务器也离开
{
out.writeUTF(s);
socket.close();
} else {
try {
out.writeUTF("我是服务器你对我说:" + s);
// 通过out向写入“线路”回复信息
} catch (IOException e) {
}
}
} catch (IOException e) {
}
}
}
}
04747_Java语言程序设计(一)_第10章_网络与数据库编程基础的更多相关文章
- 全国计算机等级考试二级教程-C语言程序设计_第10章_字符串
字符型指针数组 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //参数中,int a ...
- 04737_C++程序设计_第10章_面向对象设计实例
10.6.2 使用包含的参考程序及运行结果. 头文件cpp10.h 源文件cpp10.cpp 源文件Find10.cpp 头文件cpp10.h #if ! defined(CPP10_H) #defi ...
- 【安富莱】【RL-TCPnet网络教程】第10章 RL-TCPnet网络协议栈移植(FreeRTOS)
第10章 RL-TCPnet网络协议栈移植(FreeRTOS) 本章教程为大家讲解RL-TCPnet网络协议栈的FreeRTOS操作系统移植方式,学习了第6章讲解的底层驱动接口函数之后,移植就 ...
- ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 使用ArcGIS进行空间分析 1.1 GIS分析基础 G ...
- ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一 ...
- ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区 1 用ArcMap制作地图 作为ArcGIS for Deskto ...
- 运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程,把获取的信息存入数据库
运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程 有关前两篇的链接: 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 运 ...
- 《mysql必知必会》学习_第10章_20180731_欢
第10章,计算字段. P64 select concat (vend_name,'(',vend_country,')') from vendors order by vend_name; # 拼接, ...
- 网易云课堂_C++程序设计入门(下)_第10单元:月映千江未减明 – 模板_第10单元 - 单元作业:OJ编程 - 创建数组类模板
第10单元 - 单元作业:OJ编程 - 创建数组类模板 查看帮助 返回 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提 ...
随机推荐
- USB枚举详细过程剖析(转)
USB枚举详细过程剖析(转) 原文地址:http://blog.163.com/luge_arm/blog/static/6774972620071018117290/ 从驱动开发网看到一篇<U ...
- 必看谷歌HTML/CSS规范
背景 这篇文章定义了 HTML 和 CSS 的格式和代码规范,旨在提高代码质量和协作效率. 通用样式规范 协议 省略图片.样式.脚本以及其他媒体文件 URL 的协议部分( http:,https: ) ...
- 在线C语言编译器/解释器
在线C语言编译器/解释器 本文介绍两个C语言在线解释器/编译器,这些工具可以提高代码片段检测方便的工作效率,并可以保证这些代码的正确性,而且还可以和别人一起编辑/分享之间的代码,这样可以共同分析代码并 ...
- oracle日期计算
查询某月有多少天.代码例如以下: select to_number(add_months( trunc(to_date('2014-11-4 11:13:53','yyyy-mm-dd hh24:mi ...
- 解决如何让AsyncTask终止操作
受到这个的启发终于结局了如何在AsyncTask运行中终止其操作. 单纯的onCancelled(true)是不行的 下面把代码贴出来~实现了登陆功能. AsyncTask简介,它使创建需要与用户界面 ...
- ORACLE查看数据文件-控制文件-日志文件-表空间信息
1.查看当前数据库中的所有用户:select username from dba_users; 2.查看当前会话登录的用户:show user或select username from user_us ...
- 设置c#windows服务描述及允许服务与桌面交互的几种方法(转)
方法一: 在ProjectInstaller.cs重写 install() ,Uninstall()方法 public override void Install(IDictionary stateS ...
- Android-图标
首先需要申明一点,系统图标并不存在于项目资源中,而是存在于设备中. 这就带来一个大问题,界面风格的控制权交到了不同的设备手中.这是我们不愿意看到的. 如何解决这个问题?有两种方法: 1.创建自己的图标 ...
- sql两个表联合更新
update TableA set id=TableB.id from TableA,TableB where TableA.name=TableB.name
- hdu5323 Solve this interesting problem(爆搜)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Solve this interesting problem Time Limit ...