package Projekt; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JTextField; import java.awt.event.*; import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.InetAddress; import java.net.Socket; /* Lepiej lapac exception!!! */ public class App { static Socket tSocket; // przy evencie musi byc static albo w klasie musi byc final static JTextField ipText, portText, messageT, myUserT; static JFrame frame = new JFrame("Messages"); static JButton mSendB, fSendB, connectB; static JList messagesL; static DataOutputStream tDos; static DefaultListModel messages = new DefaultListModel<>(); public static void main(String[] args) { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(320, 400); //CZY po wyjsciu z aplikacji zamkaja sie tez inne procesy? chyba tak ipText = new JTextField("0.0.0.0"); ipText.setBounds(10, 10, 200, 30); portText = new JTextField("44444"); portText.setBounds(210, 10, 100, 30); connectB = new JButton("Connect"); connectB.setBounds(110, 50, 100, 30); connectB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ //zapobiegac podwojnemu kliknieciu w connect! Thread receiveThread = new Thread() { //watek w ktorym bedziemy tylko odbierac wiadomosci public void run() { //uwazac zeby nie dochodzic jednoczesnie do tej samej zmiennej (synchronize?) Socket rSocket; DataInputStream rDis; DataOutputStream rDos; try { InetAddress rAddr = InetAddress.getByName(ipText.getText()); int rPort = Integer.parseInt(portText.getText()); rSocket = new Socket(rAddr, rPort); // laczymy rDis = new DataInputStream(rSocket.getInputStream()); // tu bedziemy przyjmowac wiadomosci rDos = new DataOutputStream(rSocket.getOutputStream()); rDos.write(convert("R")); //wysyƂamy znaczek R ipText.setText("OK1"); while (true) { //czytamy wiadomosc byte[] rec = new byte[1024]; if (rDis.read(rec) > 0){ String s = new String(rec); //System.out.println("Serwer powiedzial: "+s); messages.addElement(s); messagesL.setModel(messages); } } } catch (Exception ae) { ae.printStackTrace(); ipText.setText("REC CANT CONNECT"); } return; } }; try { receiveThread.start(); //ustanawiamy polaczenie na wysylanie wiadomosci InetAddress tAddr = InetAddress.getByName(ipText.getText()); int tPort = Integer.parseInt(portText.getText()); tSocket = new Socket(tAddr, tPort); // laczymy tDos = new DataOutputStream(tSocket.getOutputStream()); // tu wysylamy wiadomosci tDos.write(convert("T")); //wysylamy T portText.setText("OK2"); } catch (Exception ae){ ae.printStackTrace(); portText.setText("CANT CONNECT"); } } }); myUserT = new JTextField("Nick"); myUserT.setBounds(10, 50, 100, 30); messagesL = new JList(messages); messagesL.setBounds(10, 130, 300, 150); messageT = new JTextField("Message"); messageT.setBounds(10, 290, 300, 30); mSendB = new JButton("Send"); mSendB.setBounds(55, 330, 100, 30); mSendB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //wysylanie wiadomosci try { tDos.write(convert(myUserT.getText() + "> " + messageT.getText())); //wysylamy messageT.setText(""); } catch (Exception e1) { messageT.setText("CANT SEND"); e1.printStackTrace(); } } }); fSendB = new JButton("File"); fSendB.setBounds(165, 330, 100, 30); frame.add(ipText); frame.add(portText); frame.add(messagesL); frame.add(messageT); frame.add(mSendB); frame.add(fSendB); frame.add(myUserT); frame.add(connectB); frame.setLayout(null); frame.setVisible(true); } synchronized static byte[] convert(String wiad){ byte[] mess = new byte[1024]; //UWAGA pewnie dluzsze wiadomosci trzeba jakos rozdzielac mess = wiad.getBytes(); // konwertujemy na bajty return mess; } }