Hi, also wie ihr wisst, schreibe ich ja ein ChatPRogramm, also, ich wollte dass man die "PORT-NUMMER" ändern kann, oder dass die PortNUmmer zumindest irgendwo in einem txt document gespeichert ist, natürlich sind das nur zahlen
aber wieso funktioniert das überhaupt nicht?Wenn ich die Nummer aus einem Txt Document laden will?
Oder aus einem String?
[highlight=Java]import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import com.sun.j3d.utils.applet.MainFrame;
public class JavaChat_1 extends Applet implements Runnable
{
public static int PORT;
Socket socket;
DataInputStream in;
PrintStream out;
TextField inputfield;
TextArea outputarea;
Thread thread;
String name;
public JavaChat_1()
{
File f2 = new File("F:/FUTUR.PROGRAMS/Chat/PORT.txt");
PORT = Integer.parseInt(getContents(f2));
File f = new File("F:/FUTUR.PROGRAMS/Chat/Saves.txt");
name = getContents(f);
new chatserver();
inputfield = new TextField();
outputarea = new TextArea();
outputarea.setFont( new Font("Dialog", Font.PLAIN, 12));
outputarea.setEditable(false);
this.setLayout(new BorderLayout());
this.add("South", inputfield);
this.add("Center", outputarea);
this.setBackground(Color.cyan);
this.setForeground(Color.cyan);
inputfield.setBackground(Color.black);
outputarea.setBackground(Color.black);
}
public void start()
{
try
{
socket = new Socket(this.getCodeBase().getHost(), PORT);
in = new DataInputStream(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());
} catch (IOException e)
{
this.showStatus(e.toString());
say("Verbindung zum Server fehlgeschlagen!");
}
say(name+"\n"+"ist der Chat Sitzung"+"\n"+"im Port:"+PORT+" beigetreten"+"\n");
if (thread == null)
{
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void stop()
{
try
{
socket.close();
} catch (IOException e)
{
this.showStatus(e.toString());
}
if ((thread !=null) && thread.isAlive())
{
thread.stop();
thread = null;
}
}
public void run()
{
String line;
try
{
while(true)
{
line = in.readLine();
if(line!=null)
outputarea.appendText(line+'\n' );
}
} catch (IOException e) { say("Verbindung zum Server abgebrochen"); }
}
public boolean action(Event e, Object what)
{
if (e.target==inputfield)
{
String inp=(String)name +" : "+ e.arg;
out.println(inp);
inputfield.setText("");
return true;
}
return false;
}
public void say(String msg)
{
outputarea.appendText(msg);
}
public static void main()
{
new JavaChat_1();
}
static public String getContents(File aFile)
{
StringBuilder contents = new StringBuilder();
try {
BufferedReader input = new BufferedReader(new FileReader(aFile));
try {
String line = null;
while (( line = input.readLine()) != null){
contents.append(line);
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
return contents.toString();
}
}[/highlight]
aber wieso funktioniert das überhaupt nicht?Wenn ich die Nummer aus einem Txt Document laden will?
Oder aus einem String?
[highlight=Java]import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import com.sun.j3d.utils.applet.MainFrame;
public class JavaChat_1 extends Applet implements Runnable
{
public static int PORT;
Socket socket;
DataInputStream in;
PrintStream out;
TextField inputfield;
TextArea outputarea;
Thread thread;
String name;
public JavaChat_1()
{
File f2 = new File("F:/FUTUR.PROGRAMS/Chat/PORT.txt");
PORT = Integer.parseInt(getContents(f2));
File f = new File("F:/FUTUR.PROGRAMS/Chat/Saves.txt");
name = getContents(f);
new chatserver();
inputfield = new TextField();
outputarea = new TextArea();
outputarea.setFont( new Font("Dialog", Font.PLAIN, 12));
outputarea.setEditable(false);
this.setLayout(new BorderLayout());
this.add("South", inputfield);
this.add("Center", outputarea);
this.setBackground(Color.cyan);
this.setForeground(Color.cyan);
inputfield.setBackground(Color.black);
outputarea.setBackground(Color.black);
}
public void start()
{
try
{
socket = new Socket(this.getCodeBase().getHost(), PORT);
in = new DataInputStream(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());
} catch (IOException e)
{
this.showStatus(e.toString());
say("Verbindung zum Server fehlgeschlagen!");
}
say(name+"\n"+"ist der Chat Sitzung"+"\n"+"im Port:"+PORT+" beigetreten"+"\n");
if (thread == null)
{
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void stop()
{
try
{
socket.close();
} catch (IOException e)
{
this.showStatus(e.toString());
}
if ((thread !=null) && thread.isAlive())
{
thread.stop();
thread = null;
}
}
public void run()
{
String line;
try
{
while(true)
{
line = in.readLine();
if(line!=null)
outputarea.appendText(line+'\n' );
}
} catch (IOException e) { say("Verbindung zum Server abgebrochen"); }
}
public boolean action(Event e, Object what)
{
if (e.target==inputfield)
{
String inp=(String)name +" : "+ e.arg;
out.println(inp);
inputfield.setText("");
return true;
}
return false;
}
public void say(String msg)
{
outputarea.appendText(msg);
}
public static void main()
{
new JavaChat_1();
}
static public String getContents(File aFile)
{
StringBuilder contents = new StringBuilder();
try {
BufferedReader input = new BufferedReader(new FileReader(aFile));
try {
String line = null;
while (( line = input.readLine()) != null){
contents.append(line);
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
return contents.toString();
}
}[/highlight]
Zuletzt bearbeitet von einem Moderator: