Foro de elhacker.net

Programación => Java => Mensaje iniciado por: momo1234 en 12 Mayo 2012, 16:42 pm



Título: problema server socket thread
Publicado por: momo1234 en 12 Mayo 2012, 16:42 pm
Hola todos utilizo un server multithread pero el problema es cuando hay un cliente que esta conectado funciona perfectamente pero una vez llege otro cliente y envia fichero no llege del otro cliente aqui hay mi codigi gracias.

MultiThreadedServer


Código:

package servers;



import java.net.ServerSocket;
import java.net.Socket;
import java.io.File;
import java.io.IOException;

import EspaceMateriels.bokal;

public class MultiThreadedServer implements Runnable{

    protected int          serverPort   = 1234;
    protected ServerSocket serverSocket = null;
    protected boolean      isStopped    = false;
    protected Thread       runningThread= null;

    public MultiThreadedServer(int port){
        this.serverPort = port;
    }

    public void run(){
        synchronized(this){
            this.runningThread = Thread.currentThread();
        }
        openServerSocket();
        while(! isStopped()){
            Socket clientSocket = null;
            try {
                clientSocket = this.serverSocket.accept();
            } catch (IOException e) {
                if(isStopped()) {
                    System.out.println("Server Stopped.") ;
                    return;
                }
                throw new RuntimeException(
                    "Error accepting client connection", e);
            }
            new Thread(
                new WorkerRunnable(
                    clientSocket, "Multithreaded Server")
            ).start();
        }
        System.out.println("Server Stopped.") ;
    }


    private synchronized boolean isStopped() {
        return this.isStopped;
    }

    public synchronized void stop(){
        this.isStopped = true;
        try {
            this.serverSocket.close();
        } catch (IOException e) {
            throw new RuntimeException("Error closing server", e);
        }
    }

    private void openServerSocket() {
        try {
            this.serverSocket = new ServerSocket(this.serverPort);
        } catch (IOException e) {
            throw new RuntimeException("Cannot open port 8080", e);
        }
    }
    public static void main(String...args) throws IOException
    {
    MultiThreadedServer server = new MultiThreadedServer(9000);
    new Thread(server).start();

    try {
        Thread.sleep(20 * 1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("Stopping Server");
    // server.stop();
    }
}


WorkerRunnable


Código:
package servers;

import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.net.Socket;

/**

 */
public class WorkerRunnable implements Runnable{

    protected Socket clientSocket = null;
    protected String serverText   = null;

    public WorkerRunnable(Socket clientSocket, String serverText) {
        this.clientSocket = clientSocket;
        this.serverText   = serverText;
    }

    public void run() {
        try {
        InputStream in =clientSocket.getInputStream();
             DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
             String file = dis.readUTF();
             String file1 = dis.readUTF();
             String file2 = dis.readUTF();
             int i=dis.readInt();
             if(i==1)
             {

             File directorio = new File("c:\\direccion\\"+file1+"\\"+file2+"\\");
             directorio.mkdirs();
             }
             if(file != null)
             {
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\direccion\\"+file1+"\\"+file2+"\\"+ file));
                 byte buf[] = new byte[1024];
                 int len;
                 while((len = in.read(buf)) > 0)
                 {
                     bos.write(buf, 0, len);
                 }
               in.close();
               bos.close();
             }
         
        } catch (IOException e) {
            //report exception somewhere.
            e.printStackTrace();
        }
    }
}


gracias todos.


Título: Re: problema server socket thread
Publicado por: momo1234 en 12 Mayo 2012, 17:20 pm
cuandootro cliente quiere enviar archivo tengo este mensaje


Stopping Server
java.io.EOFException
   at java.io.DataInputStream.readUnsignedShort(Unknown Source)
   at java.io.DataInputStream.readUTF(Unknown Source)
   at java.io.DataInputStream.readUTF(Unknown Source)
   at servers.WorkerRunnable.run(WorkerRunnable.java:29)
   at java.lang.Thread.run(Unknown Source)
java.io.EOFException
   at java.io.DataInputStream.readUnsignedShort(Unknown Source)
   at java.io.DataInputStream.readUTF(Unknown Source)
   at java.io.DataInputStream.readUTF(Unknown Source)
   at servers.WorkerRunnable.run(WorkerRunnable.java:30)
   at java.lang.Thread.run(Unknown Source)