How to Send SMS using CIMD2 Protocol

I know this code is not optimizing, but its work well to send message using CIMD protocol.

import java.io.*;
import java.net.*;

public class Cimd {
   
    public static final char STX = '\u0002';
    public static final char ETX = '\u0003';
    public static final char TAB = '\u0009';
   
    private String loginMessage = "";
    
    public String createLoginMsg (String username, String password) {        
        loginMessage = STX+"01:001"+TAB+"010:"+username+TAB+"011:"+password+TAB;
        return loginMessage;
    }    
    public String calculateChecksum(String data) {
        int checksum = 0;
        char charData[] = data.toCharArray();
        for(int i = 0; i < charData.length; i++) {
            checksum += charData[i];
            checksum &= 0xff;
        }      
        return new String(Integer.toHexString(checksum).toUpperCase());
    }        
    public static void main(String[] arg) throws Exception {
    	String Host = "0.0.0.0";
    	String Port = "0000";
    	String Username = "xxxx";
    	String Password = "xxxx";
    	try {    		
	    	Socket s = new Socket(Host, Integer.parseInt(Port));
	    	s.setSoTimeout(60000);
	        s.setKeepAlive(true);
	        s.setSoLinger(false, 0);
	        s.setReceiveBufferSize(65535);       
	              
	        Cimd cimd = new Cimd();
	        String loginMsg = cimd.createLoginMsg(Username,Password);
	                      
	        String chkSum = cimd.calculateChecksum(loginMsg);         
	        loginMsg = loginMsg+ETX; 
	        
	        System.out.println(loginMsg);       
	        
	        // Send the login command to host            
	        OutputStream out = s.getOutputStream();
	        out.write(loginMsg.getBytes());       
	        out.flush();
	        System.out.println(read(s));
	        int packetnumber = 3;
	        for (int i = 0; i < 2; i++) {
	        	System.out.println("packet: " + String.format("%03d", packetnumber));
	        	String message = STX+"03:"+String.format("%03d", packetnumber)+TAB+"021:910000000000"+TAB+"033:test message"+TAB;
	        	chkSum = cimd.calculateChecksum(message);         
		        message = message+chkSum+ETX; 
		        System.out.println(message );
		        OutputStream out1 = s.getOutputStream();
		        out1.write(message.getBytes());       
		        out1.flush();
		        System.out.println(read(s));
		        packetnumber +=2;
			    if (packetnumber>255) packetnumber=1;
	        }
	        System.out.println("packet: " + String.format("%03d", packetnumber));
	
	        //Logout
	    	String logout = STX+"02:"+String.format("%03d", packetnumber)+TAB;
	    	chkSum = cimd.calculateChecksum(logout);         
	    	logout = logout+chkSum+ETX; 
	        System.out.println(logout);
	        out = s.getOutputStream();
	        out.write(logout.getBytes());       
	        out.flush();
	        System.out.println(read(s));        
	        // close all
	        out.close();
	        s.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
    }
    public static String read(Socket s) throws Exception{
        BufferedReader in1 = new BufferedReader(new InputStreamReader(s.getInputStream()));        
        byte[] input1 = new byte[1024];
        boolean isDataReturned1 = false;
        int j1 = 0;                  
        while (!isDataReturned1) {
           int b = in1.read( );           
           if (b  == ETX) break;
           input1[j1++] = (byte) b;
        } 
        return new String(input1);
    }
}

If this code helps you or you have any issue let us know in the comment box below.

Tags:

Submit an Idea
  Thank you for your valuable feedback.
Your E-mail (optional):
Select Option:
Description:
 
Post a comment
Add a Comments

Log in or create an account to post a comment.

Comment
  Saving...
Feedback
Your Name / email address
Feedback as
Your feedback
close
Quickly sign in with:

Create a FATALWEB account here:

First name:

Last name:

E-mail address:

Join Fatalweb

Already have a fatalweb account? Log in here