实验八 网络
- 格式:doc
- 大小:105.00 KB
- 文档页数:8
实验八 网络 一、实验目的 1、掌握TCP和UDP 网络程序设计的模型。 2、掌握TCP和UDP 程序设计的基本方法和所使用的系统类。 3、了解基于应用层http协议的程序的设计以及使用系统类的组播、广播等网络程序设计。
二、实验类型 设计型。 三、实验内容 先熟悉所给的示例,了解网络编程的一般基本概念。然后两个同学为一组,分别设计一对控制台下 的TCP 通信程序的客户端和服务器端,然后双方要能够相互发送和反馈信息进行无限次连续通信,直到 其中一方发送表示结束通信的“end”字符串,然后接收方也返回一个“end”,双方结束通信。 若可能的话,建议最好设计出图形界面下的程序(即设计成一个简单的网络聊天程序)。可在本地机 上做测试,或两个同学为一组在不同的机器上作测试都可以,要将服务器地址和端口号作为参数,在运 行中可以进行修改。对于本机上测试,服务器地址使用127.0.0.1 或者本机实际IP地址。在网内不同机 器上测试,要给出正确的服务器IP 地址或名称。 程序源代码: 客户端 import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class Client { public static void main(String args[]) { new WindowClient(); } } class WindowClient extends JFrame implements Runnable, ActionListener { private static final long serialVersionUID = 1L; JButton connection, send, close; JLabel ip, sta; JTextField ipad; TextArea inputText; JTextArea sta1; TextArea showResult; Socket socket = null; DataInputStream in = null; DataOutputStream out = null; Thread thread; WindowClient() { setLayout(null); setBounds(200, 200, 460, 450); setResizable(false); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); socket = new Socket(); connection = new JButton("连接"); send = new JButton("发送"); close = new JButton("关闭"); ipad = new JTextField(); send.setBounds(380, 370, 60, 30); send.setEnabled(false); add(send); close.setBounds(310, 370, 60, 30); add(close); ip = new JLabel("对方IP:"); ip.setBounds(10, 215, 50, 30); add(ip); ipad.setBounds(60, 220, 100, 20); add(ipad); connection.setBounds(180, 220, 60, 20); add(connection); sta = new JLabel("状态:"); sta.setBounds(250, 215, 30, 30); add(sta); sta1 = new JTextArea(); sta1.setBounds(280, 220, 100, 20); sta1.setBackground(Color.red); sta1.setText(" 未连接..."); sta1.setEditable(false); add(sta1); sta1.invalidate(); inputText = new TextArea(); showResult = new TextArea();
showResult.setBounds(10, 10, 440, 200); showResult.setBackground(Color.cyan); add(showResult); inputText.setBounds(10, 250, 440, 100); inputText.setBackground(Color.PINK); add(inputText); connection.addActionListener(this); send.addActionListener(this); close.addActionListener(this); thread = new Thread(this);
} public void actionPerformed(ActionEvent e) { if (e.getSource() == connection) { try { // 请求和服务器建立套接字连接: if (socket.isConnected()) { } else { String add = ipad.getText(); InetAddress address = InetAddress.getByName(add); InetSocketAddress socketAddress = new InetSocketAddress( address, 4331); socket.connect(socketAddress); in = new DataInputStream(socket.getInputStream()); out = new DataOutputStream(socket.getOutputStream()); send.setEnabled(true); sta1.setText(" 连接成功!"); if (!(thread.isAlive())) thread = new Thread(this); thread.start(); } } catch (IOException ee) { System.out.println(ee); sta1.setText(" 连接失败!"); socket = new Socket(); } } if (e.getSource() == send) { String s = inputText.getText(); inputText.setText(" "); showResult.append("\n" + "客户:"); showResult.append("\n" + s); try { out.writeUTF(s); } catch (IOException e1) { } } if (e.getSource() == close) { System.exit(0); } } public void run() { String s = null;
while (true) { try { s = in.readUTF(); showResult.append("\n" + "服务器:"); showResult.append("\n" + s); } catch (IOException e) { sta1.setText(" 已断开"); socket = new Socket(); break; } } } }
服务器: import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*;
import javax.swing.*; public class Server { public static void main(String args[]) { new WindowServer(); } } class WindowServer extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; ServerSocket server = null; Socket client = null; ServerThread Thre; InetAddress address; JButton send, close; JLabel ip, sta; JTextField ipad; TextArea inputText; JTextArea sta1; TextArea showResult;
DataInputStream in = null; DataOutputStream out = null; Thread thread;
WindowServer() { setLayout(null); setBounds(200, 200, 460, 450); setResizable(false); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); client = new Socket();
send = new JButton("发送"); close = new JButton("关闭"); ipad = new JTextField(); send.setBounds(380, 370, 60, 30); send.setEnabled(false); add(send); close.setBounds(310, 370, 60, 30); add(close); ip = new JLabel("对方IP:"); ip.setBounds(10, 215, 50, 30); add(ip); ipad.setBounds(60, 220, 100, 20); ipad.setEditable(false); add(ipad);
sta = new JLabel("状态:"); sta.setBounds(250, 215, 30, 30); add(sta); sta1 = new JTextArea(); sta1.setBounds(280, 220, 100, 20); sta1.setBackground(Color.red);