Witam
Najpierw wyjasnie ,ze javy ucze sie pierwszy semestr , a tak serio to od dobrych kilku dni.
Przez całe popołudnie lekko po omacku stworzyłem mały komunikatorek ,ktorego kod podaje do wglądu.
:-|
Pierwsze to oczywiscie prosze o miejsca gdzie mam coś poprawić bo to praca zaliczeniowa .
Najgorsze jest to ,że sie wysypuje w momencie jak zrobie połącz i bez zadnej akcji rozłącz... masa błędów. [???]
I czego nie udało mi sie do konca opanować to ,to żeby w stanie connected gdy wyjmie sie kabelek sieciowy można było wyłapać reakcje(zebym mógł wyszarzyc wyslij itd) .... czasem udaje mi sie na jeden z wyjątków to zlapać ale niezawsze
(tylko gdy ostatnia akcja to wiadomosc z kompa B, a na kompie A wyjmie sie kabelek to własnie na A wyskoczy wyjątek,który idzie łatwo przechwycic). ;-)
Bardzo prosze o rady. :-|
Dziekuje ;-)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.net.*;
//autor : bor1904
public class FrameServerApp extends JFrame
{
InetAddress addr;
Socket socket;
boolean test;
PrintWriter out;
public static final int PORT = 8080;
ImageIcon i=new ImageIcon ("connect.png");
JTextArea okno = new JTextArea(12,33);
JButton send = new JButton("<< Wyslij >>");
JTextField okienko = new JTextField(" < Tekst do wysłania >");
JLabel ip = new JLabel("IP :");
JTextField oknoip = new JTextField("10.0.0.1",10);
JButton connect = new JButton(" Connect ");
JLabel stan = new JLabel(" STAN: disconnected");
JLabel icon = new JLabel(i);
JLabel blank = new JLabel(" ");
JPanel p1 = new JPanel(new GridLayout(2,1));
JPanel p2 = new JPanel(new BorderLayout());
JPanel p3 = new JPanel(new GridLayout(2,1));
JPanel p4 = new JPanel(new FlowLayout());
JMenuBar jMenuBar = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
Server server;
public class Connection extends Thread
{
Socket socket;
JTextArea textArea;
Connection(Socket s, JTextArea ta)
{
socket = s;
textArea = ta;
}
void addText(String s)
{
textArea.setText(s + "\n" + textArea.getText());
java.awt.Toolkit.getDefaultToolkit().beep();
}
public void run()
{
try
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
while (true)
{
String str = in.readLine();
try {
if ( str.equals("END"))
{
System.out.println(" Nadeszlo polecenie zamkniecia połączenia !");
break;
}
this.addText("------------------------------");
this.addText(socket.getInetAddress() + " : " + str);
}
catch(NullPointerException e)
{
;
}
}
}
finally
{
System.out.println("Połączenie zostało przerwane ....");
socket.close();
}
}
catch(IOException e)
{
connect.setText(" Connect ");
send.setEnabled(false);
stan.setText(" STAN: disconnected");
}
}
}
public class Server extends Thread
{
public static final int PORT = 8080;
JTextArea textArea;
boolean working = true;
Server(JTextArea ta)
{
textArea = ta;
}
void addText(String s)
{
textArea.setText(s + "\n" + textArea.getText());
}
public void end()
{
working = false;
}
public void run()
{
while(working)
{
try
{
ServerSocket s = new ServerSocket(PORT);
try
{
System.out.println("Oczekuje na rozmowe ...");
Socket socket = s.accept();
Connection c = new Connection(socket, textArea);
c.start();
System.out.println("Rozmowa przyjeta - trwa ! port: " + socket.getPort());
}
catch(Exception e) //IO
{
System.out.println("Blad programu: problem z przyjeciem polaczenia");
}
}
catch(IOException e)
{
;
}
}
}
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!! KONSTRUKTOR
public FrameServerApp(String title)
{
super(title);
p4.add(ip);
p4.add(oknoip);
p4.add(blank);
p4.add(icon);
p4.add(connect);
p3.add(okienko);
p3.add(p4);
p2.add(send,BorderLayout.NORTH);
p2.add(p3,BorderLayout.CENTER);
p2.add(stan,BorderLayout.SOUTH);
p1.add(okno);
p1.add(p2);
okno.setLineWrap(true);
okno.setWrapStyleWord(true);
okno.setEditable(false);
//!!!!!!!!ZABEZPIECZA OKNO Z IP
oknoip.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent evt)
{
if ((oknoip.getText().length() >= 15))
{
evt.consume();
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
}
);
//!!!!!!!!!!!! KONIEC - okno ip
stan.setBorder(BorderFactory.createLineBorder(Color.black));
stan.setForeground(Color.gray);
send.setEnabled(false);
send.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sendInformation();
}
});
//____________________________________________________________
connect.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(connect.getText().equals(" Connect "))
{
try
{
addr = InetAddress.getByName(oknoip.getText());
System.out.println("=Lacze sie z komputerem o adresie : " + addr);
socket = new Socket(addr, PORT);
System.out.println("==Na porcie: " + socket.getPort());
test = true;
}
catch(Exception b)
{
connect.setText(" Connect ");
send.setEnabled(false);
stan.setText(" STAN: disconnected");
System.out.println("Polaczenie jest niemozliwe do zrealizowania ......");
test = false;
}
if (test){
connect.setText(" Disconnect ");
send.setEnabled(true);
stan.setText(" STAN: connected");
}
}
else{
connect.setText(" Connect ");
send.setEnabled(false);
stan.setText(" STAN: disconnected");
////////////////////// WYSYPKA /////////////////////
out.println("END");
try{
socket.close();
}
catch(IOException c)
{
System.out.println("Problem z zamknieciem socketu");
}
}
}
});
//________________________________________________________________
try
{
jMenuFile.setText("MENU");
jMenuFileExit.setText("Exit");
jMenuFile.add(jMenuFileExit);
jMenuBar.add(jMenuFile);
this.setJMenuBar(jMenuBar);
jMenuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
server.end();
System.out.println("ZATRZYMANIE PRACY SERVERA");
System.exit(0);
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(p1);
this.setSize(360, 400);
this.setResizable(false);
server = new Server(okno);
server.start();
System.out.println("START SERVERA");
}
catch(Exception e)
{
System.out.println("Błąd programu : Problem z glownym oknem");
e.printStackTrace();
}
}
//!!!!!!!!!!!!!!!!!!!! KONIEC-konstruktor
// !!!!!!!!!! WYSYLANIE DANYCH
private void sendInformation()
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println(okienko.getText());
okno.setText("------------------------------" + "\n" + okno.getText());
okno.setText("Ja:" + okienko.getText() + "\n" + okno.getText());
}
catch(IOException e)
{
System.out.println("Problem z wyslaniem informacji ...");
System.out.println(e);
}
}
public static void main(String[] args) throws IOException
{
InetAddress mojeIP= InetAddress.getLocalHost();
FrameServerApp frameServerApp = new FrameServerApp("IP Komunikator (" + mojeIP.getHostAddress() + ")");
frameServerApp.setVisible(true);
}
}
[diabel] [diabel] [diabel] [diabel] [diabel] [diabel] [diabel] [diabel] [diabel] [diabel]