guardar archivo desde un socket

Que onda! estoy trabajando en un server basado en sockets, de hecho ya le tengo y corre bien pero lo que no se es como incrustar un codigo y adecuar
el codigo ya existente
Aqui esta el server [corre,escucha, acepta conexiones y envia mensajitos]
[CODE]
package servidor;

import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Servidor implements Runnable{
private static final Logger Logger = LoggerFactory.getLogger(Servidor.class);
private ServerSocket server;
private List atendentes;

private boolean inicializado;
private boolean ejecutando;
private Thread thread;

public Servidor(int porta) throws Exception {
atendentes = new ArrayList();
inicializado = false;
ejecutando = false;
open(porta);
}

private void open(int porta) throws Exception{
Logger.info("Petición de conexión");
server = new ServerSocket(porta);
inicializado = true;
}

private void close(){
Logger.info("Cierre de conexión");
for (Atendente atendente : atendentes){
try{
atendente.stop();
}
catch(Exception e){
System.out.println(e);
}

}
try {
server.close();
}
catch(Exception e){
System.out.println(e);
}
server = null;
inicializado = false;
ejecutando = false;

thread = null;

}

public void start(){
Logger.info("Servidor up and running");
if (!inicializado || ejecutando) {
return;
}
ejecutando = true;
thread = new Thread(this);
thread.start();
}
public void stop() throws Exception {
Logger.info("Cierre de server y threads");
ejecutando = false;
thread.join();
}

public void run(){
System.out.println("Esperando conexiones.");
while (ejecutando){
try {
server.setSoTimeout(2500);
Socket socket = server.accept();
System.out.println("Conexión establecida.");

Atendente atendente = new Atendente(socket);
atendente.start();
atendentes.add(atendente);
}
catch (SocketTimeoutException e) {

}
catch (Exception e) {
System.out.println(e);
break;
}
}
close();
}
public static void main(String[] args) throws Exception {
Logger.info("Inicio de servidor");
System.out.println("Iniciando server.");
Servidor servidor = new Servidor(2525);
servidor.start();
System.out.println("Presione Enter para cerrar el server");
new Scanner(System.in).nextLine();
System.out.println("Cerrando servidor");
servidor.stop();
Logger.info("Fin de servidor =p" );
}
}

[/CODE]

aqui ta la clase que apoya a server
[CODE]
package servidor;

import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.SocketTimeoutException;

public class Atendente implements Runnable {

private Socket socket;

private BufferedReader in;
private PrintStream out;

private boolean inicializado;
private boolean ejecutando;

private Thread thread;

public Atendente(Socket socket) throws Exception {
this.socket = socket;
this.inicializado = false;
this.ejecutando = false;

open();

}

private void open() throws Exception {
try{
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintStream(socket.getOutputStream());
inicializado = true;
}
catch(Exception e){
close();
throw e;

}
}
private void close() {

if (in != null){
try{
in.close();
}
catch (Exception e){
System.out.println(e);
}
}
if (out != null){
try{
out.close();
}
catch (Exception e){
System.out.println(e);
}
}

try{
socket.close();
}
catch (Exception e){
System.out.println(e);
}
in= null;
out = null;
socket= null;
inicializado = false;
ejecutando = false;
thread = null;

}

public void start(){
if(!inicializado || ejecutando){
return;
}
ejecutando = true;
thread = new Thread(this);
thread.start();

}

public void stop() throws Exception{
ejecutando = false;
thread.join();

}
public void run(){
while (ejecutando){
try {
socket.setSoTimeout(2500);
String mensaje = in.readLine();
System.out.println("Mensaje recibido de los clientes [" +
socket.getInetAddress().getHostName() +
":"+
socket.getPort()+
"]:"+
mensaje);
if ("Fin".equals(mensaje)) {
break;
}
out.println(mensaje);
}
catch (SocketTimeoutException e){

}
catch (Exception e){
System.out.println(e);
break;
}

}
System.out.println("Cerrando conexión");
close();

}

}
[/CODE]
aqui el cliente
[CODE]
package cliente;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Scanner;

public class Cliente implements Runnable{
private Socket socket;
private BufferedReader in;
private PrintStream out;
private boolean inicializado;
private boolean ejecutando;
private Thread thread;
public Cliente(String endereco, int porta) throws Exception {
inicializado = false;
ejecutando = false;

open(endereco,porta);

}
private void open(String endereco, int porta) throws Exception {
try {
socket = new Socket(endereco,porta);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out= new PrintStream(socket.getOutputStream());
inicializado = true;
}
catch (Exception e){
System.out.println(e);
close();
throw e;

}

}

private void close(){
if (in != null){
try {
in.close();
}
catch (Exception e){
System.out.println(e);

}
}
if (out != null){
try {
out.close();
}
catch (Exception e){
System.out.println(e);

}
}
if (socket != null){
try {
socket.close();
}
catch (Exception e){
System.out.println(e);

}
}
in = null;
out = null;
socket = null;
inicializado = false;
ejecutando = false;

thread = null;

}

public void start(){
if(!inicializado || ejecutando){
return;
}
ejecutando = true;
thread = new Thread(this);
thread.start();

}

public void stop() throws Exception {
ejecutando = false;
if (thread != null) {
thread.join();
}
}

public boolean isEjecutando(){
return ejecutando;
}

public void send(String mensaje){
out.println(mensaje);
}
public void run(){
while (ejecutando){
try {
socket.setSoTimeout(2500);
String mensaje = in.readLine();
if (mensaje == null){
break;
}
System.out.println(
"Mensaje enviado por el servidor: " + mensaje);

}
catch (SocketTimeoutException e){
}
catch (Exception e){
System.out.println(e);
break;
}
}
close();

}

public static void main (String[] args) throws Exception {
System.out.println("Iniciando cliente...");
System.out.println("Iniciando conexión...");
Cliente cliente = new Cliente("localhost",2525);
System.out.println("Conexión establecida");
cliente.start();

Scanner scanner = new Scanner(System.in);

while(true) {
System.out.print("Escriba su mensaje:");
String mensaje = scanner.nextLine();
if(!cliente.isEjecutando()){
break;
}

cliente.send(mensaje);

if ("Fin".equals(mensaje)){
break;
}

}
System.out.println("Cerrando cliente");
cliente.stop();
}
}
[/CODE]
entonces que? que no se como meter este codigito ni en donde (server o cliente)
[CODE]import java.io.*;
import java.util.*;
public class CuentaPalabras
{
public static void main(String[] args) throws IOException
{
BufferedReader stdin =
new BufferedReader(new InputStreamReader(System.in), 1);
String line;
StringTokenizer palabras;
int contador = 0;
// Aquí se procesan las palabras hasta que se llega al fin Ctrl+z en guindows pero en unixes es Ctrl+d . Creo=)
while ((line = stdin.readLine()) != null)
{
// Aquí se cuentan las palabras.
palabras = new StringTokenizer(line);
while (palabras.hasMoreTokens())
{
palabras.nextToken();
contador++;
}
}
System.out.println("\n" + contador + " palabras leidas");
}
}
[/CODE]
este contador se supone que ademas de contar escribe un archivo al server
gracias por sus comentarios =)

Opciones de visualización de comentarios

Seleccione la forma que prefiera para mostrar los comentarios y haga clic en «Guardar las opciones» para activar los cambios.
Imagen de neko069

Comentario

Los tags que colocas de [CODE] [/CODE] son inválidos, cámbialos por <code> y </code> respectivamente.

Imagen de java.daba.doo

Guardado normal

No importa desde donde estés llamando el guardado de archivos, Se hace de la misma forma desde un Socket, un Servlet, un WS, un EJB, un Xxxx siempre y cuando se escriba el archivo desde la maquina que ejecuta el codigo.

En tu caso lo que harias es enviar datos con tu socket y uno de tus nodos del socket se encargaria de realizar la escritura del archivo. Puedes ayudarte de esta clase para ver como se hace el guardado de un archivo.