编写网络客户/服务器程序,实现如下功能:
(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);
}
}