例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章_网络与数据库编程基础的更多相关文章

  1. 全国计算机等级考试二级教程-C语言程序设计_第10章_字符串

    字符型指针数组 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //参数中,int a ...

  2. 04737_C++程序设计_第10章_面向对象设计实例

    10.6.2 使用包含的参考程序及运行结果. 头文件cpp10.h 源文件cpp10.cpp 源文件Find10.cpp 头文件cpp10.h #if ! defined(CPP10_H) #defi ...

  3. 【安富莱】【RL-TCPnet网络教程】第10章 RL-TCPnet网络协议栈移植(FreeRTOS)

    第10章     RL-TCPnet网络协议栈移植(FreeRTOS) 本章教程为大家讲解RL-TCPnet网络协议栈的FreeRTOS操作系统移植方式,学习了第6章讲解的底层驱动接口函数之后,移植就 ...

  4. ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 使用ArcGIS进行空间分析 1.1 GIS分析基础 G ...

  5. ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一 ...

  6. ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区 1 用ArcMap制作地图 作为ArcGIS for Deskto ...

  7. 运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程,把获取的信息存入数据库

    运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程 有关前两篇的链接: 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 运 ...

  8. 《mysql必知必会》学习_第10章_20180731_欢

    第10章,计算字段. P64 select concat (vend_name,'(',vend_country,')') from vendors order by vend_name; # 拼接, ...

  9. 网易云课堂_C++程序设计入门(下)_第10单元:月映千江未减明 – 模板_第10单元 - 单元作业:OJ编程 - 创建数组类模板

    第10单元 - 单元作业:OJ编程 - 创建数组类模板 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提 ...

随机推荐

  1. mysql 中文乱码的解决方法

    添加或修改my.ini 配置文件,设置编码字符为utf8 ,默认为latin1,见红色字体 [mysql]# 设置mysql客户端默认字符集default-character-set=utf8 [my ...

  2. pyqt下拉菜单和打开指定的内容(或者exe,doc,ppt,url等内容)

    #下拉菜单及显示 def _action(self): menu=QMenu(self) menu1=menu.addMenu(u'文件') menu11=menu1.addAction(u'新建任务 ...

  3. (转)iOS7界面设计规范(1) - UI基础 - 为iOS7而设计

    今天开个新坑.其实老早就想做这事儿了.记得前一两年,苹果官方还会在开发者中心提供中文的HIG(Human Interface Guideline),后来给没了:网上能够找到的中文版本不知是官方还是同行 ...

  4. Laravel-表单篇-controller

    (慕课网_轻松学会Laravel-表单篇_天秤vs永恒老师_http://www.imooc.com/learn/699) Controller 1.Controller-Request //Requ ...

  5. 使用sublime text2怎样新建文件高速生成HTML头部信息?

    前提须要安装Emmet插件.安装完毕后重新启动sublime. 输入下面简写,按Tab. html:4t <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML ...

  6. 关闭程序 提示 C#

    private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult dr = MessageBox ...

  7. GDI+(Graphics Device Interface)例子

    使用SolidBrush 单色画笔 Bitmap bitmap = new Bitmap(800, 600);            Graphics graphics = Graphics.From ...

  8. jquery颜色渐变

    http://files.cnblogs.com/handsomer/jquery.animate-colors-min.js

  9. 将html导出到excel或word

    本质是将html写成word或excel支持的html格式. 如何将html写成word或excel支持的格式? 只需打开计算机上任意一个word或excel文档,打开文件->另存为,选择文件类 ...

  10. IE6 浏览器常见兼容问题 大汇总(23个)

    IE6以及各个浏览器常见兼容问题 大汇总 综述:虽然说IE6在2014年4月将被停止支持,但是不得不说的是,IE6的市场并不会随着支持的停止而立刻消散下去,对于WEB前端开发工程师来说,兼容IE6 兼 ...