Hallo,
um meine IO netzwerkplatine Platine internetfähig zu bekommen
erstelle ich gerade eine Applikation mit Netbeans 6.8.
Ich verbinde mich dazu per Socket Verbindung mit der Platine und schicke ihr ASCII Zeichenfolegen wie "SETPORT 1.1" um die Ausgänge zu Schalten.
Jetzt habe ich das nächste Problem. Ich Setze mit den Buttons jBein und jBaus die Stringvariable Port1.
Danach müsste ich die Methode schreibeNachricht() aufrufen.
Welchen Parameter für socket muss ich denn vorgeben? Die Methode schreibeNachricht() verlangt einen String und Socket.
Gruß
Ethidium
um meine IO netzwerkplatine Platine internetfähig zu bekommen
erstelle ich gerade eine Applikation mit Netbeans 6.8.
Ich verbinde mich dazu per Socket Verbindung mit der Platine und schicke ihr ASCII Zeichenfolegen wie "SETPORT 1.1" um die Ausgänge zu Schalten.
Java:
package tcptest;
import java.io.*;
import java.awt.*;
import java.awt.Graphics;
* EinAusgabe.java
*
* Created on 29.12.2009, 11:01:13
*/
/**
*
* @author FujitsuSiemens
*/
public class EinAusgabe extends javax.swing.JFrame {
/** Creates new form EinAusgabe */
public EinAusgabe() {
initComponents();
}
String ip;
String Port1;
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jconnectingtoDevice = new javax.swing.JLabel();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jPanel2 = new javax.swing.JPanel();
jBein = new javax.swing.JButton();
jBaus = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Einstellungen"));
jButton1.setText("Verbinden");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Trennen");
jButton3.setText("Fenster schließen");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jLabel1.setText("IP-Adresse:");
jLabel2.setText("Portnummer:");
jRadioButton1.setText("jRadioButton1");
jRadioButton2.setText("jRadioButton2");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(20, 20, 20))
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING))
.addComponent(jLabel2))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jButton2))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
.addComponent(jTextField2))))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jconnectingtoDevice, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE))
.addComponent(jRadioButton2))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jconnectingtoDevice, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jRadioButton2)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jRadioButton1)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addContainerGap())
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "IO", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 255))); // NOI18N
jBein.setText("ein");
jBein.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBeinActionPerformed(evt);
}
});
jBaus.setText("aus");
jBaus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBausActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jBaus)
.addComponent(jBein))
.addContainerGap(304, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jBein)
.addGap(18, 18, 18)
.addComponent(jBaus)
.addContainerGap(64, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose(); //Fenster schließen
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
/* jconnectingtoDevice.setOpaque(true);
jconnectingtoDevice.setBackground(new Color(51,40,255));
jconnectingtoDevice.setText("Verbinde zu Tannenbaum");
*/
// Thread connect;
//connect =new Thread ();
//connect.start();
EinAusgabe client ;
try {
test();
} catch (IOException e) {
e.printStackTrace();
}
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jBeinActionPerformed(java.awt.event.ActionEvent evt) {
Port1 = "SETPORT 4.1"; // TODO add your handling code here:
schreibe(???socket,Port1 );
}
private void jBausActionPerformed(java.awt.event.ActionEvent evt) {
Port1 = "SETPORT 4.0"; // TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EinAusgabe().setVisible(true);
}
});
}
void test() throws IOException {
ip = jTextField1.getText(); // mal vorgeben
// Die Eingabe holen
String portString = jTextField2.getText();
int port = 0;
if ( portString != null
&& portString.trim().length() > 0 ) {
port = Integer.parseInt( portString );
} // if
port = Integer.parseInt(jTextField2.getText());
System.out.println("int value = " + port);
jconnectingtoDevice.setText(ip +" "+ port);
//port = Integer.valueOf(jTextField2.getText()).intValue();
java.net.Socket socket = new java.net.Socket(ip,port); // Verbindung zur Platine
}
void schreibeNachricht(java.net.Socket socket, String nachricht) throws IOException {
String zuSendendeNachricht = Port1;
schreibeNachricht(socket, zuSendendeNachricht);
PrintWriter printWriter =
new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream()));
printWriter.print(nachricht);
printWriter.flush();
}
Jetzt habe ich das nächste Problem. Ich Setze mit den Buttons jBein und jBaus die Stringvariable Port1.
Danach müsste ich die Methode schreibeNachricht() aufrufen.
Welchen Parameter für socket muss ich denn vorgeben? Die Methode schreibeNachricht() verlangt einen String und Socket.
Gruß
Ethidium
Zuletzt bearbeitet: