More Java Network Programming

M

M Maloney

Hey all,
I currently have a working concurrent server and client program,
wher the client requests the time, and the thread handling the client
replies.
Now I want to replace the time of day request with a binary
protocol where the client can do 3 things: 1. authenticate me, 2.
alert the server that I have contacted him/her, or am away from my
terminal, and 3. display my message to the user
Of course this should also allow the server to: 1. authentication
accepted/rejected. 2. send status of user at sever (away/available),
and 3. display my message to the user

this is the format of the protocol headers:
type (8 bits), status (8 bits), length (16 bits) = 32 bits
data (variable)
Types for Client are 0 = authenticate, 1 = alert_status 2 =
set_message
Types for server are: 3 = authenticate, 1 = alert_status, 2 =
set_message
And length is the length of the data field in bytes.
Status types are: available = 1, away = 0, authentication_OK = 2,
authentication_FAIL = 3

This is all well and good, however my problem is that I don't know
how to start with the protocol format. Do I send the protocol over? Do
i convert the numbers to binary first? What is the actual thing I send
over, a packet or just a message?

Thanks for any help, appreciate it.
 
M

martinm69x

This is my client program: If the client enters 'Time' in the terminal
then server replies with time, otherwise says: invalid command'
public class TCPClient
{
public static void main(String []args) throws Exception
{
PrintWriter outy;
OutputStreamWriter out;
String message1;
if (args.length != 2)
{
System.out.println("Must provide a port number");
}
System.out.println("Connecting");
Socket s = new Socket(args[0], Integer.parseInt(args[1]));
System.out.println("Done");
System.out.println("Connected to " + s.getInetAddress() + " on
port " + s.getPort());

while(!s.isClosed())
{
try {

outy = new PrintWriter(s.getOutputStream());
BufferedReader console = new BufferedReader(new
InputStreamReader(System.in));
String input = console.readLine();

outy.println(input);
outy.flush();

BufferedReader networkIn = new BufferedReader(new
InputStreamReader(s.getInputStream()));
if((message1 = networkIn.readLine()) != null)
//if(!message1.isNull())
{
System.out.println("Message recieved was " + message1);
}
else
{
s.close();
System.out.println("Sorry Socket already closed");

}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
// s.close();
}
}

This is my Thread class that handles all connections to the server, if
there is no traffic in 15 seconds it closes the communication.
public class TCPSockDealer implements Runnable
{
private Socket sd;
private TCPServer caller;
String wanted = "Time";
PrintWriter out;
int number;

public TCPSockDealer(Socket s, int ti)
{
sd = s;
number = ti;
}

public void run()
{
// while(listener.getCount() != 0)
// {
while(!sd.isClosed())
{
try
{
sd.setSoTimeout(15000);
BufferedReader networkIn = new BufferedReader(new
InputStreamReader(sd.getInputStream()));
out = new PrintWriter(sd.getOutputStream());

String message1 = networkIn.readLine();

if(message1.equals(wanted))
{
Date d = new Date();
String message2 =
DateFormat.getDateTimeInstance().format(d);
System.out.println("Message received was " + message2);
out.println(message2);
out.flush();
}
else {
System.out.println("Message recieved was " + message1);
out.println("not a valid command");
out.flush();
}
// sd.close();
// System.out.println("Socket closed");
}catch(SocketException e)
{
System.out.println("Exception: " + e);
}
catch(IOException w)
{
System.out.println("No traffic");
try
{
sd.close();
// caller.setCheck(number);
}catch(IOException f)
{
System.out.println("Closing....");
}
}
}
return;

}
}
Finally this is my Server class:

public class TCPServer
{
int threads = 0;
boolean[] arr;
int[] tries;
String wanted = "Time";

public TCPServer() throws Exception
{
arr = new boolean[1];
arr[0] = false;
init();

}

public void init() throws Exception
{
ServerSocket ss = new ServerSocket(0);
Socket s = null;

ss.setSoTimeout(35000);
//s.setSoTimeout(16000);
System.out.println("Server running on port: " +
ss.getLocalPort());

try
{
java.net.InetAddress localMachine =
java.net.InetAddress.getLocalHost();
System.out.println ("Server running on host: " +
localMachine.getHostName());

}
catch(java.net.UnknownHostException uhe) {
//handle exception
System.out.println("Can't find server host name");
}
PrintWriter out;

while (true)
{
try {
s = ss.accept();
threads++;
s.setSoTimeout(16000);
System.out.println("Accepting");
TCPSockDealer sd = new TCPSockDealer(s, threads);
Thread th = new Thread(sd);
th.start();
}
catch(InterruptedIOException e)
{
System.out.println("No connection within 35 seconds");
if(s!=null)
s.close();
ss.close();
break;

}
/* catch(SocketException e)
{
System.out.println("Socket closing...");

} */

}
}
public static void main(String[] args) throws Exception
{
new TCPServer();
}

}

Once again I woud appreciate if anybody could help me out with my
problem. One thing I was thinking of implementing would be a menu for
the client: like: 1. authenicte 2. ..... Please select oneof the
options etc
Something like that
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top