postheadericon java socket programming example



In the era of network programming means write such a script that can be executed on multiple computers / platform. In J2SE, a package java.net provide the number of API that can be used to write code for the communication between multiple computers / platforms. The java.net package of J2SE provide the sport for two important protocol of the network that is TCP and UDP.

The Socket in java provide the way of communication between two or more computers using Transmission Control Protocol (TCP). Here we have  socket  on both side at Client as well as server. A client socket try to communicate to server socket. If everything is fine server accept the connection and communication process begin.

Here I am going to demonstrate a simple example of server socket programming. I have created a java package  bsr and my class that have the definition of socket MySocket.java is placed inside this package. 

I have also created two more class to simulate the behavior of client and server namely sender and  receiver. I used plain text editor to create my script.




MySocket.java


package bsr;
/*A code from www.bipinrupadiya.com*/
import java.util.*;
import java.net.*;
import java.io.*;
public class MySocket
{
String serverName;
int port;
String frame;
public MySocket()
{
this.serverName="localhost";
this.port=6061;
this.frame="BipinRupadiya";
}
public MySocket(int port)
{
this.port=port;
}
public MySocket(String serverName,int port,String frame)
{
this.serverName=serverName;
this.port=port;
this.frame=frame;
}

public void sendFrame(String frame) throws Exception
{
ServerSocket ss=new ServerSocket(port);
Socket s=ss.accept();

byte bFrame[]=frame.getBytes();
OutputStream os=s.getOutputStream();
os.write(bFrame);
ss.close();
s.close();

    }
 
    public String receiveFrame() throws Exception
   {
Socket s=new Socket(serverName,port);
InputStream is=s.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String str=br.readLine();
s.close();
return(str);
    }

}


sender / client


//Sender Side.
import bsr.*;
/*A code from www.bipinrupadiya.com*/
public class sender
{
public static void main(String args[])throws Exception
{

String myString="BipinRupadiya";
/*

// to read data from user
Scanner sc=new Scanner(System.in);
System.out.println("Enter String(only alphabets allowed) :");
myString=sc.next();

*/

MySocket m = new MySocket();
m.sendFrame(myString);
}

}

receiver / server


//receiver Side.
import bsr.*;
/*A code from www.bipinrupadiya.com*/
public class receiver
{
public static void main(String args[])throws Exception
{

MySocket m = new MySocket();
String myString=m.receiveFrame();
System.out.println("\nString : "+myString);
}
}




To execute this code

  • compile bsr package
  • compile sender.java
  • compile receiver.java
  • open two command prompt
  • in 1st command prompt execute sender.java
  • in 2nd command prompt execute receiver.java

0 comments:

Follow by Email

Blog Archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.