Skip to main content

Posts

Showing posts from 2009

Simple Socket Client to connect to a server

private void ConnectToServer() { using ( TcpClient tc = new TcpClient ( "127.0.0.1" , 12)) { NetworkStream ns = tc.GetStream(); StreamWriter sw = new StreamWriter (ns); StreamReader sr = new StreamReader (ns); sw.WriteLine( "test message" ); sw.Flush(); System. Console .WriteLine(sr.ReadLine()); } } This code can be used to connect to a server using a socket and send a simple message to it.

How to listen to TCP socket in C#

The code below illustrate how to listen to Tcp/Ip port for incoming stream.  The code can use to listen any port but some times you may encountered an exception ( Refer ).   private void Run_Server() { // Listen to our own port 80 (HTTP protocol port). IPAddress localAddr = IPAddress .Parse( "127.0.0.1" ); TcpListener listen = new TcpListener (localAddr, 80); try { // Start listening listen.Start(); Byte [] bytes = new byte [256]; TcpClient client = listen.AcceptTcpClient(); // This waits until we getting a connection NetworkStream stream = client.GetStream(); int i; String data = null ; while (stream.DataAvailable) { i = stream.Read(bytes, 0, bytes.Length); data = System.Text. Encoding .ASCII.GetString(bytes, 0, i); txtMessages.Text += data.ToString(); } client.Close(); } catch ( SocketException exceptionError) {

An attempt was made to access a socket in a way forbidden by its access permissions

// Listen to our own port 80 (HTTP protocol port). IPAddress localAddr = IPAddress .Parse( "127.0.0.1" ); TcpListener listen = new TcpListener (localAddr, 80); This exception occurred me in Vista because of Port. This exception rise some well-known ports thus using other port (Ex:- 12 , 8008) you may resolve this.

Ugly Number Calculation Algorithm

package javaapplication1; /** ** @author sachika */ public class Main { public static void main(String[] args) { Main mainObj = new Main(); int no = mainObj.getNthUglyNo(11); System. out .print(no); try { System. in .read(); } catch (Exception s) {} } int maxDivide( int a, int b) { while (a%b == 0) a = a/b; return a; } boolean isUgly( int no) { no = maxDivide(no, 2); no = maxDivide(no, 3); no = maxDivide(no, 5); return (no == 1)? true : false ; } int getNthUglyNo( int n) { int i = 1; int count = 1; /* ugly number count */ /*Check for all integers untill ugly count becomes n*/ while (n > count) { i++; if (isUgly(i)) count++; } return i; } }

Multiple Columns in Crystal Report

Some times you might want to print the report Column wise so this is simple. what you have to do is go to Section Expert and –> Details and check the Format with Multiple Columns. then Layout tab will appear . then  configure the relevant values. after pressing ok , you can see your detail section is resized. Now you are done.

How to send a SMS using C# (Compact Framework) in Windows Mobile

Sending SMS though our own application is very necessary when we talking business application. Windows Mobile SDK comes with PocketOutlook that we can use to send SMSs. First you need to create a smart device application. and layout your form as below. Then add the PoketOutlook assemply to your application as a reference. here is the code. Method1 using Microsoft.WindowsMobile.PocketOutlook; private void btnSend_Click( object sender, EventArgs e) { String number = txtNumber.Text; String boby = txtBody.Text; SmsMessage message = new SmsMessage (number,boby); message.Send(); } Method2 – Send SMS to Multiple recipients private void btnSend_Click( object sender, EventArgs e) { SmsMessage message = new SmsMessage (); Recipient r1 = new Recipient ( "Name1" , txtNumber.Text); Recipient r2 = new Recipient ( "Name2" , "0772789456" ); message.To.Add(r1); message.To.Add(r2); message.Body = txtBody.Text; mess

Configure XAMPP with vista when IIS is running

Go to XAMPP installed folder and go to Apache /Conf/ Extra folder  and open the httpd-ssl.conf file with note pad or some text editor. # Note: Configurations that use IPv6 but not IPv4-mapped addresses need two # Listen directives: "Listen [::]:443" and "Listen 0.0.0.0:443" #   Listen 443 find the above code and change 443 to something like 4499 . DocumentRoot "c:/web/xampp/htdocs" ServerName localhost:443 ServerAdmin admin@localhost and also find the above and change it to 4499 and save the file.Then open the file httpd.conf in apache/conf folder. #Listen [::]:80 Listen 80 find above and change 80 to something like 8080 # ServerName localhost:80 and find this and change it also to 8080

Change the background color in Blackberry Fields

There are four basic background available in blackberry. SolidBackground SolidBackgroundwithTranparency GradientBackground BitmapBackground // Solid Background Background b=BackgroundFactory.createSolidBackground(Color.ANTIQUEWHITE); setBackground(b);   // Solid Background with Transperancy //Alpha Background b=BackgroundFactory.createSolidTransparentBackground(Color.AZURE,180); setBackground(b);   // Gradient Background Background b = BackgroundFactory.createLinearGradientBackground( Color.AZURE,Color.BISQUE,Color.AZURE, Color.BISQUE); setBackground(b);   // Bitmap Background Background b = BackgroundFactory.createBitmapBackground(yourImage, 0, 0,0); setBackground(b);

GO language by Google

Google released new programming language called GO . This is a Open Source Programming language. GO combines the performance and security benefits associated with using a compiled language like C++ with the speed of a dynamic language like Python. This is how google describes GO, Go attempts to combine the development speed of working in a dynamic language like Python with the performance and safety of a compiled language like C or C++. In our experiments with Go to date, typical builds feel instantaneous; even large binaries compile in just a few seconds. And the compiled code runs close to the speed of C. Go is designed to let you move fast. References,Tutorials

Programmatically Read JAD file in Blackberry

in here following code worked but you have to deploy the application using JAD file (download through the web)  not the using alx file. public static String getJADProperty(String Name){ CodeModuleGroup[] allGroups = CodeModuleGroupManager.loadAll(); CodeModuleGroup myGroup = null; String moduleName= "" ; moduleName = ApplicationDescriptor.currentApplicationDescriptor().getModuleName(); for ( int i = 0; i < allGroups.length; i++) { if (allGroups[i].containsModule(moduleName)) { myGroup = allGroups[i]; break ; } }   // Get the property String prop = myGroup.getProperty(Name); return prop; } You can get the custom property by calling // You can get the MyTag:something String value = getJADProperty( "MyTag" )

Asynchronous Threading in Blackberry

Assume that you are having a class called Class A it needs to call Class Z as a thread. Therefore you can  implement class Z from Runnable interface and implement your code in run(). Ex:- public class ClassZ implements Runnable {   public static final int METHOD1 =1; public static final int METHOD2= 2;   int code; public ClassZ( int code) { this .code=code; }   private void method1() { //Code for Method1 }   private void method2() { //Code for Method2 }   public void run() { switch (code) { case ClassZ.METHOD1 : method1(); break ; case ClassZ.METHOD2: method2(); break ; } }   public void runThread() { thread = new Thread( this ); thread.start(); } } If you start the Thread using ClassZ it runs ClassZ’s run method. Therefore  if you have several method in side the class you need to have a v

Best Practices in programming

I though to write a continuous article describing best practices in programming. Here I’m not going to talking about  coding conventions but talking about enhanced cording techniques you can use. Using IO, Network connections InputStream streamRead =null; try { // Use InputStream to do your work } catch (Exception ex) { // Exception Handling } finally { try { // Try to close the stream, This may also give erros // resulting opened stream streamRead.close(); } catch (IOException e) { } // Set Stream to null to prevent // unwanted openings streamRead=null; } Not to use methods in loop’s conditions // This is not optimized for ( int i = 0; i < elements.capacity(); ++i) { // Code Here }   // This is optimized, // elements.capacity() only call in one time int capacity = elements.capacity(); for ( int i = 0; i < capacity; ++i) { // Code Here } Use String Buffer wherever possible. // This will create two string and