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) {
			}
		}
	}
}

 

转载于:https://www.cnblogs.com/denggelin/p/6242630.html

你可能感兴趣的:(04747_Java语言程序设计(一)_第10章_网络与数据库编程基础)