Nicba1010

TCPServer

Jan 20th, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. package infoKupProcess;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.WindowEvent;
  6. import java.awt.event.WindowStateListener;
  7. import java.io.BufferedReader;
  8. import java.io.DataOutputStream;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.net.ServerSocket;
  12. import java.net.Socket;
  13.  
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JPanel;
  17. import javax.swing.JScrollPane;
  18. import javax.swing.SwingUtilities;
  19.  
  20. class TCPServer extends JFrame {
  21. private int screenWidth = 800, screenHeight = 600;
  22. JButton quitButton;
  23. JScrollPane procScroll;
  24. static ServerSocket inSocket;
  25. static int selectedIndex;
  26. private static boolean running = false;
  27. static ProcessesList processList1;
  28. public JFrame mainFrame = this;
  29. public static Socket connectionSocket;
  30.  
  31. public TCPServer() {
  32. initUI();
  33. }
  34.  
  35. public void initUI() {
  36. setTitle("TCPServer");
  37.  
  38. JPanel panel = new JPanel();
  39. setDefaultCloseOperation(EXIT_ON_CLOSE);
  40. getContentPane().add(panel);
  41. // panel.setLayout(new BorderLayout());
  42. panel.setLayout(null);
  43. setSize(screenWidth, screenHeight);
  44. setLocationRelativeTo(null);
  45. {
  46. quitButton = new JButton("Ugasi");
  47. quitButton.setBounds(mainFrame.getWidth() - 80 - 16,
  48. mainFrame.getHeight() - 30 * 2 - 8, 80, 30);
  49.  
  50. quitButton.addActionListener(new ActionListener() {
  51. @Override
  52. public void actionPerformed(ActionEvent event) {
  53. running = false;
  54. try {
  55. inSocket.close();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. System.exit(0);
  60. }
  61. });
  62. panel.add(quitButton);
  63. }
  64.  
  65. this.addWindowStateListener(new WindowStateListener() {
  66.  
  67. @Override
  68. public void windowStateChanged(WindowEvent e) {
  69. System.out.println(e.getNewState());
  70. quitButton.setBounds(mainFrame.getWidth() - 80 - 16,
  71. mainFrame.getHeight() - 30 * 2 - 8, 80, 30);
  72. }
  73. });
  74. {
  75. processList1 = new ProcessesList(0, 0, 150, 400, panel);
  76. }
  77. System.out.println(this.getWidth());
  78. {
  79. // procList = new JList<String>(processesList);
  80. // procList.setListData(processesList);
  81. // procList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  82. // procList.addListSelectionListener(new ListSelectionListener() {
  83. //
  84. // @Override
  85. // public void valueChanged(ListSelectionEvent e) {
  86. // if (procList.getSelectedIndex() == -1) {
  87. //
  88. // } else {
  89. // if (selectedIndex != procList.getSelectedIndex()) {
  90. // System.out.println(procList.getSelectedIndex());
  91. // selectedIndex = procList.getSelectedIndex();
  92. // }
  93. // }
  94. // }
  95. // });
  96. //
  97. // procScroll = new JScrollPane(procList);
  98. // procScroll.setPreferredSize(new Dimension(150, 400));
  99. //
  100. // JPanelProcList procPanel = new JPanelProcList();
  101. // procPanel.add(procScroll);
  102. // // procPanel.setBorder(new EmptyBorder(0,-223,150,400));
  103. // procPanel.setBounds(0, 0, 150, 400);
  104. // panel.add(procPanel);
  105. }
  106. }
  107.  
  108. public static void main(String argv[]) throws Exception {
  109. running = true;
  110. SwingUtilities.invokeLater(new Runnable() {
  111. @Override
  112. public void run() {
  113. TCPServer ex = new TCPServer();
  114. ex.setVisible(true);
  115. }
  116. });
  117. processProcesses();
  118. }
  119.  
  120. public static void processProcesses() throws Exception {
  121. String clientSentence;
  122. inSocket = new ServerSocket(25565);
  123. while (running) {
  124. connectionSocket = inSocket.accept();
  125. BufferedReader inFromClient = new BufferedReader(
  126. new InputStreamReader(connectionSocket.getInputStream()));
  127.  
  128. clientSentence = inFromClient.readLine();
  129. // processes = clientSentence;
  130. if (clientSentence != null)
  131. processList1.setData(clientSentence.split(":"));
  132. // processesList = processes.split(":");
  133. // procList.setListData(processesList);
  134. // procList.setSelectedIndex(selectedIndex);
  135. DataOutputStream outToClient = new DataOutputStream(
  136. TCPServer.connectionSocket.getOutputStream());
  137. outToClient.writeBytes("killpr");
  138. }
  139. }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment