diff --git a/klient/Projekt-Final.jar b/klient/Projekt-Final.jar new file mode 100644 index 0000000..936cae9 Binary files /dev/null and b/klient/Projekt-Final.jar differ diff --git a/klient/pom.xml b/klient/pom.xml index 76b162c..c8cc7cc 100644 --- a/klient/pom.xml +++ b/klient/pom.xml @@ -27,48 +27,36 @@ - - - - - maven-clean-plugin - 3.1.0 - - - - maven-resources-plugin - 3.0.2 - - - maven-compiler-plugin - 3.8.0 - - - maven-surefire-plugin - 2.22.1 - - - maven-jar-plugin - 3.0.2 - - - maven-install-plugin - 2.5.2 - - - maven-deploy-plugin - 2.8.2 - - - - maven-site-plugin - 3.7.1 - - - maven-project-info-reports-plugin - 3.0.0 - - - + ${project.artifactId}-${project.version} + + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + false + false + + + + Projekt.App + + + + + jar-with-dependencies + + + + + + + + diff --git a/klient/src/main/java/Projekt/App.java b/klient/src/main/java/Projekt/App.java index c4831b5..4bc8060 100644 --- a/klient/src/main/java/Projekt/App.java +++ b/klient/src/main/java/Projekt/App.java @@ -2,9 +2,7 @@ package Projekt; import javax.swing.DefaultListModel; import javax.swing.JButton; -import javax.swing.JComboBox; import javax.swing.JFrame; -import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JTextField; import java.awt.event.*; @@ -18,34 +16,78 @@ Lepiej lapac exception!!! */ public class App { - static Socket socket; // przy evencie musi byc static albo w klasie musi byc final + 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; - static JLabel usersL; - static JComboBox usersC; + static JButton mSendB, fSendB, connectB; static JList messagesL; - static DataOutputStream dos; - static DataInputStream dis; + 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("Server IP"); + ipText = new JTextField("0.0.0.0"); ipText.setBounds(10, 10, 200, 30); - portText = new JTextField("Port"); + 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); - usersL = new JLabel("Choose user:"); - usersL.setBounds(15, 90, 100, 30); - String users[] = { "Tomus", "Piotrus" }; - usersC = new JComboBox<>(users); - usersC.setBounds(110, 90, 100, 30); - messages.addElement(" Elo"); - messages.addElement(" No siema"); messagesL = new JList(messages); messagesL.setBounds(10, 130, 300, 150); messageT = new JTextField("Message"); @@ -53,28 +95,11 @@ public class App { mSendB = new JButton("Send"); mSendB.setBounds(55, 330, 100, 30); mSendB.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { + public void actionPerformed(ActionEvent e) { //wysylanie wiadomosci try { - InetAddress addr = InetAddress.getByName(ipText.getText()); - int port = Integer.parseInt(portText.getText()); - socket = new Socket(addr, port); // laczymy - dos = new DataOutputStream(socket.getOutputStream()); // tu wysylamy wiadomosci - dis = new DataInputStream(socket.getInputStream()); // tu bedziemy przyjmowac wiadomosci - - byte[] mess = new byte[1024]; - //UWAGA pewnie dluzsze wiadomosci trzeba jakos rozdzielac - String messS = myUserT.getText() + ">" + messageT.getText() + " " + messageT.getText())); //wysylamy messageT.setText(""); - //czytamy wiadomosc - byte[] rec = new byte[1024]; - dis.read(rec); - String s = new String(rec); - //System.out.println("Serwer powiedzial: "+s); - messages.addElement(s); - messagesL.setModel(messages); } catch (Exception e1) { messageT.setText("CANT SEND"); e1.printStackTrace(); @@ -87,30 +112,21 @@ public class App { frame.add(ipText); frame.add(portText); - frame.add(usersL); - frame.add(usersC); frame.add(messagesL); frame.add(messageT); frame.add(mSendB); frame.add(fSendB); frame.add(myUserT); + frame.add(connectB); frame.setLayout(null); frame.setVisible(true); - -/* - - // czytaj odpowiedz to jest kod golinskiego - //String s = dis.readUTF(); - // wypisz odpowiedz - //System.out.println("Serwer powiedzial: "+s); - dis.close(); - dos.close(); - socket.close(); - - } catch (Exception e) { //kiepskie lapanie wyjatkow!! lepiej bardziej sprecyzowac jaki wyjatek lapiemy - e.printStackTrace(); - } - System.out.println("Klient zakonczyl dzialanie"); - */ } + + 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; + } + } diff --git a/server/server.c b/server/server.c index 522bc44..e69de29 100644 --- a/server/server.c +++ b/server/server.c @@ -1,148 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -struct clients{ - char nick[30]; - char ip[30]; - unsigned short port; -}; - -int finduser(struct clients cli[], char name[]){ //zwraca na ktorym miejscu jest user, -1 jesli go nie ma - for (int i = 0; i < 10; i++){ - if (cli[i].nick[0] != '\0'){ //jesli cos juz zapisalismy - int ok = 1; - int j = 0; - while (cli[i].nick[j] != '\0'){ //dopoki nie ma konca - if (cli[i].nick[j] != name[j]){ //jesli sa rozne to przerywamy - ok = 0; - break; - } - j++; - } - if (ok){ //znalezlismy - return i; - } - } - } - return -1; -} - -int main(void) { - char bufor[1024]; //wiadomosc - int gniazdo, gniazdo2, gniazdo3; //gniazda - struct sockaddr_in adr, nadawca, odbiorca; //nasz adres i adres do ktorego przesylamy wiadomosc - struct clients cli[10]; //maksymalnie 10 klientow - for(int i = 0; i < 10; i++){ //czyscimy zmienne w klientach - cli[i].port = 0; - for(int j = 0; j < 30; j++){ - cli[i].nick[j] = '\0'; - cli[i].ip[j] = '\0'; - } - } - socklen_t dl = sizeof(struct sockaddr_in); - - //nasze dane - gniazdo = socket(PF_INET, SOCK_STREAM, 0); - adr.sin_family = AF_INET; - adr.sin_port = htons(44445); //port - adr.sin_addr.s_addr = INADDR_ANY; - - printf("Slucham na %s:%d\n", inet_ntoa(adr.sin_addr), ntohs(adr.sin_port)); - - if (bind(gniazdo, (struct sockaddr*) &adr, sizeof(adr)) < 0) { //bind - printf("Bind nie powiodl sie.\n"); - return 1; - } - if (listen(gniazdo, 10) < 0) { //listen - printf("Listen nie powiodl sie.\n"); - return 1; - } - printf("Czekam na polaczenie ...\n"); - while ((gniazdo2 = accept(gniazdo, (struct sockaddr*) &nadawca, &dl)) > 0) { //odbieranie - memset(bufor, 0, 1024); - recv(gniazdo2, bufor, 1024, 0); //odbieramy - - //pobieramy dane uzytkownika - char name[30]; - char ip[30]; - unsigned short port = 0; - for (int i = 0; i < 30; i++){ //czyscimy - name[i] = '\0'; - ip[i] = '\0'; - } - int i = 0; - while (bufor[i] != '>'){ //> to jest nasz znak specjalny ktory konczy nazwe uzytkownika - name[i] = bufor[i]; - i++; - } - char *temp = inet_ntoa(nadawca.sin_addr); - size_t len = strlen(temp); - for (int i = 0; i < len; i++){ - ip[i] = temp[i]; - } - port = nadawca.sin_port; - printf("Wiadomosc od %s(%s:%i): %s\n", name, ip, port, bufor); - - //dodajemy go do listy - i = 0; - if (finduser(cli, name) == -1){ //jesli go jeszcze nie ma to doadajemy - while(1){ - if (i >= 10){ - printf("NIE MA DLA CIEBIE MIEJSCA\n"); - break; - } - if(cli[i].nick[0] == '\0'){ //mamy wolne miejsce - cli[i].port = port; - for(int j = 0; j < 30; j++){ - cli[i].nick[j] = name[j]; - cli[i].ip[j] = ip[j]; - } - break; - } - i++; - } - } - - //Znajdujemy do kogo przeslac wiadomosc - char nametosend[30]; - for (int j = 0; j < 30; j++){ //czyscimy - nametosend[j] = '\0'; - } - i = 0; - while(bufor[i] != '<'){ - i++; - } - int j = 0; - i++; - while(bufor[i] != '\0'){ - nametosend[j] = bufor[i]; - i++; - j++; - } - printf("Wiadomosc do %s \n", nametosend); - - //wydobywamy informacje z listy i wysylamy - int index = finduser(cli, nametosend); - - gniazdo3 = socket(PF_INET, SOCK_STREAM, 0); - odbiorca.sin_family = AF_INET; - odbiorca.sin_port = htons(cli[index].port); - odbiorca.sin_addr.s_addr = inet_addr(cli[index].ip); - if (connect(gniazdo3, (struct sockaddr*) &odbiorca, sizeof(odbiorca)) < 0) { - printf("Nawiazanie polaczenia nie powiodlo sie.\n"); - } - //GNIAZDO 2 TO WYSYLANIE WIAD TAM SKAD DOSTALISMY GNIAZDO 3 JAK BEDZIEMY MIELI 3 KOMPUTERY - //JAK USTAWIMY GNIAZDO 2 TO WYRZUCI BLAD ALE PRZESLE - send(gniazdo2, bufor, strlen(bufor), 0); - - close(gniazdo3); - close(gniazdo2); - } - close(gniazdo); - return 0; -} \ No newline at end of file diff --git a/server/uberProstyServer.c b/server/uberProstyServer.c new file mode 100644 index 0000000..7ed3bb6 --- /dev/null +++ b/server/uberProstyServer.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main(void) { + char bufor[1024], rBufor[1024], tBufor[1024]; //wiadomosc + int gniazdo, gniazdo2, rGniazdo, tGniazdo; + struct sockaddr_in adr, nadawca, rNadawca, tNadawca; + socklen_t dl = sizeof(struct sockaddr_in); + + gniazdo = socket(PF_INET, SOCK_STREAM, 0); + adr.sin_family = AF_INET; + adr.sin_port = htons(44444); //port + adr.sin_addr.s_addr = INADDR_ANY; + + printf("Slucham na %s:%d\n", inet_ntoa(adr.sin_addr), ntohs(adr.sin_port)); + + if (bind(gniazdo, (struct sockaddr*) &adr, sizeof(adr)) < 0) { //bind + printf("Bind nie powiodl sie.\n"); + return 1; + } + if (listen(gniazdo, 10) < 0) { //listen + printf("Listen nie powiodl sie.\n"); + return 1; + } + printf("Czekam na polaczenie ...\n"); + while ((gniazdo2 = accept(gniazdo, (struct sockaddr*) &nadawca, &dl)) > 0) { //odbieranie + memset(bufor, 0, 1024); + recv(gniazdo2, bufor, 1024, 0); + printf("Wiadomosc od %s: %s\n", inet_ntoa(nadawca.sin_addr), bufor); + if (bufor[0] == 'T'){ + tGniazdo = gniazdo2; + tNadawca = nadawca; + if (fork() == 0){ //child wejdzie w while + while (1){ + memset(tBufor, 0, 1024); + recv(tGniazdo, tBufor, 1024, 0); + printf("Wiadomosc od %s: %s\n", inet_ntoa(tNadawca.sin_addr), tBufor); + } + } + } else { + rGniazdo = gniazdo2; + rNadawca = nadawca; + if(fork() == 0){ //child wejdzie w while + int licznik = 1; + while(1){ + memset(rBufor, 0, 1024); + rBufor[1] = licznik + '0'; + printf("Wysylam licznik\n"); + send(rGniazdo, rBufor, 1024, 0); + licznik += 1; + sleep(5); + } + } + } + close(gniazdo2); + } + close(gniazdo); + return 0; +} \ No newline at end of file