网络客户/服务器程序

编写网络客户/服务器程序,实现如下功能:

(1)设计服务器程序,运行时等待客户端连接;

(2)客户端设计如下界面,并在启动时与服务器建立连接;

(3)客户端发送消息,服务器收到消息后,将所有小写字符转换为大写字符,发回给客户端;

(4)客户端收到服务器发回的消息后,在界面上显示。

客户端的窗口------>>


Server---->>

package practice9;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
	
	private static Socket so;
	private static DataInputStream dis;
	private static DataOutputStream dos;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			
			ServerSocket ss = new ServerSocket(1000);
			so = ss.accept();
			System.out.println("Conneted successfully");
			dis = new DataInputStream(so.getInputStream());
		} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		//new Thread(()->{}).start();
		//里面的()->{}时Lambda表达式,不懂的自行百度或者翻阅教材
		new Thread(()->{
			try {
				dos = new DataOutputStream(so.getOutputStream());
			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			while(true) {
				try {
					String str = dis.readUTF();
					str = str.toUpperCase();
					dos.writeUTF(str);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					try {
						dis.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
			
		}).start();
	}

}

Clien't-------->>

package practice9;

import java.awt.EventQueue;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Client {

	private JFrame frame;
	private static JTextField textField;
	private static JTextField textField_1;
	private static Socket so;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Client window = new Client();
					window.frame.setTitle("Client");
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		try {
			so = new Socket("127.0.0.1",1000);
			
		} catch (UnknownHostException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		System.out.println("Connect successfully!");
		new Thread(()->{
			DataInputStream dis = null;
			try {
				dis = new DataInputStream(so.getInputStream());
				String str;
				while(true) {
					str = dis.readUTF();
					textField_1.setText(str);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				try {
					
					dis.close();
					so.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}).start();
	}

	/**
	 * Create the application.
	 */
	public Client() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("\u8F93\u5165\u6D88\u606F");
		label.setBounds(14, 32, 72, 18);
		frame.getContentPane().add(label);
		
		JLabel lblNewLabel = new JLabel("\u670D\u52A1\u5668\u54CD\u5E94");
		lblNewLabel.setBounds(14, 91, 84, 18);
		frame.getContentPane().add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(102, 29, 214, 24);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setBounds(102, 88, 214, 105);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		JButton button = new JButton("\u53D1\u9001");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str = textField.getText();
				DataOutputStream dos = null;
				try {
					dos = new DataOutputStream(so.getOutputStream());
					dos.writeUTF(str);
					dos.flush();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					try {
						dos.close();
					} catch (IOException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
				}
				
			}
		});
		button.setBounds(319, 28, 113, 27);
		frame.getContentPane().add(button);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江湖无为

感谢你们的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值