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.学生可以在作业截止时间之前不限次数提 ...
随机推荐
- 关于bootstrap--表格(table的各种样式)
1.table-striped:斑马线表格 2.table-bordered:带边框的表格 3.table-hover:鼠标悬停高亮的表格 4.table-condensed:紧凑型表格(单元格的内距 ...
- poj 1466 Girls and Boys(二分匹配之最大独立集)
Description In the second year of the university somebody started a study on the romantic relations ...
- Linux下PHP安装配置MongoDB数据库连接扩展
Web服务器: IP地址:192.168.21.127 PHP安装路径:/usr/local/php 实现目的: 安装PHP的MongoDB数据库扩展,通过PHP程序连接MongoDB数据库 具体操作 ...
- IIS 问题解决
一.网站发布后 报500错误 解决办法:重新向iis注册framwork: 二.试图加载格式不正确的程序.(Exception from HRESULT: 0x8007000B) 解决办法:对应应用程 ...
- Js获取元素样式值(getComputedStyle¤tStyle)兼容性解决方案
因为:style(document.getElementById(id).style.XXX)只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的. 一般js获取内部样式和外部样式使用 ...
- Linux应用开发环境搭建
因为笔者是一名大学生,对Linux内核开发方向非常感兴趣,可是实在是能(ji)力(shu)有(cha)限(jin),仅仅能从Linux应用开发開始,由浅入深,逐步进步,登上人生高峰,因此,昨天搭建了开 ...
- laravel3中文文档是迈入laravel4的捷径
http://v3.golaravel.com/docs/ 目录 Laravel概览 更新日志 安装与设置 系统需求 安装 服务器设置 基本设置 环境 友好的链接(URL) 路由 基础 通配符(Wil ...
- python-文件压缩和解压
import tarfile #压缩 tar = tarfile.open('your.tar','w') tar.add('ooo.xml',arcname='ooo.xml') tar.close ...
- [Asp.Net]状态管理(Session、Application、Cache、Cookie 、Viewstate、隐藏域 、查询字符串)
Session: 1. 客户在服务器上第一次打开Asp.Net页面时,会话就开始了.当客户在20分钟之内没有访问服务器,会话结束,销毁session.(当然也可以在Web.config中设置缓存时间 ...
- C# 获取远程xml文件
/// <summary> /// 加载远程XML文档 /// </summary> /// <param name="URL"></pa ...