klient poprawka

This commit is contained in:
Jakub Adamski 2019-11-20 10:35:02 +01:00
parent a3293212cb
commit 0085add47c
3 changed files with 53 additions and 51 deletions

Binary file not shown.

View File

@ -42,21 +42,18 @@ public class App {
public void run() { //uwazac zeby nie dochodzic jednoczesnie do tej samej zmiennej (synchronize?) public void run() { //uwazac zeby nie dochodzic jednoczesnie do tej samej zmiennej (synchronize?)
Socket rSocket; Socket rSocket;
DataInputStream rDis; DataInputStream rDis;
DataOutputStream rDos;
try { try {
InetAddress rAddr = InetAddress.getByName(ipText.getText()); InetAddress rAddr = InetAddress.getByName(ipText.getText());
int rPort = Integer.parseInt(portText.getText()); int rPort = Integer.parseInt(portText.getText());
rSocket = new Socket(rAddr, rPort); // laczymy rSocket = new Socket(rAddr, rPort + 1); // laczymy
rDis = new DataInputStream(rSocket.getInputStream()); // tu bedziemy przyjmowac wiadomosci rDis = new DataInputStream(rSocket.getInputStream()); // tu bedziemy przyjmowac wiadomosci
rDos = new DataOutputStream(rSocket.getOutputStream()); messages.addElement("Listen connection ok");
rDos.write(convert("R")); //wysyłamy znaczek R messagesL.setModel(messages);
ipText.setText("OK1");
while (true) { while (true) {
//czytamy wiadomosc //czytamy wiadomosc
byte[] rec = new byte[1024]; byte[] rec = new byte[1024];
if (rDis.read(rec) > 0){ if (rDis.read(rec) > 0){
String s = new String(rec); String s = new String(rec);
//System.out.println("Serwer powiedzial: "+s);
messages.addElement(s); messages.addElement(s);
messagesL.setModel(messages); messagesL.setModel(messages);
} }
@ -70,20 +67,7 @@ public class App {
} }
}; };
try { receiveThread.start();
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 = new JTextField("Nick");
@ -97,12 +81,18 @@ public class App {
mSendB.addActionListener(new ActionListener() { mSendB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //wysylanie wiadomosci public void actionPerformed(ActionEvent e) { //wysylanie wiadomosci
try { try {
//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(myUserT.getText() + "> " + messageT.getText())); //wysylamy tDos.write(convert(myUserT.getText() + "> " + messageT.getText())); //wysylamy
messageT.setText(""); messageT.setText("");
tSocket.close(); //czy to tez trzeba?
} catch (Exception e1) {
messageT.setText("CANT SEND"); } catch (Exception ae){
e1.printStackTrace(); ae.printStackTrace();
portText.setText("CANT SEND");
} }
} }
}); });

View File

@ -10,16 +10,25 @@
int main(void) { int main(void) {
char bufor[1024], rBufor[1024], tBufor[1024]; //wiadomosc char bufor[1024], rBufor[1024], tBufor[1024]; //wiadomosc
int gniazdo, gniazdo2, rGniazdo, tGniazdo; int gniazdo, gniazdo2, rGniazdo, tGniazdo;
struct sockaddr_in adr, nadawca, rNadawca, tNadawca; struct sockaddr_in adr, rNadawca, tNadawca, adrS;
socklen_t dl = sizeof(struct sockaddr_in); socklen_t dl = sizeof(struct sockaddr_in);
gniazdo = socket(PF_INET, SOCK_STREAM, 0); gniazdo = socket(PF_INET, SOCK_STREAM, 0);
gniazdo2 = socket(PF_INET, SOCK_STREAM, 0);
//adres do odbierania widomosci od klienta
adr.sin_family = AF_INET; adr.sin_family = AF_INET;
adr.sin_port = htons(44444); //port adr.sin_port = htons(44444); //port
adr.sin_addr.s_addr = INADDR_ANY; adr.sin_addr.s_addr = INADDR_ANY;
printf("Slucham na %s:%d\n", inet_ntoa(adr.sin_addr), ntohs(adr.sin_port)); //adres do wysylania widomosci do klienta
adrS.sin_family = AF_INET;
adrS.sin_port = htons(44445); //port
adrS.sin_addr.s_addr = INADDR_ANY;
printf("Odbieram na %s:%d\n", inet_ntoa(adr.sin_addr), ntohs(adr.sin_port));
printf("Wysylam na %s:%d\n", inet_ntoa(adrS.sin_addr), ntohs(adrS.sin_port));
//konfiguracja do odbierania wiadomosci
if (bind(gniazdo, (struct sockaddr*) &adr, sizeof(adr)) < 0) { //bind if (bind(gniazdo, (struct sockaddr*) &adr, sizeof(adr)) < 0) { //bind
printf("Bind nie powiodl sie.\n"); printf("Bind nie powiodl sie.\n");
return 1; return 1;
@ -28,38 +37,41 @@ int main(void) {
printf("Listen nie powiodl sie.\n"); printf("Listen nie powiodl sie.\n");
return 1; return 1;
} }
printf("Czekam na polaczenie ...\n");
while ((gniazdo2 = accept(gniazdo, (struct sockaddr*) &nadawca, &dl)) > 0) { //odbieranie //konfiguracja do wysylania wiadomosci
memset(bufor, 0, 1024); if (bind(gniazdo2, (struct sockaddr*) &adrS, sizeof(adrS)) < 0) { //bind
recv(gniazdo2, bufor, 1024, 0); printf("Bind nie powiodl sie.\n");
printf("Wiadomosc od %s: %s\n", inet_ntoa(nadawca.sin_addr), bufor); return 1;
if (bufor[0] == 'T'){ }
tGniazdo = gniazdo2; if (listen(gniazdo2, 10) < 0) { //listen
tNadawca = nadawca; printf("Listen nie powiodl sie.\n");
if (fork() == 0){ //child wejdzie w while return 1;
while (1){ }
memset(tBufor, 0, 1024); printf("Polaczenia skonfigurowane ...\n");
recv(tGniazdo, tBufor, 1024, 0);
printf("Wiadomosc od %s: %s\n", inet_ntoa(tNadawca.sin_addr), tBufor); if (fork() == 0){ //child wejdzie w to TUTAJ WYSYLAM WIDOMOSCI co 5 sekund
} while ((tGniazdo = accept(gniazdo2, (struct sockaddr*) &tNadawca, &dl)) > 0) {
}
} else {
rGniazdo = gniazdo2;
rNadawca = nadawca;
if(fork() == 0){ //child wejdzie w while
int licznik = 1; int licznik = 1;
while(1){ while(1){
memset(rBufor, 0, 1024); memset(tBufor, 0, 1024);
rBufor[1] = licznik + '0'; tBufor[1] = licznik + '0';
printf("Wysylam licznik\n"); printf("Wysylam licznik\n");
send(rGniazdo, rBufor, 1024, 0); send(tGniazdo, tBufor, 1024, 0);
licznik += 1; licznik += 1;
sleep(5); sleep(5);
} }
}
} }
close(gniazdo2); } else { //tutaj odbieram wiadomosci
while ((rGniazdo = accept(gniazdo, (struct sockaddr*) &rNadawca, &dl)) > 0) { //odbieranie
memset(rBufor, 0, 1024);
recv(rGniazdo, rBufor, 1024, 0);
printf("Wiadomosc od %s: %s\n", inet_ntoa(rNadawca.sin_addr), rBufor);
close(rGniazdo);
}
} }
//do tego w sumie nigdy nie dojdziemy
close(gniazdo2);
close(gniazdo); close(gniazdo);
return 0; return 0;
} }