Skip to main content

Posts

Showing posts from December, 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.