Wednesday, March 14, 2012

Java Program -1 way Client-Server Communication using TCP/IP



In java Networking is done using Sockets and ServerSockets.To get a good idea of how sockets are used in java for creating a client server model see the article. http://c-madeeasy.blogspot.com/2012/03/concept-of-sockets-and-networking-in.html

TCP/IP(Transmission Control Protocol/Internet Protocol) :
Is connection based protocol that is widely used over the internet.It is commonly referred to as IP.The following program uses TCP/IP to communicate.


After reading the above article,see the code below.You can see how socket and server sockets are used to create a one way client server program.Here a client can communicate with the server only.Both are DOS/Terminal Java Programs.

Fist you start the server program followed by client program in two separate terminals.In the client terminal type anything,you can see that appearing in the server terminal window.

The complete Java Program source code to implement 1 way client and server model is provided below as two separate programs Client.java and Server.Java

 //Server Program  
 //c-madeeasy.blogspot.com www.codeuniverse.tk  
 import java.io.*;  
 import java.net.*;  
 class server{  
 public static void main(String []args)  
 {  
 String data;  
 ServerSocket ssock;  
 Socket clientsock=null;  
  DataInputStream is;  
 try{  
 ssock=new ServerSocket(2000);  
 System.out.print("Server Started");  
 clientsock=ssock.accept();  
 is=new DataInputStream(clientsock.getInputStream());  
 System.out.println("Connection Accepted");  
 while(true)  
 {  
 data=is.readLine();  
 System.out.println(data);  
 }  
 }  
 catch(Exception e)  
 {  
 System.out.println("ERROR");  
 }  
 }  
 }  
 ---------------------------------------------------------------  
 //Client Program  
 import java.io.*;  
 import java.net.*;  
 class client{  
 public static void main(String []args)  
 {  
 String text;  
 Socket sock=null;  
  DataOutputStream dout;  
  PrintStream sender;  
  DataInputStream keyboardreader;  
  System.out.println("Connecting to Server.....");  
  try{  
   sock=new Socket("localhost",2000);  
   }  
  catch(Exception e)  
  {  
  }  
  try{  
  System.out.println("Connected");  
  keyboardreader=new DataInputStream(System.in);  
  sender=new PrintStream(sock.getOutputStream());  
  do  
  {  
  text=keyboardreader.readLine();  
  sender.println(text);  
  }while(!text.equals("quit"));  

Friday, March 9, 2012

The Concept of Sockets and Networking in Java Simplified

Socket:
A socket is one end-point of a two-way communication link between two programs running on the network.java.net Package provides support for sockets. Socket classes can be used to implement the connection between a client program and a server program.

ServerSocket:
ServerSocket class is exclusive for the Server side implementation of the client-server model.


 Java programming with Socket class allows easy implementation of a client-server model for one way or two way communication. We can easily implement a simple one way client server model,2 way client server model,a broad cast server,a multicast server using Sockets and Server Sockets in Java. 

                                                                      courtesy:oracle

Client Side
 Basically a socket can used in a client to send message it involves creation of socket by using the code


Socket mysocket=new Socket(localhost,2000)


Here a socket is created in the local machine itself you can specify the
ip address of the server machine instead of localhost.

Server Side


In the Sever Side a ServerSocket is created.ServerSocket is a different
class rather than socket.It is meant for the server side only.
In the server side the following code is usually used


ServerSocket ssock=new ServerSocket(2000)


As you can see the same port number should be used in the client and server side.

Classes like PrintStream is used to send message from the client to the server.It is established easily like this

PrintStream ps=new PrintStream(mysocket.getOutputStream)
ps.println("my message");

This code in the client side sends message to the server.In the server 
side this message can be easily accepted and displayed using the 
following code

ServerSocket ssock=new ServerSocket(2000)
Socket mysocket=ssock.accept()
DataInputStream d=new DataInputStream(mysocket.getInputStream)
String msgfromserver=d.readLine() 
System.out.println(msgfromserver)


The method accept() is used to accept a connection from the client.Note that you need to surround the statements with a try-catch block to handle all the exceptions.
In this way you can send a message from the client to server.



Thursday, March 8, 2012

Hybrid Inheritance Example Program in C++ to Calculate the Marks and Percentage of a Student



Hybrid Inheritance is the combination of two or more inheritances : single, multiple,multilevel or hierarchical Inheritances. The following is a C++ Program to for Calculating the marks secured by a student.A Parent class with student identification is created and another class called marks is inherited from the main class.This class marks is further inherited by another class called sports and finally the sports class is inherited by the percentage class to calculated the percentage of marks.This is the best example for Hybrid Inheritance in c++The complete source code is provided Below 



 #include<iostream.h>  
 #include<conio.h>  
 #include<stdio.h>  
 #include<string.h>  
 class student_id  
 {  
 //c-madeeasy.blogspot.com  
 int rno;  
 char name[20];  
 public:  
 void read_id()  
 {  
 cout<<"\n\nEnter the Name of the Student : ";  
 gets(name);  
 cout<<"\n\nEnter the Roll No. : ";  
 cin>>rno;  
 }  
 void display_nr()  
 {  
 cout<<"\n\n\t\tSTUDENT REPORT\n\nNAME : ";  
 puts(name);  
 cout<<"\n\nROLL NO. : "<<rno;  
 }  
 };  
 class marks:public student_id  
 {  
 public:  
 int i,mark[3];  
 void read_m()  
 {  
 read_id();  
 for(i=0;i<3;i++)  
 {  
 cout<<"\n\nEnter the Marks Secured in SUBJECT "<<i+1<<" out of 100 : ";  
 cin>>mark[i];  
 }  
 }  
 void display_m()  
 {  
 display_nr();  
 cout<<"\n\n\tMarks Secured ";  
 for(i=0;i<3;i++)  
 cout<<"\n\nSUBJECT "<<i+1<<" : "<<mark[i];  
 }  
 };  
 class sports  
 {  
 public:  
 int sm;  
 void read_sportm()  
 {  
 cout<<"\n\nEnter the marks in SPORTS out of 10 : ";  
 cin>>sm;  
 }  
 };  
 class percentage:public marks,public sports  
 {  
 public:  
 float total,prcntge;  
 void calculate()  
 {  
 read_m();  
 read_sportm();  
 total=0;  
 for(i=0;i<3;i++)  
 {  
 total+=mark[i];  
 }  
 total+=sm;  
 prcntge=(total/310)*100;  
 }  
 void display_totp()  
 {  
 display_m();  
 cout<<"\n\nTOTAL = "<<total;  
 cout<<"\n\nPERCENTAGE = "<<prcntge;  
 }  
 };  
 void main()  
 {  
 int cont;  
 percentage pc;  
 clrscr();  
 do  
 {  
 pc.calculate();  
 clrscr();  
 pc.display_totp();  
 cout<<"\n\nDo You Want to Continue?(1-YES/0-NO)";  
 cin>>cont;  
 }while(cont==1);  
 getch();  
 }  

Which is the Best Photo Watermarking Software

Photo Theft is becoming more and more common in the web with the outburst of social websites like Facebook,Google Plus and Image sharing se...