wreszcie dziala
This commit is contained in:
parent
030fa4ef98
commit
a3293212cb
BIN
klient/Projekt-Final.jar
Normal file
BIN
klient/Projekt-Final.jar
Normal file
Binary file not shown.
@ -27,48 +27,36 @@
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||
<finalName>${project.artifactId}-${project.version}</finalName>
|
||||
<plugins>
|
||||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</plugin>
|
||||
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>2.5.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.2</version>
|
||||
</plugin>
|
||||
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-site-plugin</artifactId>
|
||||
<version>3.7.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<attach>false</attach>
|
||||
<appendAssemblyId>false</appendAssemblyId>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>
|
||||
Projekt.App
|
||||
</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
|
@ -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<String> usersC;
|
||||
static JButton mSendB, fSendB, connectB;
|
||||
static JList<String> messagesL;
|
||||
static DataOutputStream dos;
|
||||
static DataInputStream dis;
|
||||
static DataOutputStream tDos;
|
||||
static DefaultListModel<String> 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("<USER> Elo");
|
||||
messages.addElement("<YOU> No siema");
|
||||
messagesL = new JList<String>(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() + "<kubus";
|
||||
mess = messS.getBytes(); // konwertujemy na bajty
|
||||
dos.write(mess); //wysylamy
|
||||
tDos.write(convert(myUserT.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;
|
||||
}
|
||||
|
||||
}
|
||||
|
148
server/server.c
148
server/server.c
@ -1,148 +0,0 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <stdio.h>
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
65
server/uberProstyServer.c
Normal file
65
server/uberProstyServer.c
Normal file
@ -0,0 +1,65 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <stdio.h>
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user