📄 client.java~1~
字号:
package chat1;
import javax.swing.JFrame;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
public class Client extends JFrame {
public Client() {
}
private DatagramSocket ds;
private Radio radio;
private Play play;
private JTextField tf;
private JTextArea jTextArea1;
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message="";
private String chatServer;
private Socket client;
private XYLayout xYLayout1 = new XYLayout();
public Client(String host){
super("Client");
chatServer = host;
//Container container = getContentPane();
//XYLayout xYLayout1 = new XYLayout();
getContentPane().setLayout(xYLayout1);
tf = new JTextField();
tf.setEnabled(false);
/*tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
sendData(event.getActionCommand());
tf.setText("");
}
});*/
this.getContentPane().add(tf, new XYConstraints(97, 19, 231, 50));
jTextArea1 = new JTextArea();
this.getContentPane().add(new JScrollPane(jTextArea1), new XYConstraints(98, 95, 232, 119));
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
jButton2.setText("send");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
sendData(event.getActionCommand());
tf.setText("");
}
});
jButton3.setText("disconnectin");
jLabel1.setText("send");
jLabel2.setText("message");
this.getContentPane().add(jLabel1,new XYConstraints(35, 21, 103, 51));
this.getContentPane().add(jLabel2,new XYConstraints(31, 98, 65, 35));
// this.getContentPane().add(new JScrollPane(),BorderLayout.WEST);
jButton1 = new JButton("get sound");
this.getContentPane().add(jButton1, new XYConstraints(134, 226, 155, 40));
setSize(400, 300);
setVisible(true);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
go();
} catch (IOException e) {
}
}
});
}
public void go() throws IOException {
ds = new DatagramSocket(2345);
radio = new Radio(ds, "202.117.21.54");
play = new Play(ds);
radio.start();
play.start();
}
public void runClient(){
try{
connectToServer();
getStreams();
processConnection();
closeConnection();
}
catch(EOFException eofException) {
System.out.println("Server terminated connection");
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
private void getStreams() throws IOException{
output=new ObjectOutputStream(client.getOutputStream());
output.flush();
input=new ObjectInputStream(client.getInputStream());
// jTextArea1.append("\nGot I/O streams\n");
}
private void connectToServer() throws IOException{
//jTextArea1.setText("Attempting connection\n");
client=new Socket(InetAddress.getByName(chatServer),8000);
//jTextArea1.append("Connected to:"+client.getInetAddress().getHostName());
}
private void processConnection() throws IOException{
tf.setEnabled(true);
do{
try{
message=(String) input.readObject();
jTextArea1.append("\n"+message);
jTextArea1.setCaretPosition(jTextArea1.getText().length());
}
catch(ClassNotFoundException classNotFoundException){
jTextArea1.append("\nUnknown object type received");
}
}
while(!message.equals("SERVER>>>TERMINATE"));
}
private void closeConnection() throws IOException{
jTextArea1.append("\nClosing connection");
output.close();
input.close();
client.close();
}
private void sendData(String message){
try{
output.writeObject("CLIENT>>>"+message);
output.flush();
jTextArea1.append("\nCLIENT>>>"+message);
}
catch (IOException ioException){
jTextArea1.append("\nError writing object");
}
}
public static void main(String args[]){
Client application;
if (args.length == 0)
application = new Client("202.117.21.54");
else
application = new Client(args[0]);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.runClient();
}
private void jbInit() throws Exception {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -