Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JButton;
- import javax.swing.JTextField;
- import java.net.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.io.*;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class Server_GUI {
- private JFrame frame;
- private JTextField portTF;
- private ServerSocket sSocket;
- private int port;
- public static boolean alive=false;
- private static JLabel lblStatus;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- Server_Alive livecheck = new Server_Alive();
- livecheck.start();
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Server_GUI window = new Server_GUI();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public Server_GUI() {
- initialize();
- }
- public static void isAlive(){
- if(alive==false){
- lblStatus.setText("Offline");
- }
- if(alive==true){
- lblStatus.setText("Online");
- alive=false;
- }
- }
- private boolean isInteger(String readin){
- Pattern p = Pattern.compile("[0-9]+");
- Matcher m = p.matcher(readin);
- boolean b = m.find();
- if(b==true){
- return true;
- }
- return false;
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 320, 177);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
- JLabel lblMeineIps = new JLabel("Meine IPs:");
- lblMeineIps.setBounds(114, 11, 82, 14);
- frame.getContentPane().add(lblMeineIps);
- JLabel lblNewLabel = new JLabel("Meine \u00F6ffentliche IP");
- lblNewLabel.setBounds(23, 36, 115, 14);
- frame.getContentPane().add(lblNewLabel);
- JLabel lblMeinePrivateIp = new JLabel("Meine private IP");
- lblMeinePrivateIp.setBounds(23, 50, 115, 14);
- frame.getContentPane().add(lblMeinePrivateIp);
- JButton btnServerStarten = new JButton("Server starten");
- btnServerStarten.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- boolean listeningSocket = true;
- String portST = portTF.getText();
- if(isInteger(portST)==true){
- port=Integer.parseInt(portST);
- try{
- sSocket = new ServerSocket(port);
- }
- catch(IOException e){
- Client_GUI.writeTA("Could not listen to the port");
- }
- while(listeningSocket){
- try{
- Socket clientSocket = sSocket.accept();
- Server_Thread sT = new Server_Thread(clientSocket);
- sT.start();
- alive=true;
- }
- catch(IOException e){
- }
- }
- try{
- sSocket.close();
- }
- catch(Exception e){
- }
- }
- }
- });
- btnServerStarten.setBounds(16, 100, 252, 23);
- frame.getContentPane().add(btnServerStarten);
- JLabel lblStatuslbl = new JLabel("Status:");
- lblStatuslbl.setBounds(23, 125, 46, 14);
- frame.getContentPane().add(lblStatuslbl);
- lblStatus = new JLabel("Offline");
- lblStatus.setBounds(222, 125, 46, 14);
- frame.getContentPane().add(lblStatus);
- JLabel public_IPlbl = new JLabel("127.0.0.1");
- public_IPlbl.setBounds(186, 36, 118, 14);
- frame.getContentPane().add(public_IPlbl);
- JLabel local_IPlbl = new JLabel("127.0.0.1");
- local_IPlbl.setBounds(186, 50, 118, 14);
- frame.getContentPane().add(local_IPlbl);
- JLabel lblPort = new JLabel("Port:");
- lblPort.setBounds(23, 75, 46, 14);
- frame.getContentPane().add(lblPort);
- portTF = new JTextField();
- portTF.setText("75");
- portTF.setBounds(186, 70, 82, 20);
- frame.getContentPane().add(portTF);
- portTF.setColumns(10);
- try {
- local_IPlbl.setText(InetAddress.getLocalHost().getHostAddress());
- } catch (UnknownHostException e) {
- e.printStackTrace();
- }
- try{
- public_IPlbl.setText(new BufferedReader(new InputStreamReader(new URL("http://agentgatech.appspot.com").openStream())).readLine());
- }
- catch (Exception e){
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement