Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* ChatServer.java
*
* Created on 20.08.2010, 20:54:05
*/
/**
*
* @author RadioActiveFrosch
*/
package mulserver;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main extends javax.swing.JFrame {
/** Creates new form ChatServer */
public Main() {
initComponents();
}
/** 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() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jProgressBar1 = new javax.swing.JProgressBar();
jLabel4 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextField2 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Chat");
jLabel1.setText("First Enter IP");
jTextField1.setText("5.174.214.159");
jLabel2.setText("Press The Button");
jButton1.setText("Press Me");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel3.setText("Wating for Progress Bar ");
jLabel4.setText("Chatting ;)");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton2.setText("Enter");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(jButton1)
.addComponent(jLabel3)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel4)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}// </editor-fold>
/*
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
*/
public String ta;
void setValue( String val )
{
this.ta =jTextArea1.getText();
}
String getValue()
{
return ta;
}
public void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
{
String ip;
Socket server = null;
String teststring;
ip=jTextField1.getText();
try
{
server = new Socket( ip , 3141 );
Scanner in = new Scanner( server.getInputStream() );
PrintWriter out = new PrintWriter( server.getOutputStream(), true );
out.println( "Bin ich angekommen?" );
teststring= in.nextLine();
if(teststring == null ? "Ja bin Ich!" == null : teststring.equals("Ja bin Ich!")){
jProgressBar1.setValue(100);
jTextArea1.append("Connected to the Server!\n");
}else{
jTextArea1.append("ERROR! CANT' CONNECT TO THE SERVER");
}
}
catch ( UnknownHostException e ) {
e.printStackTrace();
}
catch ( IOException e ) {
e.printStackTrace();
}
finally
{
if ( server != null )
try { server.close(); } catch ( IOException e ) { }
}
}}
public void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setEditable(false);
String newline = "\n";
String ta=jTextArea1.getText();
String ip;
Socket server = null;
ip=jTextField1.getText();
String text;
if(ta == null ? "ERROR! CANT' CONNECT TO THE SERVER" == null : ta.equals("ERROR! CANT' CONNECT TO THE SERVER")){
}else{
text = jTextField2.getText();
if( !text.equals("")){
jTextArea1.append(text + newline);
}else{}
}
try
{
text = jTextField2.getText();
server = new Socket( ip , 3141 );
PrintWriter out = new PrintWriter( server.getOutputStream(), true );
out.println(text);
jTextField2.setText("");
}
catch ( UnknownHostException e ) {
e.printStackTrace();
}
catch ( IOException e ) {
e.printStackTrace();
}
finally
{
if ( server != null )
try { server.close(); } catch ( IOException e ) { }
}
}
/*
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
}
*/
private static void handleConnection( Socket client ) throws IOException
{
Scanner in = new Scanner( client.getInputStream() );
PrintWriter out = new PrintWriter( client.getOutputStream(), true );
String instring = in.nextLine();
if( instring == null ? "Bin ich angekommen?" == null : instring.equals("Bin ich angekommen?"))
out.println("Ja bin Ich!");
}
private void sendtext( Socket client ) throws IOException
{
String ip;
Socket server = null;
String teststring;
ip=jTextField1.getText();
try
{
server = new Socket( ip , 3141 );
Scanner in = new Scanner( server.getInputStream() );
String instring = in.nextLine();
jTextArea1.append(instring+"\n");
}
catch ( UnknownHostException e ) {
e.printStackTrace();
}
catch ( IOException e ) {
e.printStackTrace();
}
finally
{
if ( server != null )
try { server.close(); } catch ( IOException e ) { }
}}
public static void main(String args[]) throws IOException
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
} });
Timer timer = new Timer("MeinDienst");
TimerTask timerTask = new TimerTask(){
public void run() {
sendtext(client);
}
};
timer.scheduleAtFixedRate(timerTask, 10, 500);
/*Server Part*/
ServerSocket server = new ServerSocket( 3141 );
while ( true )
{
Socket client = null;
try
{
client = server.accept();
handleConnection ( client );
}
catch ( IOException e ) {
e.printStackTrace();
}
finally {
if ( client != null )
try { client.close(); } catch ( IOException e ) { }
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JProgressBar jProgressBar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
Hi ihr!
Ich habe ein kleines problem und zwar möchte ich gerne mit einem Timer die funktion sendtext alle 0.5 - 1 sekunde Durchführen lassen damit ich schauen kann ob ein neuer String von außen angekommen ist und ihn in jTextArea1 hinzufügen.
Ihr werdet sicherlich über die Schreibweise meckern habe aber erstmal Codeschnipsel gesammelt und zusammen gefügt.
Das die Kommunikation Funktioniert sehe ich schon am jButton1 aber irgendwie kriege ich es nicht hin den Text ind das TextArea hinzuzufügen da er dann rummeckern wird(der Comp) das es eine non-static im static ist(immo). Hoffe ihr könnt es lesen und dann vllt mir helfen^^
OS:Windows
IDE:Netbeans mit allen drum und drann
Verwendung: ChatClient