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();
}
}
}
No comments:
Post a Comment