954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

socket connection

am currently workin on RFID Technology
and am stuck with the socket connectivity...


there is a client which is an EXE file called RFID.exe
now we need to create a server program to connect the client n read the msgs sent by the client application. in the server program v need to store the msgs using the HASH table n filter the msgs with no duplication and store it in the database..


these are the following codes..
but cant understand any..
and the client prog is missing...

RFID data processor

package com.satyam.rfid.processor;

import java.util.Hashtable;

import org.apache.log4j.Logger;

import com.satyam.impl.bo.DCPickConfirmation;
import com.satyam.rfid.util.RFIDUtil;
import com.satyam.rfid.beans.RFIDBean;
import com.satyam.rfid.beans.ReaderBean;
import com.satyam.rfid.dao.CommonDao;
import com.satyam.rfid.exceptions.InvalidMsg;
import com.satyam.rfid.util.RFIDInfo;
import com.satyam.rfid.util.RFIDLogger;

/**
* @author admin
*/
public class RFIDDataProcessor {
private RFIDBean rfidBean;
private CommonDao dao;
private ReaderBean readerBean;
private Logger log;
//private Hashtable htMsg = null;
private Hashtable htReader = new Hashtable();
private Hashtable htLogicalLocation= new Hashtable();

/** Detault constructor
*
*/
public RFIDDataProcessor(){
super();
log = RFIDLogger.getLogger(this);
}

/**
*/

public void doProcess(Hashtable htMsg){
//log.info("Start of doProcess()");
try{
String msgType=(String)htMsg.get(RFIDInfo.RFID_MSG_TYPE);
if(msgType.equalsIgnoreCase(RFIDInfo.RFID_MSG_CONFIG)){
String readerIP = (String)htMsg.get(RFIDInfo.RFID_MSG_REARDER_IP);
String readerPort = (String)htMsg.get(RFIDInfo.RFID_MSG_READER_PORT);
String readerName = (String)htMsg.get(RFIDInfo.RFID_MSG_READER_NAME);
int cycleTime = Integer.parseInt((String)htMsg.get(RFIDInfo.RFID_MSG_READER_CYCLE_TIME));
int totalAntenna = Integer.parseInt((String)htMsg.get(RFIDInfo.RFID_MSG_READER_ANT));
int totalAntGroup = Integer.parseInt((String)htMsg.get(RFIDInfo.RFID_MSG_READER_ANT_GRP));
Hashtable htAntennaGroupID=new Hashtable();
Hashtable htAntennaGroupName = new Hashtable();
for (int count=0;countRFID message parser/**
*
*/
package com.satyam.rfid.processor;

import java.util.Hashtable;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;

import com.satyam.rfid.util.RFIDLogger;


/**
* @author Badmabooshan
* @version 1.0
*/
public class RFIDMessageParser {

private Hashtable msgMap = new Hashtable(100);
private Logger log;
//Default constructor
public RFIDMessageParser(){
log = RFIDLogger.getLogger(this);
}

public Hashtable getMessageDetails(String recMsg){
StringTokenizer messageContent;
int count;
String msgSegment;
StringTokenizer msg;
messageContent = new StringTokenizer(recMsg,"$");
if(messageContent.countTokens()>=1)
{
int messageSize=messageContent.countTokens();
for (count =0; countRFID socket Reader:
package com.satyam.rfid.processor;

import com.satyam.rfid.dao.CommonDao;
import com.satyam.rfid.exceptions.InvalidMsg;
import com.satyam.rfid.util.RFIDInfo;
import com.satyam.rfid.util.RFIDLogger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;
import java.util.Hashtable;
import org.apache.log4j.Logger;


/**
* @author admin
*/
public class RFIDSocketReader implements Runnable{
/**
* @uml.property name="socket"
*/
private Socket socket;
private Logger log;
private String receviedMsg=null;
private Hashtable ht=null;
private InputStream is;
private BufferedReader brOut;
private RFIDDataProcessor rfidDataPrcr= new RFIDDataProcessor();

//initializes the logger
public RFIDSocketReader() {
log = RFIDLogger.getLogger(this);
}
/**This method processes the InputStream from the socket and parses the
* character array. It initialises the RFIDDataProcessor and invoke the life
* cycle methods for further processing.
* Finally it calls the close method to close the socket connection.
*/
public void run() {
try {

//Instantiating CommonDao
CommonDao dao = new CommonDao();

log.info("Connection accepted from " + socket.getRemoteSocketAddress());

while(true){
if(this.brOut!=null){
receviedMsg = this.brOut.readLine();
}
else{
receviedMsg="Disconnected";
}
if(receviedMsg.equals("Disconnected")){
throw new InvalidMsg(receviedMsg);
}else if(receviedMsg==null){
throw new InvalidMsg(receviedMsg);
}
else{
log.info("Received Message :" + receviedMsg);
// Parsing the RFID message from the above string

RFIDMessageParser rmp = new RFIDMessageParser();
ht = rmp.getMessageDetails(receviedMsg);
//Getting RFID Details from the parser results
String msgType=(String)ht.get(RFIDInfo.RFID_MSG_TYPE);
//Diconnect the Client
if(msgType.equalsIgnoreCase(RFIDInfo.RFID_MSG_CDISCONNECT)){ // Diconnecting the Reader
close();
}
else{
log.info("Starting Life Cycle Process");
rfidDataPrcr.doProcess(ht);
//rfidDataPrcr.destroy();
//rfidDataPrcr = null;
}
}
}

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

/**
* @return Returns the socket.
* @uml.property name="socket"
*/
public Socket getSocket() {
return socket;
}
/**
* @param socket The socket to set.
* @uml.property name="socket"
*/
public void setSocket(Socket socket) {
try {
this.socket = socket;
//Getting the input stream from the socket
is = socket.getInputStream();
//Creating the BufferedReader from the input stream
brOut = new BufferedReader(new InputStreamReader(is));
} catch (Exception e) {
e.printStackTrace();
}
}
/**This method closes the socket connection
*/
private void close(){
try{
this.socket.close();
this.is = null;
this.brOut=null;
Thread t = Thread.currentThread();
log.info("Reader Disconnected...End the thread:" + t.getName());
if(t.isDaemon()){
t.destroy();
}
}catch(SocketException se){
log.error(se.getMessage());
}catch(IOException ioe){
log.error(ioe.getMessage());
}
}
}


TCPIP:
package com.satyam.rfid.processor;

/**
* @author bb27629
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;


import java.net.InetAddress;
import java.net.Socket;
import java.sql.Connection;


public class TCPIP implements Runnable{

BufferedReader bufferedReaderOut = null;
Thread thread = null;
Socket socketClient = null;
byte[] byteArray = new byte[255];
int numBytes=0;
boolean toRun = false;

public TCPIP()
{

}
public TCPIP(Socket socketClient)
{
toRun = true;
this.socketClient = socketClient;
try{
bufferedReaderOut = new BufferedReader( new InputStreamReader(socketClient.getInputStream()));
}catch(IOException ioe){
System.out.println("Error : " + ioe.getMessage());
ioe.printStackTrace();
}
thread = new Thread(this);
thread.start();
}
public void stopThread()
{
try
{
if(!(this.socketClient==null))
{
this.socketClient.close();
this.thread.stop();
}

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


public void run()
{
boolean toRun = true;
byte[] byteArray = new byte[255];
int intByteCount = 0,i=0;
char[] chrArray;
while(true)
{
try{

String strData = bufferedReaderOut.readLine();
System.out.println("Received Data : " + strData);
byteArray = strData.getBytes();
System.out.println("Length : " + byteArray.length);
System.out.println("Reading....");
}catch(Exception e)
{
System.out.println("Error :" + e.getMessage());
e.printStackTrace();
}
}
}

public static TCPIP ipConfig(String strIpAddress)
{
TCPIP tcpip =null;
boolean blnConnected = true;

while (blnConnected)
{
try{
Connection connection = null;
//System.out.println("IPCONFIG");
Socket socketClient= new Socket( InetAddress.getByName(strIpAddress),23);
//System.out.println("IPCONFIG");
tcpip = new TCPIP(socketClient);
blnConnected=false;

}catch(Exception e)
{
System.out.println("********* Error : " + e.getMessage());
e.printStackTrace();
blnConnected=true;
}
}
return tcpip;
}
public static void main(String[] args)
{
TCPIP tcpIpConfig = new TCPIP();
tcpIpConfig.ipConfig("172.18.33.164");
//TCPIP.ipConfig("172.18.33.157");
}
}

Attachments RFID_Setup.zip (167.17KB)
ihtraa
Newbie Poster
10 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You