Program Klient-Serwer

Strażak po godzinach
Awatar użytkownika

Topic author
damian_osp
Posty: 571
https://dekodeco.com.pl/producent-mebli-na-wymiar-w-warszawie-nowoczesne-kuchnie/
Rejestracja: ndz 24 kwie 2011, 10:15
OSP (gm., woj.): OSP Posada Zarszyńska
Kontakt:
Status: Offline

Program Klient-Serwer

Post autor: damian_osp »

Koledzy czy ktoś kuma język JAVA?? Mam projekt do zrobienie i mam problem.

Uruchamiam serwer
Serwer czeka na klienta
Uruchamiam Klienta
Serwer cały czas wysyła a klient cały czas odbiera
Problem w tym ze serwer wysyła tylko print-screena którego zrobił za pierwszym razem a następne print-screeny klient nie odbiera.
CO JEST ŹLE??

Wysyłanie:

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class Wysylanie extends Thread {

public final static int defaultPort = 6789;
ServerSocket theConnection;
static int numberOfThreads=10;


public static void main(String[] args) throws IOException, AWTException{

int port = defaultPort;

try{
port =Integer.parseInt(args[0]);
} catch(Exception ex){}

if (port<=0 || port >= 65536) port=defaultPort;
try{

ServerSocket socket = new ServerSocket(port);
for (int i=0; i<numberOfThreads; i++){


Wysylanie wys = new Wysylanie(socket);
wys.start();

}
}catch (IOException ex){System.err.println(ex);}
}
private Rectangle Rectangle;

public Wysylanie(ServerSocket socket){
theConnection = socket;
}


public void run(){

FileInputStream fin = null;
DataOutputStream out = null;
Socket s = null;
File file = null;
Robot robot = null;
Dimension a = null;
BufferedImage zrzut = null;
try {

s = theConnection.accept();
out = new DataOutputStream(s.getOutputStream());
file = new File("img/zrzut.jpg");
fin = new FileInputStream(file);
robot = new Robot();
a = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle = new Rectangle(a);

System.out.println("Wykonano screena");

while(true){


zrzut = robot.createScreenCapture(Rectangle);
file = new File("img/zrzut.jpg");
ImageIO.write(zrzut, "jpg",file);
fin = new FileInputStream(file);

System.out.println("Wysylanie...");
byte[] readData = new byte[1024];
int i;
while((i = fin.read(readData)) != -1)
out.write(readData, 0, i);
System.out.println("Wyslano!");

} //koniec while(true)
}

catch (AWTException ex) {
Logger.getLogger(Wysylanie.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Wysylanie.class.getName()).log(Level.SEVERE, null, ex);

}
finally{
try {
out.close();
s.close();
System.out.println("Zamknieto polaczenie");
} catch (IOException ex) {
Logger.getLogger(Wysylanie.class.getName()).log(Level.SEVERE, null, ex);
}

}
}
}





Odbieranie:



import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Odbieranie{

public static void main(String[] args) throws IOException{
File file=null;
Socket socket = null;
DataInputStream in = null;
FileOutputStream fout = null;
byte[] readData = new byte[1024];
int i=0;

System.out.println("Tworzenie strumienia...");
file = new File("img/image.jpg");
socket = new Socket("localhost", 6789);

in = new DataInputStream(socket.getInputStream());
fout = new FileOutputStream(file);

while(true){

try{

while((i = in.read(readData)) != -1){

System.out.println("zapisywanie..." + i);
fout.write(readData, 0, i);
}

}
finally{

fout.flush();
fout.close();
in.close();
socket.close();
System.out.println("Zamknieto polaczenie");
}
}

}

}
Bogu na chwałę, ludziom na pożytek