Wednesday, March 23, 2011

MultiSocketServer Example with Thread

ServerCode:

import java.net.ServerSocket;
import java.util.HashMap;

/**
*
* @author R.Amirtharaj
*/
public class Server implements Runnable {

// Storing Clients in HashMap
public static HashMap hmClients = new HashMap();
ServerSocket server = null;
boolean sFlag = false;

public void initializeServer() throws Exception {
server = new ServerSocket(5010);
}

public void run() {
try {
initializeServer();
System.out.println("SERVER STARTTED");
sFlag = true;
while (sFlag) {
new ClientWorker(server.accept());
System.out.println("client accepted");
}
} catch (Exception e) {
e.printStackTrace();
closeConnection();
}
}

public void closeConnection() {
try {
hmClients.clear();
if (server != null) {
server.close();
}
server = null;
} catch (Exception e) {
e.printStackTrace();
}

}

public static void clearClients(String cName) {
try {
ClientWorker tcw = (ClientWorker) hmClients.get(cName);
if (tcw != null) {
tcw.closeConnection(2);
tcw = null;
}

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


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

public class ClientWorker extends Thread {

private Socket client;
private DataInputStream din = null;
private DataOutputStream dout = null;
boolean rFlag = false;

ClientWorker(Socket client) {
try {
this.client = client;
din = new DataInputStream(client.getInputStream());
dout = new DataOutputStream(client.getOutputStream());
String strC = din.readUTF();
Server.clearClients(strC);
Server.hmClients.put(strC, this);
rFlag = true;
start();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void run() {

while (rFlag) {
try {
String line = din.readUTF();
System.out.println("MSG = " + line);
} catch (Exception e) {
e.printStackTrace();
closeConnection(1);
}
}
}

public void closeConnection(int i) {
System.out.println("closing " + i);
try {
rFlag = false;
if (client != null) {
client.close();
}
if (din != null) {
din.close();
}
if (dout != null) {
dout.close();
}
client = null;
din = null;
dout = null;
Thread.currentThread().interrupt();

} catch (Exception e) {
}
}

public void sendMessage(String strMsg) {
try {
dout.writeUTF(strMsg);
} catch (Exception e) {
e.printStackTrace();
}
}
}


For Client 1:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

/**
*
* @author R.Amirtharaj
*/
public class Client1 implements Runnable {
public Socket soc = null;
DataOutputStream dout = null;
DataInputStream din = null;
boolean rFlag = false;

public void initializeSocket() throws Exception
{
soc = new Socket("192.168.0.40", 5010);
dout = new DataOutputStream(soc.getOutputStream());
din = new DataInputStream(soc.getInputStream());
dout.writeUTF("CLIENT_1");
}

public void run() {
try {
initializeSocket();
System.out.println("CLIENT_1 started");
rFlag = true;
while (rFlag) {
String msg = din.readUTF();
System.out.println("CLIENT_1 read : "+msg);
}
}
catch(Exception e)
{
e.printStackTrace();
closeConnection();
}
}

public void sendMessage(String strMsg)
{
try {
System.out.println("CLIENT_1 write : "+strMsg);
dout.writeUTF(strMsg);
} catch (Exception e) {
e.printStackTrace();
closeConnection();
}
}

public void closeConnection()
{
System.out.println("CLIENT_1 closing");
try
{
rFlag = false;
if(din != null) din.close();
if(dout != null) dout.close();
if(soc != null) soc.close();
din = null;
dout = null;
soc = null;
}
catch(Exception e)
{
//e.printStackTrace();
}
}

}

For Client 2:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

/**
*
* @author R.Amirtharaj
*/
public class Client2 implements Runnable {
public Socket soc = null;
DataOutputStream dout = null;
DataInputStream din = null;
boolean rFlag = false;

public void initializeSocket() throws Exception
{
soc = new Socket("192.168.0.40", 5010);
dout = new DataOutputStream(soc.getOutputStream());
din = new DataInputStream(soc.getInputStream());
dout.writeUTF("CLIENT_2");
}

public void run() {
try {
initializeSocket();
System.out.println("CLIENT_2 started");
rFlag = true;
while (rFlag) {
String msg = din.readUTF();
System.out.println("CLIENT_2 read : "+msg);
}
}
catch(Exception e)
{
e.printStackTrace();
closeConnection();
}
}

public void sendMessage(String strMsg)
{
try {
System.out.println("CLIENT_2 write : "+strMsg);
dout.writeUTF(strMsg);
} catch (Exception e) {
e.printStackTrace();
closeConnection();
}
}

public void closeConnection()
{
System.out.println("CLIENT_2 closing");
try
{
rFlag = false;
if(din != null) din.close();
if(dout != null) dout.close();
if(soc != null) soc.close();
din = null;
dout = null;
soc = null;
}
catch(Exception e)
{
//e.printStackTrace();
}
}

}

Tuesday, March 22, 2011

JTextField Validation

case 1: Int field (TextField can allow only numbers and length of numbers to be 5)

JTextField tfInt = new JTextField();

// Validation part
tfInt.setDocument( new PlainDocument() {
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException
{
if(str == null) return;
//accept only number
ch = str.charAt(0);
if (!((ch >47)&&(ch<58))) {
return;
}

// For length validation
if(offs > 4) return;
// While copy paste then below length validation required
if(str.length()>5) return;
super.insertString(offs, str, a);
}
});


// For Number validation
public class OnlyNumberValidator extends javax.swing.text.PlainDocument {
@Override
public void insertString(int offs, String str,
javax.swing.text.AttributeSet a)
throws javax.swing.text.BadLocationException {
StringBuffer buf = new StringBuffer(str);
int size = buf.length();
char c;
for (int i = 0; i < size; i++) {
c = buf.charAt(i);
if (!Character.isDigit(c)) {
buf.deleteCharAt(i);
}
}

super.insertString(offs, buf.toString(), a);
}
}