Advertisement
Guest User

svrclass

a guest
Oct 6th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. import java.awt.EventQueue;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.JButton;
  6. import javax.swing.JTextField;
  7.  
  8. import java.net.*;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11. import java.io.*;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.ActionEvent;
  14.  
  15.  
  16.  
  17. public class Server_GUI {
  18.  
  19. private JFrame frame;
  20. private JTextField portTF;
  21. private ServerSocket sSocket;
  22. private int port;
  23. public static boolean alive=false;
  24. private static JLabel lblStatus;
  25.  
  26. /**
  27. * Launch the application.
  28. */
  29. public static void main(String[] args) {
  30.  
  31. Server_Alive livecheck = new Server_Alive();
  32. livecheck.start();
  33.  
  34. EventQueue.invokeLater(new Runnable() {
  35. public void run() {
  36. try {
  37. Server_GUI window = new Server_GUI();
  38. window.frame.setVisible(true);
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. });
  44. }
  45.  
  46. /**
  47. * Create the application.
  48. */
  49. public Server_GUI() {
  50. initialize();
  51. }
  52.  
  53.  
  54. public static void isAlive(){
  55. if(alive==false){
  56. lblStatus.setText("Offline");
  57. }
  58. if(alive==true){
  59. lblStatus.setText("Online");
  60. alive=false;
  61. }
  62. }
  63.  
  64.  
  65. private boolean isInteger(String readin){
  66.  
  67.  
  68. Pattern p = Pattern.compile("[0-9]+");
  69. Matcher m = p.matcher(readin);
  70. boolean b = m.find();
  71.  
  72. if(b==true){
  73. return true;
  74. }
  75.  
  76. return false;
  77.  
  78. }
  79.  
  80. /**
  81. * Initialize the contents of the frame.
  82. */
  83. private void initialize() {
  84. frame = new JFrame();
  85. frame.setBounds(100, 100, 320, 177);
  86. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  87. frame.getContentPane().setLayout(null);
  88.  
  89. JLabel lblMeineIps = new JLabel("Meine IPs:");
  90. lblMeineIps.setBounds(114, 11, 82, 14);
  91. frame.getContentPane().add(lblMeineIps);
  92.  
  93. JLabel lblNewLabel = new JLabel("Meine \u00F6ffentliche IP");
  94. lblNewLabel.setBounds(23, 36, 115, 14);
  95. frame.getContentPane().add(lblNewLabel);
  96.  
  97. JLabel lblMeinePrivateIp = new JLabel("Meine private IP");
  98. lblMeinePrivateIp.setBounds(23, 50, 115, 14);
  99. frame.getContentPane().add(lblMeinePrivateIp);
  100.  
  101. JButton btnServerStarten = new JButton("Server starten");
  102. btnServerStarten.addActionListener(new ActionListener() {
  103. public void actionPerformed(ActionEvent arg0) {
  104.  
  105. boolean listeningSocket = true;
  106. String portST = portTF.getText();
  107. if(isInteger(portST)==true){
  108. port=Integer.parseInt(portST);
  109. try{
  110. sSocket = new ServerSocket(port);
  111.  
  112. }
  113. catch(IOException e){
  114. Client_GUI.writeTA("Could not listen to the port");
  115. }
  116.  
  117. while(listeningSocket){
  118.  
  119. try{
  120. Socket clientSocket = sSocket.accept();
  121. Server_Thread sT = new Server_Thread(clientSocket);
  122. sT.start();
  123. alive=true;
  124. }
  125.  
  126. catch(IOException e){
  127.  
  128. }
  129.  
  130. }
  131. try{
  132. sSocket.close();
  133. }
  134. catch(Exception e){
  135.  
  136. }
  137.  
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144. }
  145. });
  146. btnServerStarten.setBounds(16, 100, 252, 23);
  147. frame.getContentPane().add(btnServerStarten);
  148.  
  149. JLabel lblStatuslbl = new JLabel("Status:");
  150. lblStatuslbl.setBounds(23, 125, 46, 14);
  151. frame.getContentPane().add(lblStatuslbl);
  152.  
  153. lblStatus = new JLabel("Offline");
  154. lblStatus.setBounds(222, 125, 46, 14);
  155. frame.getContentPane().add(lblStatus);
  156.  
  157. JLabel public_IPlbl = new JLabel("127.0.0.1");
  158. public_IPlbl.setBounds(186, 36, 118, 14);
  159. frame.getContentPane().add(public_IPlbl);
  160.  
  161. JLabel local_IPlbl = new JLabel("127.0.0.1");
  162. local_IPlbl.setBounds(186, 50, 118, 14);
  163. frame.getContentPane().add(local_IPlbl);
  164.  
  165. JLabel lblPort = new JLabel("Port:");
  166. lblPort.setBounds(23, 75, 46, 14);
  167. frame.getContentPane().add(lblPort);
  168.  
  169. portTF = new JTextField();
  170. portTF.setText("75");
  171. portTF.setBounds(186, 70, 82, 20);
  172. frame.getContentPane().add(portTF);
  173. portTF.setColumns(10);
  174.  
  175. try {
  176. local_IPlbl.setText(InetAddress.getLocalHost().getHostAddress());
  177. } catch (UnknownHostException e) {
  178.  
  179. e.printStackTrace();
  180. }
  181.  
  182. try{
  183. public_IPlbl.setText(new BufferedReader(new InputStreamReader(new URL("http://agentgatech.appspot.com").openStream())).readLine());
  184. }
  185.  
  186. catch (Exception e){
  187.  
  188. }
  189.  
  190.  
  191. }
  192.  
  193.  
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement