fork download
  1. public class ServerForm extends javax.swing.JFrame {
  2.  
  3. ServerSocket server =null;
  4. Socket client =null;
  5. DataOutputStream dos = null;
  6. DataInputStream dis = null;
  7.  
  8. public ServerForm() {
  9. initComponents();
  10. }
  11.  
  12. /**
  13.   * This method is called from within the constructor to initialize the form.
  14.   * WARNING: Do NOT modify this code. The content of this method is always
  15.   * regenerated by the Form Editor.
  16.   */
  17. @SuppressWarnings("unchecked")
  18. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  19. private void initComponents() {
  20.  
  21. Nasluchuj = new javax.swing.JButton();
  22. btn_send = new javax.swing.JButton();
  23. txt_msg = new javax.swing.JTextField();
  24. jScrollPane1 = new javax.swing.JScrollPane();
  25. txt_recMsg = new javax.swing.JTextArea();
  26.  
  27. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  28.  
  29. Nasluchuj.setText("Nasłuchuj");
  30. Nasluchuj.addActionListener(new java.awt.event.ActionListener() {
  31. public void actionPerformed(java.awt.event.ActionEvent evt) {
  32. NasluchujActionPerformed(evt);
  33. }
  34. });
  35.  
  36. btn_send.setText("wyslij");
  37. btn_send.addActionListener(new java.awt.event.ActionListener() {
  38. public void actionPerformed(java.awt.event.ActionEvent evt) {
  39. btn_sendActionPerformed(evt);
  40. }
  41. });
  42.  
  43. txt_recMsg.setColumns(20);
  44. txt_recMsg.setRows(5);
  45. jScrollPane1.setViewportView(txt_recMsg);
  46.  
  47. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  48. getContentPane().setLayout(layout);
  49. layout.setHorizontalGroup(
  50. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  51. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  52. .addContainerGap()
  53. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  54. .addGroup(layout.createSequentialGroup()
  55. .addGap(0, 0, Short.MAX_VALUE)
  56. .addComponent(Nasluchuj))
  57. .addGroup(layout.createSequentialGroup()
  58. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  59. .addComponent(txt_msg, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
  60. .addGroup(layout.createSequentialGroup()
  61. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
  62. .addGap(0, 0, Short.MAX_VALUE)))
  63. .addGap(18, 18, 18)
  64. .addComponent(btn_send)))
  65. .addGap(22, 22, 22))
  66. );
  67. layout.setVerticalGroup(
  68. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  69. .addGroup(layout.createSequentialGroup()
  70. .addContainerGap()
  71. .addComponent(Nasluchuj)
  72. .addGap(18, 18, 18)
  73. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
  74. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  75. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  76. .addComponent(btn_send)
  77. .addComponent(txt_msg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  78. .addContainerGap())
  79. );
  80.  
  81. pack();
  82. }// </editor-fold>
  83.  
  84. private void NasluchujActionPerformed(java.awt.event.ActionEvent evt) {
  85. try {
  86. server = new ServerSocket(2000) ;
  87. client = server.accept();
  88. JOptionPane.showMessageDialog(null,"Połączenie udane: ");
  89. dos = new DataOutputStream(client.getOutputStream());
  90. dis = new DataInputStream(client.getInputStream());
  91. ReceiveMessage serverThread = new ReceiveMessage(dis,txt_recMsg);
  92. serverThread.setDaemon(true);
  93. serverThread.setName("Client");
  94. serverThread.start();
  95. } catch (IOException ex) {
  96.  
  97. JOptionPane.showMessageDialog(null,"Połączenie nieudane: ");
  98. }
  99. }
  100.  
  101. private void btn_sendActionPerformed(java.awt.event.ActionEvent evt) {
  102. String m = txt_msg.getText();
  103. try {
  104. dos.writeUTF(m);
  105. } catch (IOException ex) {
  106. Logger.getLogger(ServerForm.class.getName()).log(Level.SEVERE, null, ex);
  107. }
  108. }
  109.  
  110. /**
  111.   * @param args the command line arguments
  112.   */
  113. public static void main(String args[]) {
  114. /* Set the Nimbus look and feel */
  115. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  116. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  117.   * For details see http://d...content-available-to-author-only...e.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  118.   */
  119. try {
  120. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  121. if ("Nimbus".equals(info.getName())) {
  122. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  123. break;
  124. }
  125. }
  126. } catch (ClassNotFoundException ex) {
  127. java.util.logging.Logger.getLogger(ServerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  128. } catch (InstantiationException ex) {
  129. java.util.logging.Logger.getLogger(ServerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  130. } catch (IllegalAccessException ex) {
  131. java.util.logging.Logger.getLogger(ServerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  132. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  133. java.util.logging.Logger.getLogger(ServerForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  134. }
  135. //</editor-fold>
  136.  
  137. /* Create and display the form */
  138. java.awt.EventQueue.invokeLater(new Runnable() {
  139. public void run() {
  140. new ServerForm().setVisible(true);
  141. }
  142. });
  143. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty