GUI freeze problem

Hi everyone, im having a new problem with my sockets, i want to implement a button to stop server but because its client/server architecture it is freezed, how can i separate interface, please help :S
[207 byte] By [big_rota] at [2007-11-27 2:41:23]
# 1
> but because its client/server architecture it is freezedClient and server are two separate programs, so their GUIs also should be distinctly separated. But, really, I don't understand what are you talking about. Try post a small demo code that we can compile and run.
hiwaa at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 2

Sorry for the inconvenience, i know they are different programs, but check this out:

public void actionPerformed(ActionEvent ae){

if(ae.getSource()==boton){

int clientes=ContadorClientes.contar("conectados.txt");

int i;

Socket incoming;

System.out.println("Server Started. Waiting for connections...");

try

{

ServerSocket s = new ServerSocket(12345);

for(i=1;i<=clientes;i++)

{

incoming = s.accept();

System.out.println("New Client Connected with id " + i );

Thread t = new Servert3(incoming,i);

t.start();

}

s.close();

}

catch(Exception e)

{

System.out.println("Error: " + e);

}

}

else{

s3.terminar();

}}

when it starts it got freeze because its waiting for the clients i know but how can i make a button available? or write some code in a text area in the same GUI, thanks for ur time.

big_rota at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 3

You shouldn't do a long time-consuming task in a GUI event handler because Java GUI runs as a single thread. Delegate the task to a separate thread. See below.

public class XXXGUI{

public void actionPerformed(ActionEvent ae){

if (ae.getSource() == boton){

boton.setEnabled(false);

new Thread(new StartServer()).start();

else{

s3.terminar();

}

}

}

class StartServer implements Runnable{

int clientes;

ServerSocket s;

public void run(){

clientes = ContadorClientes.contar("conectados.txt");

try{

s = new ServerSocket(12345);

System.out.println("Server Started. Waiting for connections...");

for (int i = 1; i <= clientes; ++i){

Socket sc = s.accept();

System.out.println("New Client Connected with id " + i );

// Thread t = new Servert3(sc, i); // wrong

Servert3 t = new Servert3(sc, i);

t.start();

}

}

catch (Exception e){

e.printStackTrace();

}

}

}

Message was edited by:

hiwa

hiwaa at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 4

Hi, thanks again hiwa, the thing is that is the same program, the post of problems sending 2 files with sockets, my program is a brute force program, let me explain:

The server sends to N number of clients a file, the file encripted, and a range of numbers, the client creates from the numbers a key and encript the file, then compare the encripted file with its own encripted file, if its the same it sends the server a stirng saying "I found it" and the program stop for all clients, and the server has a stop button that if its clicked send the message to the clients stop!!! and the clients send a message saying i tryed from this number to this number, i dont know if i make my self clear, any doubt ask me, thanks a bunch hiwa, ur going to save my life.

And other thing is that the stop button is freezed because the server is waiting for the client conections, and i cant press it please help, again sorry if im a little bit anoying.

big_rota at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 5
How about socket channel may them resolve my problem?
big_rota at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 6
Nope. See my reply #3 and think about it.And beware, multiple concurrent threads can't access a same file simultaneously.
hiwaa at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 7
> stop button is freezed because the server is waiting for the client conectionsRun the server as a separate thread, then GUI won't freeze.
hiwaa at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 8

O my god hiwa i didnt realized your code up there, or maybe i just miss readed it, ok now i got the StartServer Thread, it is unfreezed, but i have another problem now, i gat the expeption in this method

import java.net.*;

class StartServer implements Runnable{

int clientes;

ServerSocket s;

contadorClientes ContadorClientes;

public StartServer(){

}

public void run(){

ContadorClientes=new contadorClientes();

clientes =ContadorClientes.contar("conectados.txt");

try{

s = new ServerSocket(12345);

System.out.println("Server Started. Waiting for connections...");

for (int i = 1; i <= clientes; ++i){

try{

Socket sc = s.accept();

System.out.println("New Client Connected with id " + i );

Servert3 t = new Servert3(sc, i);

t.start();}

catch(Exception e){System.out.println("Cant start Socket");}

}

}

catch (Exception e){

System.out.println("Error en servidor");

}

}

}

And the console output is:

Server Started. Waiting for connections...

Error en servidor

What is the problem please help again.

big_rota at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 9

Delete the try/catch in the for loop!!!!!

Your client has problem.

You are suppressing the precious information from Java runtime:

catch (Exception e){

System.out.println("Error en servidor");

}

Do like this and post the exception output:

catch (Exception e){

e.printStackTrace();

}

Message was edited by:

hiwa

Message was edited by:

hiwa

hiwaa at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 10

Ok with the stack printed now i know that the problem is that the socket is not closing correctly, this is the exception caughted:

java.net.BindException: Address already in use.

i have to close the socket, so i have to say that when the close button is pressed the socket has to close?,

big_rota at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 11

Like you say hiwa maybe the problems is the client, i posted in other post, but is not problem to post it again, thanks!!!

import java.net.*;

import java.io.*;

public class RecivirArchivoEncriptado2{

String fileCharSet;

String separator;

Socket clientsocket;

BufferedReader socketInput;

String s;

public RecivirArchivoEncriptado2(){

fileCharSet = "UTF-8";

separator = "##$##$##";

try {

clientsocket = new Socket("127.0.0.1", 12349);

socketInput = new BufferedReader

(new InputStreamReader(clientsocket.getInputStream(), fileCharSet));

}

catch (Exception e){

e.printStackTrace();

}

}

public void doFileGet(){

try{

PrintWriter fileOutput

= new PrintWriter(new FileOutputStream("archivoEncriptado.txt"), true);

while ((s = socketInput.readLine()) != null){

if (s.equals(separator)){

break;

}

fileOutput.println(s);

}

fileOutput.close();

fileOutput.flush();

System.out.println("archivo encriptado recivido");

fileOutput

= new PrintWriter(new FileOutputStream("archivoPlano.txt"), true);

while ((s = socketInput.readLine()) != null){

fileOutput.println(s);

}

fileOutput.flush();

fileOutput.close();

System.out.println("archivo plano recivido");

socketInput.close();

clientsocket.close();

}

catch(IOException e) {

System.out.println("conection refused port 12345");

}

}}

big_rota at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 12

> Ok with the stack printed now i know that the problem

> is that the socket is not closing correctly, this is

> the exception caughted:

> java.net.BindException: Address already in use.

> i have to close the socket, so i have to say that

> when the close button is pressed the socket has to

> close?,

In my reply #3 implementation, the ServerSocket is instantiated only once. So you should never get BindException. If you have to instaitiate it multiple times, you have to write like this:

t.start();

}

s.close();

or,

try{

if (s == null){

s = new ServerSocket(12345);

}

Message was edited by:

hiwa

hiwaa at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 13

Ok, yes i have to start it once, but because i have to prove the code, i have to run and run it, tnanks hiwa, now thats not the problem, i think the problem now is the client, which code is there, again its getting the .txt empty, why can it be happening this, if i don't use the thread, it works ok, but now with the threads the client freezes!!! **** i used this word a lot in this program lol, but i don't know why, please hiwa, and any one that want to, i need ur help, thanks

big_rota at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 14

Hey, I don't see your client launching code yet. If it uses multi-threading, your client RecivirArchivoEncriptado2 has to run as a thread. And those threads should not access same file(s). Don't show your code bit by bit. Let us know the whole structure of your application.

And your sever, multiple Servert3 threads, also should not access same file(s).

hiwaa at 2007-7-12 3:05:09 > top of Java-index,Java Essentials,Java Programming...
# 15

Ok, lets see, the fact here is that i have to send the same files to different clients,

ok here is all about my program:

The application is a server that reads a file with the number of clients that

will connect to them, thats why the number off threads created are clients.number(),

then when a client is connected the Server sends an

encrypted file, a plain text file, 2 numbers (a range of numbers like

1-100000).

Then the client uses a class that make the number to a string, encode it

and encrypt the file with the encoded string, then it compares the file and if

it is the same it has to send a message to the server like ("I have the key, it

is HIWA")

and the server has to send a message to the other clients and say them

(Ok stop the process). Thats why i need to send the same file to each

client, any code u need i will happily send it to you. And when the stop

button is pressed, it sends a message to all clients and tell them ("stop the

process"), and the clients respond ("I tried keys from x to y"), i know is a

kind of bad explanation but any doubt i will answer :).

big_rota at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 16
Should i send different files with the same information?, i think is not the solution, so what do you think is the option.
big_rota at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 17

here is my client threaded socket

import java.net.*;

import java.io.*;

public class recivirArchivoEncriptado2 implements Runnable{

String fileCharSet;

String separator;

Socket clientsocket;

BufferedReader socketInput;

String s;

public recivirArchivoEncriptado2(){

fileCharSet = "UTF-8";

separator = "##$##$##";

try {

clientsocket = new Socket("127.0.0.1", 1242);

socketInput = new BufferedReader

(new InputStreamReader(clientsocket.getInputStream(), fileCharSet));

}

catch (Exception e){

e.printStackTrace();

}

}

public void run(){

try{

PrintWriter fileOutput

= new PrintWriter(new FileOutputStream("archivoEncriptado.txt"), true);

fileOutput.flush();

System.out.println("Conectado");

while ((s = socketInput.readLine()) != null){

if (s.equals(separator)){

System.out.println("Conectado");

break;

}

fileOutput.println(s);

}

fileOutput.flush();

fileOutput.close();

System.out.println("archivo encriptado recivido");

fileOutput

= new PrintWriter(new FileOutputStream("archivoPlano.txt"), true);

while ((s = socketInput.readLine()) != null){

fileOutput.println(s);

}

fileOutput.flush();

fileOutput.close();

System.out.println("archivo plano recivido");

socketInput.close();

clientsocket.close();

System.out.println("Archivo recivido con exito");

}

catch(IOException e) {

System.out.println("conection refused port 1234");

}

}}

and my clients gui:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

import java.lang.*;

import java.net.*;

import java.io.Writer;

public class GUI extends JFrame implements ActionListener{

public JTextField texto;

public JTextArea texto2;

recivirArchivoEncriptado2 e;

public JButton boton,boton2;

public JPanel p1,p2,p3,p4,p51;

public JFrame frame2;

public GUI(){

frame2=new JFrame();

frame2.setSize(200,200);

p51=new JPanel();

p51.setPreferredSize(new Dimension(200,200));

texto=new JTextField(10);

texto2=new JTextArea(10,10);

boton=new JButton("Plano");

boton2=new JButton("Encriptado");

boton.addActionListener(this);

boton2.addActionListener(this);

p1=new JPanel();

p2=new JPanel();

p3=new JPanel();

p4=new JPanel();

p1.setPreferredSize(new Dimension(900,600));

p2.setPreferredSize(new Dimension(200,600));

p3.setPreferredSize(new Dimension(300,600));

p4.setPreferredSize(new Dimension(200,600));

}

public void actionPerformed(ActionEvent ae){

Thread t=new Thread(new recivirArchivoEncriptado2());

t.start();

}

private void createAndShowGUI() {

setTitle("Proyecto Final");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel emptyLabel = new JLabel("");

p51.add(boton2);

p51.setVisible(true);

p2.add(texto2);

boton.setVisible(true);

boton2.setVisible(true);

p1.setVisible(true);

p2.setVisible(true);

boton.setVisible(true);

getContentPane().add(p1);

p1.add(p2);

p1.add(p3);

p1.add(p4);

pack();

setVisible(false);

frame2.getContentPane().add(p51);

frame2.pack();

frame2.setVisible(true);

}

public static void main(String[] args) {

final GUI g=new GUI();

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

g.createAndShowGUI();

}

});

}

}

THanks

big_rota at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 18
> Should i send different files with the same> information?, i think is not the solution, so what> do you think is the option.Multiple threads running on a same host can't access same file(s) simultaneously.You need synchronization on those file
hiwaa at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 19

Ok, i put

public synchronized void run()

on both sides client and server, and still doesn't work, so I decided to upload all my file so if any doubt there it is the link is:

http://files-upload.com/198900/workspace3.zip.html

Anyway questions i could answer, thanks again

big_rota at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 20

now that i've synchronized the server, when i close the application i can reuse the socket, but the problem is in my client i guess, it connects with the server, starts the print writer, starts the archivoEncriptado.txt and let it empty, i think is the same problem about the threads reading the same file, how can i handle this problem

big_rota at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 21
> threads reading the same fileJust stop it. You should do file I/O sequentially, not concurrently.Queue the clients and dequeue them one by one for doing file I/O.> i've synchronized the serverYou have not synchronized the file I/O, I suspect.
hiwaa at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 22

Ok this time i dindn't get it at all, stop reading the same file, queue the clients

something like

Queue a;

for (int i = 1; i <= clientes; ++i){

Socket sc = s.accept();

System.out.println("New Client Connected with id " + i );

Servert3 t = new Servert3(sc, i);

a.append(t);

}}

or i don't get it

big_rota at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 23

And what do you mean, synchronize the file I/O, i just can synchronize methods, do you mean something like

public synchronized void encriptado(String a1){try{

fileInput= new BufferedReader(new InputStreamReader

(new FileInputStream(a1), fileCharSet));

while ((s = fileInput.readLine()) != null){

socketOutput.println(s);

}

fileInput.close();

socketOutput.println(separator);

socketOutput.flush();

System.out.println("archivo encriptado enviado");

socketOutput.flush();

System.out.println("archivo plano enviado");

}catch(Exception e){e.printStackTrace();

}

}

big_rota at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 24

If files are same for every client, use this instead of your old Servert3:

(Besides, your Servert3 run() method signature is wrong!)

Client program RecivirArchivoEncriptado2, not recivirArchivoEncriptado2, should work as is.

import java.io.*;

import java.net.*;

public class NewServert3 extends Thread{

String fileCharSet;

String separator;

String s;

String archivo1, archivo2;

ServerSocket serversocket;

public Socket incoming;

int counter;

Chronometer cm;

public NewServert3(Socket i, int c, String a1, String a2){

archivo1 = a1;

archivo2 = a2;

fileCharSet = "UTF-8";

separator = "##$##$##";

cm = new Chronometer();

cm.start();

incoming = i;

counter = c;

}

public void run(){

doFileSend();

}

public synchronized void doFileSend(){

try{

PrintWriter socketOutput

= new PrintWriter(incoming.getOutputStream(), true);

BufferedReader fileInput = new BufferedReader(new InputStreamReader

(new FileInputStream(archivo1), fileCharSet));

while ((s = fileInput.readLine()) != null){

socketOutput.println(s);

}

fileInput.close();

socketOutput.println(separator);

socketOutput.flush();

System.out.println("archivo encriptado enviado");

fileInput = new BufferedReader(new FileReader(archivo2));

while ((s = fileInput.readLine()) != null){

socketOutput.println(s);

}

System.out.println("archivo plano enviado");

socketOutput.flush();

System.out.println("archivo plano enviado");

fileInput.close();

socketOutput.close();

incoming.close();

cm.stop();

System.out.print((int)cm.htime() + ":");

System.out.print((int)cm.mtime() + ":");

System.out.print((int)cm.stime() + ":");

}

catch (IOException e) {

e.printStackTrace();}

}

public void terminar(){

try{

incoming.close();

cm.stop();

System.out.print((int)cm.htime()+":");

System.out.print((int)cm.mtime()+":");

System.out.print((int)cm.stime()+":");

}

catch(Exception e){

e.printStackTrace();

}

}

}

Message was edited by:

hiwa

hiwaa at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 25
Wow!! this worked find, now i have another question :S, if i want to send some thing like images or videos, do i use bytes or what? haha sorry but i wont stop until i finish sorry.
big_rota at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 26
> images or videosWrap the socket streams with BufferedInput/OutputStream.
hiwaa at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 27

Oh men now i have another problem, the file stuff is OK, just perfect, but now i want to send 2 integers to the clients, both are generated at runtime, but the client freezes, again, look my code:

public void dogetInt(){

try{

DataInputStream dis=new DataInputStream(clientsocket.getInputStream());

System.out.println(dis.readLong());

System.out.println(dis.readLong());

dis.close();

}

catch(Exception e){

e.printStackTrace();

}

at the recivirArchivoEncriptado2

public void actionPerformed(ActionEvent ae){

e.dogetInt();

e.doFileGet();

}

at the clients GUI.

import java.io.*;

import java.net.*;

public class NewServert3 extends Thread{

String fileCharSet;

String separator;

String s;

String archivo1, archivo2;

ServerSocket serversocket;

public Socket incoming;

int counter ;

long ini, finale;

public NewServert3(Socket i, int c, String a1, String a2,long inicio,long finnal){

archivo1 = a1;

archivo2 = a2;

ini=inicio;

finale=finnal;

fileCharSet = "UTF-8";

separator = "##$##$##";

Chronometer.start();

incoming = i;

counter = c;

}

public void run(){

doSendRange();

doFileSend();

}

public synchronized void doSendRange(){

try{

DataOutputStream dos = new DataOutputStream(incoming.getOutputStream());

dos.writeLong(ini);

dos.writeLong(finale);}

catch(Exception e){

e.printStackTrace();

}

}

public synchronized void doFileSend(){

try{

PrintWriter socketOutput

= new PrintWriter(incoming.getOutputStream(), true);

BufferedReader fileInput = new BufferedReader(new InputStreamReader

(new FileInputStream(archivo1), fileCharSet));

while ((s = fileInput.readLine()) != null){

socketOutput.println(s);

}

fileInput.close();

socketOutput.println(separator);

socketOutput.flush();

System.out.println("archivo encriptado enviado");

fileInput = new BufferedReader(new FileReader(archivo2));

while ((s = fileInput.readLine()) != null){

socketOutput.println(s);

}

socketOutput.flush();

fileInput.close();

socketOutput.close();

incoming.close();

Chronometer.stop();

System.out.print((int)Chronometer.htime() + ":");

System.out.print((int)Chronometer.mtime() + ":");

System.out.print((int)Chronometer.stime() + ":");

}

catch (IOException e) {

e.printStackTrace();}

}

public void terminar(){

try{

incoming.close();

Chronometer.stop();

System.out.print((int)Chronometer.htime() + ":");

System.out.print((int)Chronometer.mtime() + ":");

System.out.print((int)Chronometer.stime() + ":");

}

catch(Exception e){

e.printStackTrace();

}

}

}

the server code, thanks.

big_rota at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 28

Don't call multiple synchronized methods in multi-threaded execution enviroment.They are not guaranteed to be called tandem. Do like this:

public synchronized void doFileSend(){

try{

DataOutputStream dos = new DataOutputStream(incoming.getOutputStream());

dos.writeLong(ini);

dos.writeLong(finale);

dos.flush();

PrintWriter socketOutput

= new PrintWriter(incoming.getOutputStream(), true);

BufferedReader fileInput = new BufferedReader(new InputStreamReader

(new FileInputStream(archivo1), fileCharSet));

while ((s = fileInput.readLine()) != null){

....etc.....

This is also wrong. Use Chronometer cm = new Chronometer(); and instance methods that access instance variables of the 'cm', not static ones. Multiple threads should not access non-synchronized static methods nor non-synchronized methods of a single object.

Chronometer.stop();

System.out.print((int)Chronometer.htime() + ":");

System.out.print((int)Chronometer.mtime() + ":");

System.out.print((int)Chronometer.stime() + ":");

This is also wrong. If you close it here, you can't do nothing hereafter. Don't write a separate method dogetInt(). A single socket io method should suffice.

dis.close(); // why close ?!?!

hiwaa at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 29
Thanks hiwa i see my mistake maybe 2 late, now thats not the problem, i get file and int, later i post something else :D, i think about the stoping button, thanks
big_rota at 2007-7-21 20:30:30 > top of Java-index,Java Essentials,Java Programming...
# 30

Yes. This is also wrooong. In the GUI event handler, you should dispatch a separate thread for long and time-consuming task e.g. I/O.

public void actionPerformed(ActionEvent ae){

// WRONG

e.dogetInt();

e.doFileGet();

}

hiwaa at 2007-7-21 20:30:35 > top of Java-index,Java Essentials,Java Programming...
# 31

Ok thats solved hiwa, i threaded the long issue, now the last problem id this:

in the server side there is a button that if clicked say to all the clients to stop the process and then close the client, and the other thing, if any client find the right "key" to the server and all clients will stop the process they are doing and close port, how could i do this, thanks

Thanks again man!!

big_rota at 2007-7-21 20:30:35 > top of Java-index,Java Essentials,Java Programming...
# 32

> button that if clicked say to all the clients to stop

In the event handler of the button, call shutdownOutput() on every accept()ed client sockets. Then, client read ends with an EOF or EOFException.

> if any client find the right "key" to the server

What is it? What do you mean by 'find'?

hiwaa at 2007-7-21 20:30:35 > top of Java-index,Java Essentials,Java Programming...
# 33

Well my friend hiwa, with "find" i meant transform the int to a string, encrypt the file with it, compare with the encrypted file and get true, i mean, that the client get the key with i for loop, mm i don't know if its clear, i mean the server and all the clients are searching for a key, thats the purpose of the program, thanks

big_rota at 2007-7-21 20:30:35 > top of Java-index,Java Essentials,Java Programming...
# 34

> Well my friend hiwa, with "find" i meant transform

> the int to a string, encrypt the file with it,

> compare with the encrypted file and get true, i mean,

> that the client get the key with i for loop, mm i

> don't know if its clear, i mean the server and all

> the clients are searching for a key, thats the

> purpose of the program, thanks

How does the server know that?

hiwaa at 2007-7-21 20:30:35 > top of Java-index,Java Essentials,Java Programming...
# 35

Don't know if i didn't explain my point or if you're just asking if you understand right my point, what i meant is:

server sends 2 files and 2 ints to the clients

the 2 ints are a range of number i meant the first client connected receives from 100000 to 200000 lets say, the server uses range 1 to 99999, the next client 2000001 to 300000 etc... each client and the server uses a class that with a loop, lets say the key is HIWA so the number is 1560000 (not really just an example), so the client 1 that found that the key is 15600000, at the moment the client realize that, tell the server hey i found the key (lets said with a non null string), so the server said, ok every one stops the loop!! and then the server prints "Client 1 found the key its HIWA" and stop the program, maybe with a bool that ask if an OutputStream is empty or a method, i don't have a clue lol

big_rota at 2007-7-21 20:30:35 > top of Java-index,Java Essentials,Java Programming...
# 36
Well, then, close all the sockets connected to the server. Next I/O try on the client will get an exception.
hiwaa at 2007-7-21 20:30:35 > top of Java-index,Java Essentials,Java Programming...
# 37

I'M having another weird problem pliz man help,

the file plain.txt is received with like 5 white spaces i don't know why, i did what you said, here is code of that:

CLIENT SIDE:

public void doFileGet(){

try{ PrintWriter fileOutput

= new PrintWriter(new FileOutputStream("archivoEncriptado.txt"), true);

fileOutput.flush();

System.out.println("Conectado");

while ((s = socketInput.readLine()) != null){

if (s.equals(separator)){

System.out.println("Conectado");

break;

}

fileOutput.println(s);

}

fileOutput.flush();

fileOutput.close();

System.out.println("archivo encriptado recivido");

fileOutput

= new PrintWriter(new FileOutputStream("archivoPlano.txt"), true);

while ((s = socketInput.readLine()) != null){

fileOutput.println(s);

}

fileOutput.flush();

fileOutput.close();

System.out.println("archivo plano recivido");

socketInput.close();

clientsocket.close();

System.out.println("Archivo recivido con exito");

}

catch(IOException e) {

System.out.println("conection refused port 1234");

SERVER SIDE :

public synchronized void doFileSend(){

try{

PrintWriter socketOutput

= new PrintWriter(incoming.getOutputStream(), true);

BufferedReader fileInput = new BufferedReader(new InputStreamReader

(new FileInputStream(archivo1), fileCharSet));

while ((s = fileInput.readLine()) != null){

socketOutput.println(s);

}

fileInput.close();

socketOutput.println(separator);

socketOutput.flush();

System.out.println("archivo encriptado enviado");

fileInput = new BufferedReader(new FileReader(archivo2));

while ((s = fileInput.readLine()) != null){

socketOutput.println(s);

}

socketOutput.flush();

fileInput.close();

socketOutput.close();

incoming.close();

Chronometer.stop();

System.out.print((int)Chronometer.htime() + ":");

System.out.print((int)Chronometer.mtime() + ":");

System.out.print((int)Chronometer.stime() + ":");

}

catch (IOException e) {

e.printStackTrace();}

}

Both are threaded, If this not the problem i can put my code of send an integer, thanks

big_rota at 2007-7-21 20:30:35 > top of Java-index,Java Essentials,Java Programming...
# 38
Do all the I/O in a single method, both on client and the client-handler thread on the server. You haven't take my advice and got error and error. Why you don't take my advice? You are making simple things unnecessarily complex.
hiwaa at 2007-7-21 20:30:35 > top of Java-index,Java Essentials,Java Programming...
# 39
> the file plain.txt is received with like 5 white spaces What change did you do on the current code?
hiwaa at 2007-7-21 20:30:35 > top of Java-index,Java Essentials,Java Programming...
# 40

Do you meant to make all the DataOutputStream and DataInputStream in the same method?.

Here is my code with the changes.

import java.io.*;

import java.net.*;

public class NewServert3 extends Thread{

String fileCharSet;

String separator;

String s;

String archivo1, archivo2;

ServerSocket serversocket;

public Socket incoming;

int counter ,key;

long ini, finale;

Chronometer cr;

public NewServert3(Socket i, int c, String a1, String a2,long inicio,long finnal,int llave){

cr=new Chronometer();

key=llave;

archivo1 = a1;

archivo2 = a2;

ini=inicio;

finale=finnal;

fileCharSet = "UTF-8";

separator = "##$##$##";

Chronometer.start();

incoming = i;

counter = c;

}

public void run(){

doSendRange();

doFileSend();

}

public synchronized void doSendRange(){

try{

DataOutputStream dos = new DataOutputStream(incoming.getOutputStream());

dos.writeLong(ini);

dos.flush();

dos.writeLong(finale);

dos.flush();

dos.writeInt(key);}

catch(Exception e){

e.printStackTrace();

}

}

public synchronized void doFileSend(){

try{

PrintWriter socketOutput

= new PrintWriter(incoming.getOutputStream(), true);

BufferedReader fileInput = new BufferedReader(new InputStreamReader

(new FileInputStream(archivo1), fileCharSet));

while ((s = fileInput.readLine()) != null){

socketOutput.println(s);

}

fileInput.close();

socketOutput.println(separator);

socketOutput.flush();

System.out.println("archivo encriptado enviado");

fileInput = new BufferedReader(new FileReader(archivo2));

while ((s = fileInput.readLine()) != null){

socketOutput.println(s);

}

socketOutput.flush();

fileInput.close();

socketOutput.close();

incoming.close();

Chronometer.stop();

System.out.print((int)Chronometer.htime() + ":");

System.out.print((int)Chronometer.mtime() + ":");

System.out.print((int)Chronometer.stime() + ":");

}

catch (IOException e) {

e.printStackTrace();}

}

public void terminar(){

try{

incoming.close();

Chronometer.stop();

System.out.print((int)Chronometer.htime() + ":");

System.out.print((int)Chronometer.mtime() + ":");

System.out.print((int)Chronometer.stime() + ":");

}

catch(Exception e){

e.printStackTrace();

}

}

}

The other one.

import java.net.*;

import java.io.*;

public class recivirArchivoEncriptado2 implements Runnable{

String fileCharSet;

String separator;

Socket clientsocket;

BufferedReader socketInput;

String s;

long ini,fin;

pruebaLlaves p;

public recivirArchivoEncriptado2 (){

fileCharSet = "UTF-8";

separator = "##$##$##";

try {

clientsocket = new Socket("127.0.0.1", 11155);

socketInput = new BufferedReader

(new InputStreamReader(clientsocket.getInputStream(), fileCharSet));

}

catch (Exception e){

e.printStackTrace();

}

}

public void doFileGet(){

try{

PrintWriter fileOutput

= new PrintWriter(new FileOutputStream("a1.txt"), true);

while ((s = socketInput.readLine()) != null){

if (s.equals(separator)){

break;

}

fileOutput.println(s);

}

fileOutput.close();

fileOutput.flush();

System.out.println("archivo encriptado recivido");

fileOutput

= new PrintWriter(new FileOutputStream("a2.txt"), true);

while ((s = socketInput.readLine()) != null){

fileOutput.print(s);

}

fileOutput.flush();

fileOutput.close();

System.out.println("archivo plano recivido");

socketInput.close();

clientsocket.close();

}

catch(IOException e) {

System.out.println("conection refused port 12345");

}

}

public void dogetInt(){

try{

DataInputStream dis=new DataInputStream(clientsocket.getInputStream());

ini=new Long(dis.readLong()).longValue();

fin=new Long(dis.readLong()).longValue();

System.out.println("Recivi llave de "+ini+"hasta"+fin);

}

catch(Exception e){

e.printStackTrace();

}

}

public void revisar(){

p=new pruebaLlaves(new Long("293153950000").longValue(),new Long("293153950020").longValue(),"a1.txt","a2.txt");

Thread t=new Thread(p);

t.start();

}

public void run(){

dogetInt();

doFileGet();

revisar();

}

}

the change you mean is:

public void doFileGet(){

try{

PrintWriter fileOutput

= new PrintWriter(new FileOutputStream("a1.txt"), true);

while ((s = socketInput.readLine()) != null){

if (s.equals(separator)){

break;

}

fileOutput.println(s);

}

fileOutput.close();

fileOutput.flush();

System.out.println("archivo encriptado recivido");

fileOutput

= new PrintWriter(new FileOutputStream("a2.txt"), true);

while ((s = socketInput.readLine()) != null){

fileOutput.print(s);

}

fileOutput.flush();

fileOutput.close();

System.out.println("archivo plano recivido");

DataInputStream dis=new DataInputStream(clientsocket.getInputStream());

ini=new Long(dis.readLong()).longValue();

fin=new Long(dis.readLong()).longValue();

System.out.println("Recivi llave de "+ini+"hasta"+fin);

socketInput.close();

clientsocket.close();

}

catch(IOException e) {

System.out.println("conection refused port 12345");

}

public void run(){

doFileget();

}

}

big_rota at 2007-7-21 20:30:35 > top of Java-index,Java Essentials,Java Programming...
# 41
When u said hiwa use different threads for io, you meant 1 that get the IntRange and other that get the files?
big_rota at 2007-7-21 20:30:35 > top of Java-index,Java Essentials,Java Programming...
# 42
> When u said hiwa use different threads for io, you> meant 1 that get the IntRange and other that get the> files?Nope.
hiwaa at 2007-7-21 20:30:35 > top of Java-index,Java Essentials,Java Programming...
# 43

This is my last post in this thread. You must learn Java language and Java programming basics before anything else.

Single I/O methods should be like this:

public synchronized void doFileSend(){

try{

DataOutputStream dos = new DataOutputStream(incoming.getOutputStream());

dos.writeLong(ini);

dos.writeLong(finale);

dos.writeInt(key);

dos.flush();

PrintWriter socketOutput

= new PrintWriter(incoming.getOutputStream(), true);

BufferedReader fileInput = new BufferedReader(new InputStreamReader

(new FileInputStream(archivo1), fileCharSet));

while ((s = fileInput.readLine()) != null){

socketOutput.println(s);

}

fileInput.close();

socketOutput.println(separator);

socketOutput.flush();

System.out.println("archivo encriptado enviado");

fileInput = new BufferedReader(new FileReader(archivo2));

while ((s = fileInput.readLine()) != null){

socketOutput.println(s);

}

socketOutput.flush();

fileInput.close();

socketOutput.close();

incoming.close();

//Chronometer.stop(); // stop using class methods here in a thread

//System.out.print((int)Chronometer.htime() + ":");

//System.out.print((int)Chronometer.mtime() + ":");

//System.out.print((int)Chronometer.stime() + ":");

}

catch (IOException e) {

e.printStackTrace();

}

}

public void terminar(){

try{

incoming.close();

// Chronometer.stop();

// System.out.print((int)Chronometer.htime() + ":");

// System.out.print((int)Chronometer.mtime() + ":");

// System.out.print((int)Chronometer.stime() + ":");

}

catch(Exception e){

e.printStackTrace();

}

}

--

long ini, fin;

int key;

public void doFileGet(){

try{

DataInputStream dis = new DataInputStream(clientsocket.getInputStream());

ini = dis.readLong();

fin = dis.readLong();

key = dis.readInt();

/* DON'T DO THIS IN THE CONSTRUCTOR */

socketInput = new BufferedReader

(new InputStreamReader(clientsocket.getInputStream(), fileCharSet));

PrintWriter fileOutput

= new PrintWriter(new FileOutputStream("a1.txt"), true);

while ((s = socketInput.readLine()) != null){

if (s.equals(separator)){

break;

}

fileOutput.println(s);

}

fileOutput.close();

System.out.println("archivo encriptado recivido");

fileOutput

= new PrintWriter(new FileOutputStream("a2.txt"), true);

while ((s = socketInput.readLine()) != null){

fileOutput.print(s);

}

fileOutput.close();

System.out.println("archivo plano recivido");

socketInput.close();

clientsocket.close();

}

catch(IOException e) {

e.printStackTrace();

}

}

hiwaa at 2007-7-21 20:30:36 > top of Java-index,Java Essentials,Java Programming...
# 44
Ok hiwa, is curious you said that i don't know java basics, even i am jscp, and actually i was going to do that, is not a problem, but if you don't want to help me is ok, thanks for all the other help hiwa, good luck.
big_rota at 2007-7-21 20:30:36 > top of Java-index,Java Essentials,Java Programming...