实验名称: 网络实验
所使用的工具软件及环境:
环境:Windows 8.1;jdk 1.8;
工具:eclipse
实验目的:
了解JAVA网络编程的基础知识,IP和端口等。
1.掌握客户端服务器的基本概念。
2.掌握Socket通信的基本过程。
3.掌握Socket通信的常用方法。
实验内容:
图形界面编程,利用socket和UDP/TCP编写,客户端和服务端程序可以进行多次对话。
源码
1.ServerDemo.java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import java.awt.Color;
import javax.swing.JScrollPane;
public class ServerDemo extends JFrame {
private JPanel contentPane;
public JTextArea textField ;
private JScrollPane scrollPane ;
public JTextField textField_1;
public JButton button = new JButton("发 送");
private boolean flag=false;
public ServerDemo() {
setTitle("ServerDemo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label_1 = new JLabel("对话内容:");
label_1.setBounds(30, 10, 80, 15);
contentPane.add(label_1);
/*JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(30, 172, 376, -130);
contentPane.add(scrollPane);
*/
scrollPane = new JScrollPane();
scrollPane.setBounds(30, 37, 376, 144);
contentPane.add(scrollPane);
textField = new JTextArea();
textField.setBounds(30, 37, 376, 144);
textField.setEditable(false);
scrollPane.setViewportView(textField);
//contentPane.add(textField);
JLabel label = new JLabel("请输入您要发送的消息:");
label.setBounds(30, 191, 146, 15);
contentPane.add(label);
textField_1 = new JTextField();
textField_1.setBounds(172, 188, 221, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
button.setBounds(66, 228, 93, 23);
contentPane.add(button);
JButton btnNewButton = new JButton("清 除");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField_1.setText("");
}
});
btnNewButton.setBounds(242, 228, 93, 23);
contentPane.add(btnNewButton);
}
public JTextArea get() {
return this.textField;
}
public JTextField get_1() {
return this.textField_1;
}
public boolean getFlag() {
return this.flag;
}
public void revise() {
this.flag=false;
}
}
2.Server.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.text.SimpleDateFormat;
public class Server extends Thread{
private ServerDemo serverFrame;
public void run() {
ServerSocket serverSocket=null;
Socket clientSocket=null;
String str=null;
serverFrame = new ServerDemo();
serverFrame.setVisible(true);
try {
serverSocket=new ServerSocket(60002);
clientSocket=serverSocket.accept();
serverFrame.textField.setText("有客户连接成功!"+"\r\n");
DataInputStream in=new DataInputStream(clientSocket.getInputStream());
DataOutputStream out=new DataOutputStream(clientSocket.getOutputStream());
out.writeUTF("连接成功!");
serverFrame.button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(serverFrame.textField_1.getText().equals("")==false) {
if(serverFrame.textField.getText()!=null) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String s = simpleDateFormat.format(date);
serverFrame.textField.setText(serverFrame.textField.getText()+"服务端:"+s+"\r\n"+" "+serverFrame.textField_1.getText()+"\r\n");
}
else {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String s = simpleDateFormat.format(date);
serverFrame.textField.setText("服务端:"+s+"\r\n"+" "+serverFrame.textField_1.getText()+"\r\n");
}
try{
out.writeUTF(serverFrame.textField_1.getText());
}catch (Exception exception) {
System.out.println(exception.getMessage());
}
serverFrame.textField_1.setText("");
}
}
});
while(true) {
str=in.readUTF();
if(str.equals("")==false) {
if(serverFrame.textField.getText()!=null) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String s = simpleDateFormat.format(date);
serverFrame.textField.setText(serverFrame.textField.getText()+"客户端:"+s+"\r\n"+" "+str+"\r\n");
}
else {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String s = simpleDateFormat.format(date);
serverFrame.textField.setText("客户端:"+s+"\r\n"+" "+str);
}
}
//out.writeUTF(serverFrame.textField_1.getText());
}
//in.close();
//out.close();
//clientSocket.close();
//serverSocket.close();
}catch (Exception exception) {
System.out.println(exception.getMessage());
}
}
public static void main(String[] args) {
Server server=new Server();
server.start();
}
}
3.ClientDemo.java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
public class ClientDemo extends JFrame {
private JPanel contentPane;
public JTextArea textField ;
private JScrollPane scrollPane ;
public JTextField textField_1;
public JButton button = new JButton("发 送");
private boolean flag=false;
public ClientDemo() {
setTitle("ClientDemo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("对话内容:");
label.setBounds(26, 10, 69, 15);
contentPane.add(label);
scrollPane = new JScrollPane();
scrollPane.setBounds(26, 30, 379, 142);
contentPane.add(scrollPane);
textField = new JTextArea();
textField.setBounds(26, 30, 379, 142);
scrollPane.setViewportView(textField);
//contentPane.add(textField);
JLabel label_1 = new JLabel("请输入你要发送的消息:");
label_1.setBounds(26, 192, 149, 21);
contentPane.add(label_1);
textField_1 = new JTextField();
textField_1.setBounds(185, 192, 193, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
button.setBounds(65, 223, 93, 23);
contentPane.add(button);
JButton button_1 = new JButton("清 除");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField_1.setText("");
}
});
button_1.setBounds(230, 223, 93, 23);
contentPane.add(button_1);
}
public JTextArea get() {
return this.textField;
}
public JTextField get_1() {
return this.textField_1;
}
public boolean getFlag() {
return this.flag;
}
public void revise() {
this.flag=false;
}
}
4.Client.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Client extends Thread{
private ClientDemo clientFrame;
public void run() {
Socket client=null;
String fromServer=null;
clientFrame=new ClientDemo();
clientFrame.setVisible(true);
try {
client=new Socket("127.0.0.1",60002);
DataOutputStream out=new DataOutputStream(client.getOutputStream());
DataInputStream in=new DataInputStream(client.getInputStream());
clientFrame.button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(clientFrame.textField_1.getText().equals("")==false) {
if(clientFrame.textField.getText()!=null) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String s = simpleDateFormat.format(date);
clientFrame.textField.setText(clientFrame.textField.getText()+"客户端:"+s+"\r\n"+" "+clientFrame.textField_1.getText()+"\r\n");
}
else {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String s = simpleDateFormat.format(date);
clientFrame.textField.setText("客户端:"+s+"\r\n"+" "+clientFrame.textField_1.getText()+"\r\n");
}
try{
out.writeUTF(clientFrame.textField_1.getText());
}catch (Exception exception) {
System.out.println(exception.getMessage());
}
clientFrame.textField_1.setText("");
}
}
});
while(true) {
fromServer=in.readUTF();
if(fromServer.equals("")==false)
{
if(fromServer.equals("连接成功!")==true)
clientFrame.textField.setText("连接成功!"+"\r\n");
else {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String s = simpleDateFormat.format(date);
clientFrame.textField.setText(clientFrame.textField.getText()+"服务端:"+s+"\r\n"+" "+fromServer+"\r\n");
}
}
}
//out.close();
//in.close();
//client.close();
}catch(Exception exception) {
System.out.println(exception.getMessage());
}
}
public static void main(String[] args) {
Client client=new Client();
client.start();
}
}
截图