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