Guten Abend
ich habe einen kleinen p2p Chat programmiert.
Ich hatte als Vorbild ein anderes Programm, das funktioniert auch perfekt.
Allerding funktioniert mein chat nicht.keine exception etc. die msg kommt beim anderen rechner nicht an.
der aufbau ist simpel, 2 threads fürs versenden eines strings und der andere fürs empfangen.
dann noch eine klasse für die gui ,die auch alles verknüpft.und noch ne weitere, die die eigene IP in einem separatem fenster anzeigt.
ClientThread :
dann der ServerThread :
der HauptThread :
habt ihr vll. nen denkanstoss ?
Gruss
ps : die FAQ hab ich schon gelesen.
ich habe einen kleinen p2p Chat programmiert.
Ich hatte als Vorbild ein anderes Programm, das funktioniert auch perfekt.
Allerding funktioniert mein chat nicht.keine exception etc. die msg kommt beim anderen rechner nicht an.
der aufbau ist simpel, 2 threads fürs versenden eines strings und der andere fürs empfangen.
dann noch eine klasse für die gui ,die auch alles verknüpft.und noch ne weitere, die die eigene IP in einem separatem fenster anzeigt.
ClientThread :
Java:
package javaapplication11;
import java.io.*;
import java.util.*;
import java.net.*;
import java.text.*;
public class ClientThread extends Thread {
public static String ip , name ,msg;
public P2P_Main main = new P2P_Main();
public ClientThread(String ip,String name,String msg){
this.ip = ip;
this.name = name;
this.msg = msg;
}
public void run() {
String uhrzeit = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(new Date());
try {
Socket s = new Socket(ip,6790);
BufferedWriter send = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
send.write("<"+uhrzeit+"> "+name+": "+msg+"\n");
send.flush();
s.close();
}catch (Exception e){main.jTextArea1.setText("clienterror");}
}
}
dann der ServerThread :
Java:
package javaapplication11;
import java.net.*;
import java.io.*;
public class ServerThread extends Thread{
public String k = null;
public ServerSocket servsocket = new ServerSocket(6790);
public P2P_Main main = new P2P_Main();
public ServerThread() throws Exception{
}
@Override
public void run(){
while (true){
try{
Socket sock = servsocket.accept();
BufferedReader buffread = new BufferedReader(new InputStreamReader(sock.getInputStream()));
k = buffread.readLine();
buffread.close();
String before = main.jTextArea1.getText();
main.jTextArea1.setText(before + k);
}catch(Exception e){main.jTextArea1.setText("errorserv"+"\n");}
}
}
}
der HauptThread :
Java:
package javaapplication11;
import java.text.DateFormat;
import javax.swing.JList;
import javax.swing.ListModel;
import java.util.ArrayList;
import java.util.Date;
public class P2P_Main extends javax.swing.JFrame {
public String[] model;
public int allindex;
public ArrayList<String> arrip = new ArrayList<String>();
public static String ipstr;
public static String name2;
public static String msg2;
public P2P_Main() {
initComponents();
}
//die GUI,nicht beachten
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jScrollPane3 = new javax.swing.JScrollPane();
jList2 = new javax.swing.JList();
jLabel1 = new javax.swing.JLabel();
jButton4 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Send");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane2.setViewportView(jTextArea1);
jButton2.setText("Add");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Delete");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jScrollPane3.setViewportView(jList2);
jLabel1.setText("IP");
jButton4.setText("Meine IP");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(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.TRAILING, false)
.addComponent(jTextField1)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE))
.addComponent(jTextField2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jLabel1))))
.addComponent(jTextField3))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jScrollPane3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1)
.addComponent(jButton4))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
//gui-code Fertig
//Jlist Optionen (Jbutton2 + 3)
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
int sel = jList2.getSelectedIndex();
if ((jList2.getSelectedIndex()) == -1){
}else{
arrip.remove(sel);
allindex = arrip.size();
String[] notperm = new String[allindex];
for (int i = 0; i<allindex; i++){
notperm[i] = arrip.get(i);
}
JList s = new JList(notperm);
ListModel list = s.getModel();
jList2.setModel(list);
}
}
//----------------------
//jList option
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if (arrip.contains(jTextField2.getText())){
}else {
arrip.add(jTextField2.getText());
allindex = arrip.size();
String[] notperm = new String[allindex];
for (int i = 0; i<allindex; i++){
notperm[i] = arrip.get(i);
}
JList tlist = new JList(notperm);
ListModel list = tlist.getModel();
jList2.setModel(list);
}
}
//-------------------------------------------
//clientthread starten
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (jList2.getSelectedIndex() != -1){
if(!jTextField1.getText().equals("")){
int selip = jList2.getSelectedIndex();
ipstr = arrip.get(selip);
name2 = jTextField3.getText();
msg2 = jTextField1.getText();
ClientThread ct = new ClientThread(ipstr,name2,msg2);
ct.start();
String uhrzeit = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(new Date());
String before2 = jTextArea1.getText();
jTextArea1.setText(before2 + ("<"+uhrzeit+"> "+name2+": "+msg2+"\n"));
jTextField1.setText("");
}
}
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
MyIP d = new MyIP();
d.setVisible(true);
}
public static void main(String args[]) {
//look and feel
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(P2P_Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(P2P_Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(P2P_Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(P2P_Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//look and feel hier fertig
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new P2P_Main().setVisible(true);
//serverthread starten
try{
ServerThread servthread = new ServerThread();
servthread.start();
}catch (Exception e){}
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JList jList2;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
public javax.swing.JTextArea jTextArea1;
public javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration
}
habt ihr vll. nen denkanstoss ?
Gruss
ps : die FAQ hab ich schon gelesen.