How to handle a Client/Server application for sending message and/or files
I have done a lot of searches around here, but I haven't found anything
that matches even a bit of what I need.
I'm not so expert in client/server application, and this is my first
attempt, so, please, be patient if I have made some error or if I made
silly questions.
Now, back to my problem..
I need to build a multi-client / server application.
The server side should be a simple manager for the clients.
So, for example:
the server can change some clients parameters
accepts files from the clients
other boring stuff.. that basically can be translated in sending string or
sending files or getting string and files
The clients, in the other hand, is a complex application (at least, for
the user) that should send to the server:
some user registration data (no need for security stuff here, I'm already
pretty messed up with a simple client/server connection)
a PSD file that should be the result of the user work, so when the user
click "I'm done" the application get this PSD file and send it to the
Server for storing purpose
other client information such us the default configuration data, and so on..
So, my question is basically this: How can I handle the communication from
the server with one specific client? I mean, I have the server up, and I
what to change a configuration just for one client.
I suppose I need to store the clients in some way.. like in an array (a
List), but I don't know if this is the right way to do this. (Basically I
dont't know how the classes Socket and ServerSocket works.. if this can
help you to better understand)
Also, when the server is up and listen.. the GUI need to be updated to
show the new connected clients, so I need some kind of listener to the
server that fires an action back to the interface when a new Client shows
up? (A lot of people uses the while(true) { socket = server.accept(); }
method, but this doesn't sound very smart to me..)
This is the basic Client.java and Server.java files that contains the
client and server basic functions that I wrote based on a lots of Google
searches.
But all of the code below doesn't math all my needs..
Client.java
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client extends Socket {
private static Client instance = null;
/**
* The main init() function for this class, to create a Singleton
instance for the Client
*
* @param host
* The host of the Server
* @param port
* The port of the Server
* @return The Client instance that is a new instance if no one exists
previusly,
* otherwise an older instance is returned
* @throws UnknownHostException
* @throws IOException
*/
public static Client init( String host, Integer port ) throws
UnknownHostException, IOException
{
if ( Client.instance == null )
Client.instance = new Client( host, port );
return Client.instance;
}
/**
* Default Constructor made private so this class can only be
instantiated by the
* singleton init() function.
*
* @param host
* The host of the server
* @param port
* The port of the server
* @throws UnknownHostException
* @throws IOException
*/
private Client( String host, Integer port ) throws
UnknownHostException, IOException
{
super( host, port );
}
/**
* Function used to send a file to the server.
* When this function fires, the Client class start sending a file to
the server.
* Internally this function handles the filesize, and some other file
information
* that the server needs to store the file in the correct location
*
* @param filename
* The filename of the file that will be sended to the server
*/
public void sendFile( String filename ) throws FileNotFoundException,
IOException
{
// The file object from the filename
File file = new File( filename );
// A string object to build an half of the message that will be
sent to the exceptions
StringBuilder exception_message = new StringBuilder();
exception_message.append( "The File [" ).append( filename
).append( "] " );
// Check if the file exists
if ( !file.exists() )
throw new FileNotFoundException( exception_message + "does not
exists." );
// Check if the file size is not empty
if ( file.length() <= 0 )
throw new IOException( exception_message + "has zero size." );
// Save the filesize
Long file_size = file.length();
// Check if the filesize is something reasonable
if ( file_size > Integer.MAX_VALUE )
throw new IOException( exception_message + "is too big to be
sent." );
byte[] bytes = new byte[file_size.intValue()];
FileInputStream fis = new FileInputStream( file );
BufferedInputStream bis = new BufferedInputStream( fis );
BufferedOutputStream bos = new BufferedOutputStream(
this.getOutputStream() );
int count;
// Loop used to send the file in bytes group
while ( ( count = bis.read( bytes ) ) > 0 )
{
bos.write( bytes, 0, count );
}
bos.flush();
bos.close();
fis.close();
bis.close();
}
/**
* Function used to send string message from client to the server
*
* @param message
* The string message the server should get
* @throws IOException
*/
public void sendMessage( String message ) throws IOException
{
OutputStream os = this.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter( os );
BufferedWriter bw = new BufferedWriter( osw );
bw.write( message );
bw.flush();
}
/**
* Function used to get a message from the Server
*
* @return The message the server sent back
* @throws IOException
*/
public String getMessage() throws IOException
{
InputStream is = this.getInputStream();
InputStreamReader isr = new InputStreamReader( is );
BufferedReader br = new BufferedReader( isr );
String message = br.readLine();
return message;
}
}
Server.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends ServerSocket {
private static Server instance = null;
private Socket socket = null;
/**
*
* @param port
* @return
* @throws IOException
*/
public static Server init( Integer port ) throws IOException
{
if ( Server.instance == null )
Server.instance = new Server( port );
return Server.instance;
}
/**
*
* @param port
* @throws IOException
*/
private Server( Integer port ) throws IOException
{
super( port );
// Maybe this is something that needs to be improved
while ( true )
this.socket = this.accept();
}
/**
*
* @param message
* @throws IOException
*/
public void sendMessage( String message ) throws IOException
{
OutputStream os = this.socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter( os );
BufferedWriter bw = new BufferedWriter( osw );
bw.write( message );
bw.flush();
}
/**
*
* @return
* @throws IOException
*/
public String getMessage() throws IOException
{
InputStream is = this.socket.getInputStream();
InputStreamReader isr = new InputStreamReader( is );
BufferedReader br = new BufferedReader( isr );
String message = br.readLine();
return message;
}
}
aehm.. apologize for my english.. please.
No comments:
Post a Comment