import java.awt.EventQueue;
public class ServerApp {
private JFrame frame;
private JTextField textField;
private ServerSocket server = null;
private Socket socket = null;
private BufferedReader cin = null;
private PrintStream cout = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ServerApp window = new ServerApp();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ServerApp() {
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);
textField = new JTextField();
textField.setBounds(142, 28, 111, 24);
frame.getContentPane().add(textField);
textField.setColumns(10);
final JTextArea textArea = new JTextArea();
JButton btnNewButton = new JButton("开始监听");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
server = new ServerSocket(Integer.parseInt(textField.getText()));
if(server!=null)
{
socket = server.accept();
cin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
cout = new PrintStream(socket.getOutputStream());
String str = "";
do
{
str = cin.readLine();
textArea.append(str+"\n");
}while(!str.equals("-1"));
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnNewButton.setBounds(271, 27, 113, 27);
frame.getContentPane().add(btnNewButton);
JLabel label = new JLabel("监听端口:");
label.setBounds(38, 31, 90, 18);
frame.getContentPane().add(label);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(48, 67, 331, 159);
frame.getContentPane().add(scrollPane);
scrollPane.setViewportView(textArea);
}
}