Saturday, December 15, 2012

Socket Programming in JAVA


Whats a socket?

Every computer on a network has an IP address. This address is   like your house address, something that identifies your   computer uniquely, allowing others  to communicate with your  computer. I wont go much into IP addresses, but let me  just tell you that  an IP 
address looks something like this - 64.104.137.158  a set of  numbers separated with dots.

However, some computers with rich owners will also choose to  have a domain name in addition to this sick looking number, so  that people can easily identify them. 

A domain name looks far more sane - like www.yahoo.com.There  are some special computer  on the Internet, whose sole purpose  in life is to translate the domain name to IP address and vice  versa

 An IP address = uniquely identifies a computer on the network.
 A port number = uniquely identifies a program running on a computer.
 An IP address + A port number = SOCKET

a socket is a combination of an IP address and a port. A socket address lets other computers on the network locate a  certain program running on a certain computer. You may represent a socket address  like 64.104.137.58:80, where 64.104.137.58 is the IP address and 80 is the port number.

Setps For Program

1: One Java program will try to connect to another Java program. 
   Lets call the  first program   
                   as Client, 
   and the second 
                   as   Server.
 
2: Once connected, the client program is going to accept whatever you type,  and send it 
    dutifully to the server program.
 
3: The server is going to send back the same text to the client, just to show  that it is least 
    interested in doing such an uninteresting thing.
 
 Create 2 fresh Java programs and call them Server.java and Client.java. 

Server.java
import java.net.*;
import java.io.*; 
public class Server
public static void main(String[] ar) 
 int port = 6666; // just a random port.  
 try 
   ServerSocket ss = new ServerSocket(port); // create a server socket and bind 
   System.out.println("Waiting for a client..."); 
   Socket socket = ss.accept(); // make the server listen for a connection     
   System.out.println("Client Recived."); 
   System.out.println();  /* Get the input and output streams of the socket, so that you can receive and send data to the client.*/ 
 
   InputStream sin = socket.getInputStream();
   OutputStream sout = socket.getOutputStream();  /*Just converting them to different streams, so that string handling becomes   easier.*/  
   DataInputStream in = new DataInputStream(sin); 
   DataOutputStream out = new DataOutputStream(sout);
   String line = null; 
   while(true)
   { 
    line = in.readUTF(); // wait for the client to send a line of text.
    System.out.println("Data from Client: " + line);
    System.out.println("I'm sending it back...");
    out.writeUTF(line); // send the same line back to the client. 
    out.flush(); //flush the stream .  
    System.out.println("Waiting for the next line..."); 
    System.out.println(); 
   }
  } 
  catch(Exception x)
  { 
  x.printStackTrace(); 
   }
 }
} 

Client.java
  
import java.net.*;
import java.io.*;
public class Client 
{
public static void main(String[] ar)
 {
 int serverPort = 6666; // make sure you give the port number on which the server is listening.
 String address = "127.0.0.1"; // this is the IP address of the server program's computer. 
 try 
{
 InetAddress ipAddress = InetAddress.getByName(address); // create an object that represents the above IP address.
 System.out.println("a socket with IP address " + address + " and port " + serverPort + "?");
 Socket socket = new Socket(ipAddress, serverPort); // create a socket with the server's IP address and server's port.
 System.out.println("----Connect-----");
 /* Get the input and output streams of the socket, so that you can receive and send data to the client*/
 InputStream sin = socket.getInputStream();
 OutputStream sout = socket.getOutputStream();
 // Just converting them to different streams, so that string handling becomes easier.
 DataInputStream in = new DataInputStream(sin);
 DataOutputStream out = new DataOutputStream(sout);
 // Create a stream to read from the keyboard.
 BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
 String line = null;
 System.out.println("Type in something and press enter. ");
 System.out.println();
 while(true)
 {
 line = keyboard.readLine(); // wait for the user to type in something and press enter.
 System.out.println("Sending this line to the server...");
 out.writeUTF(line); // send the above line to the server.
 out.flush(); // flush the stream to ensure that the data reaches the other end.
 line = in.readUTF(); // wait for the server to send a line of text.
 System.out.println("The server was very polite. It sent me this : " + line);
 System.out.println("Looks like the server is pleased with us. Go ahead and enter more lines.");
 System.out.println();
 }
 } catch(Exception x) {
 x.printStackTrace();
 }
 }
 }
 Compile it with 
  javac Server.java Client.java
  
Open two command windows (DOS prompts). In one of those, enter
                java Server
and in the other,
                java Client

Thursday, December 22, 2011

C Program for check the String is plaindrom or not.......

/* Plaindrom String */

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
 char str1[10],str2[10],i,l;
 clrscr();
 printf("Eneter the string:");
 gets(str1);

  l=strlen(str1);
  l--;

  i=0;

  while(l>=0)
  {
   str2[i]=str1[l];
   l--;
   i++;
   }
   str2[i]='\0';
   i=strcmp(str1,str2);

   if(i==0)
    printf("\nString is plaindrom");
   else
    printf("\nString is not plaindrom");

   getch();

}

C Program for Factorial useing Recursion....

#include<stdio.h>
#include<conio.h>
long int fact(long int);
 void main()
 { 
   long int  num,f;
   clrscr();
   printf("Input a number: ");
   scanf("%ld",&num);
   f=fact(num);
   printf("\nFactorial is %ld",f);
   getch();
 }
 long int fact(long int n)
 {
    if(n==0)
         return 1;
    else 
             return(n*fact(n-1)); 
 }

C Program for Bubble Sort.....

/************************Bubble Sorting**************************/

 #include<stdio.h> 
 #include<conio.h> 
  
  void main() 
   { 

       int a[100],n,i,j,t;
      clrscr(); 
      printf("\n\n Enter integer value for total no.s of elements to be sorted: "); 
       scanf("%d",&n); 
  
      for( i=0;i<=n-1;i++) 
                   scanf("%d",&a[i]); 
   for(i=n-2;i>=0;i--) 
          { 
             for(j=0;j<=i;j++) 
  
                   { 
                     if(a[j]>a[j+1]) 
                                     { 
                                       t=a[j]; 
                                      a[j]=a[j+1]; 
                                      a[j+1]=t; 
                                     } 
                    } 
            }          
        printf("\n\n Finally sorted array is: "); 
        for( i=0;i<=n-1;i++) 
    printf("\n%d",a[i]);
    getch();
  
   }