socket programing

Ads

 
 

Share on Google+Share on Google+

venigalla srikanth
socket programing
2 Answer(s)      5 years and 7 months ago
Posted in : Java Beginners

how can u pass data from 1-another system by socket class?

Ads
View Answers

March 5, 2012 at 8:45 PM


ReceiverMain.java

import java.net.*; import java.io.*; class RequesterMain {

ObjectOutputStream out;
ObjectInputStream in;
Socket connection=null;
String message,message1;
RequesterMain(){}
void run()
{
    try
    {


        connection=new Socket("localhost",19999);
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader stdin=new BufferedReader(isr);
        out=new ObjectOutputStream(connection.getOutputStream());
        in=new ObjectInputStream(connection.getInputStream());
        System.out.println("connected to port"+connection.getLocalPort());
        try
        {
        message=(String)in.readObject();
        System.out.println(message);
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Exception is"+e);
        }
        message="";
        System.out.println("Enter the  number :");
        message1=stdin.readLine();
        out.writeObject(message1);
        out.flush();

        try
        {
        message=(String)in.readObject();
        System.out.println(message);
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Exception is"+e);
        }
        System.out.println(" The binary  form of  number is:"+message);

    }
    catch(UnknownHostException ae)
    {
        System.out.println("Exception is"+ae);
    }
    catch(IOException e)
    {
        System.out.println("Exception is"+e);
    }
    try
    {
    out.close();
        in.close();
        connection.close();
    }
    catch(IOException e)
    {
        System.out.println("Exception is"+e);
    }

}


public  static void main(String args[])throws IOException
{
    RequesterMain s=new RequesterMain();
    s.run();
}

}

ServerMain.java

import java.net.*; import java.io.*; class ServerMain {

ServerSocket server;
ObjectOutputStream out;
ObjectInputStream in;
Socket connection1=null;
String message,message1,message2="";
int i=7;
ServerMain(){}
void run()
{
    try
    {
        System.out.println("waiting  for  connection");
        message="hi  i  am server";
        server=new ServerSocket(19999,10);

        connection1=server.accept();
        System.out.println("connected to port"+connection1.getLocalPort());
        out=new ObjectOutputStream(connection1.getOutputStream());
        in=new ObjectInputStream(connection1.getInputStream());
        out.writeObject(message);
        out.flush();
        try
        {
        message1=(String)in.readObject();
        System.out.println(message1);
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Exception is"+e);
        }
        int m;
        int n=Integer.parseInt(message1);
        message1="";
        while(n>0)
        {
         m=n%2;
             message1=message1+m;
         n=n/2;

         i--;
        }
        System.out.println(message1);
         n=message1.length();
        System.out.println("string in reversed order is ");
        for(int i=n-1;i>=0;i--)
        {
        char c=message1.charAt(i);
        message2+=c;
        }

        out.writeObject(message2);
        out.flush();            

    }
    catch(UnknownHostException ae){
        System.out.println("Exception is"+ae);
    }
    catch(IOException e)
    {
        System.out.println("Exception is"+e);
    }
    try
    {
    out.close();
        in.close();
        connection1.close();
    }
    catch(IOException e)
    {
        System.out.println("Exception is"+e);
    }   }



public  static void main(String args[])throws IOException
{
    ServerMain s=new ServerMain();
    s.run();
}

}

run first ServerMain in one cmd and ReceiverMain.java in another cmd.



March 5, 2012 at 8:45 PM


ReceiverMain.java

import java.net.*; import java.io.*; class RequesterMain {

ObjectOutputStream out;
ObjectInputStream in;
Socket connection=null;
String message,message1;
RequesterMain(){}
void run()
{
    try
    {


        connection=new Socket("localhost",19999);
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader stdin=new BufferedReader(isr);
        out=new ObjectOutputStream(connection.getOutputStream());
        in=new ObjectInputStream(connection.getInputStream());
        System.out.println("connected to port"+connection.getLocalPort());
        try
        {
        message=(String)in.readObject();
        System.out.println(message);
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Exception is"+e);
        }
        message="";
        System.out.println("Enter the  number :");
        message1=stdin.readLine();
        out.writeObject(message1);
        out.flush();

        try
        {
        message=(String)in.readObject();
        System.out.println(message);
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Exception is"+e);
        }
        System.out.println(" The binary  form of  number is:"+message);

    }
    catch(UnknownHostException ae)
    {
        System.out.println("Exception is"+ae);
    }
    catch(IOException e)
    {
        System.out.println("Exception is"+e);
    }
    try
    {
    out.close();
        in.close();
        connection.close();
    }
    catch(IOException e)
    {
        System.out.println("Exception is"+e);
    }

}


public  static void main(String args[])throws IOException
{
    RequesterMain s=new RequesterMain();
    s.run();
}

}

ServerMain.java

import java.net.*; import java.io.*; class ServerMain {

ServerSocket server;
ObjectOutputStream out;
ObjectInputStream in;
Socket connection1=null;
String message,message1,message2="";
int i=7;
ServerMain(){}
void run()
{
    try
    {
        System.out.println("waiting  for  connection");
        message="hi  i  am server";
        server=new ServerSocket(19999,10);

        connection1=server.accept();
        System.out.println("connected to port"+connection1.getLocalPort());
        out=new ObjectOutputStream(connection1.getOutputStream());
        in=new ObjectInputStream(connection1.getInputStream());
        out.writeObject(message);
        out.flush();
        try
        {
        message1=(String)in.readObject();
        System.out.println(message1);
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Exception is"+e);
        }
        int m;
        int n=Integer.parseInt(message1);
        message1="";
        while(n>0)
        {
         m=n%2;
             message1=message1+m;
         n=n/2;

         i--;
        }
        System.out.println(message1);
         n=message1.length();
        System.out.println("string in reversed order is ");
        for(int i=n-1;i>=0;i--)
        {
        char c=message1.charAt(i);
        message2+=c;
        }

        out.writeObject(message2);
        out.flush();            

    }
    catch(UnknownHostException ae){
        System.out.println("Exception is"+ae);
    }
    catch(IOException e)
    {
        System.out.println("Exception is"+e);
    }
    try
    {
    out.close();
        in.close();
        connection1.close();
    }
    catch(IOException e)
    {
        System.out.println("Exception is"+e);
    }   }



public  static void main(String args[])throws IOException
{
    ServerMain s=new ServerMain();
    s.run();
}

}

run first ServerMain in one cmd and ReceiverMain.java in another cmd.


Ads









Related Tutorials/Questions & Answers:
socket programing in servlet
socket programing in servlet  give me a example to write socket programing in servlet
socket programing
socket programing  how can u pass data from 1-another system by socket class
Advertisements
SOCKET PROGRAMING IN THE JAVA FOR NETWORKING CONCEPT
SOCKET PROGRAMING IN THE JAVA FOR NETWORKING CONCEPT  I WANT TO KNOW ABOUT THE JAVA SOCKET PROGRAMING FROM BASIC LEVEL AND HOW CAN I WORK ON IT I KNOW THE JAVA PROGRAMING.SO PLEASE HELP ME HOW CAN I DO SOCKET PROGRAMING
Java programing
Java programing   need help wrting this Write a program that has an array of 5 Strings, and determines which ones are palindromes (letter-case does not matter). thanks
programing question
programing question  how do i use loops(for,while) to add components in java   Hi Friend, The below code might help you. import java.awt.*; import javax.swing.*; class TextFieldArray extends JFrame{ JLabel l[]=new
Programing Help - Java Beginners
Programing Help  how to design a filebrowser in java "witout using Swing
socket programming
socket programming  how to create server socket in web service...it will read parameter from file
Java Socket
Java Socket  What is a socket?  Sockets are communication channels, which facilitate inter-process communication. A socket is one end... such as the Internet,it uses a software component called a socket. The socket opens
Socket Programming
Socket Programming  How to connect a computer through IP only and check whether it is up and running? I can't see any method in Socket programming where I can pass only IP address as argument. I need the pass port number also
Java programing help
Java programing help  Can you help me create this program? Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower, or Mixed). As they enter the data, assign
javascript programing error - JSP-Servlet
javascript programing error  hi deepak i m using explorer to run javascript program in this there is no option to check error will u plz tell me the way to check error in explorer as we can check with mozila
Java Socket Programming
Java Socket Programming  Hi, What is Java Socket Programming? Can anyone give the examples of Java Socket Programming? Thanks   Hi, Please see the tutorial:Overview of Networking through JAVA Thanks
java programing problem - Java Beginners
java programing problem  Create a class named Movie that can be used with your video rental business. The Movie class should track the Motion picture Association of America rating , ID Number, and movie title with appropriate
Socket and ports
Socket and ports       In common language we can say that the socket is one of the most... with a socket. Socket classes are used to establish a connection between client
What is Java Client Socket?
What is Java Client Socket?  Hi, What is client socket in Java..., The client socket is basic communication interface between networked computers. The socket that make connectivity with socket client and with a socket server
Java socket programming
Java socket programming  Hey i want to transfer a file from Server to Client or vice-versa using JAVA socket programming. Can you please guide me
socket programming question
socket programming question   can you respond to me the multiple clients chat application java code and a server that manages the clients using the socket programming in java
socket programming question
socket programming question   can you respond to me the multiple clients chat application java code and a server that manages the clients using the socket programming in java
socket programming question
socket programming question   can you respond to me the multiple clients chat application java code and a server that manages the clients using the socket programming in java
guys,, need help,, in java programing,, arrays
guys,, need help,, in java programing,, arrays  create a program where you will input 10 numbers and arrange it in ascending way using arrays
Setting source port on a Java Socket?
Setting source port on a Java Socket?  Is it possible to explicitly set the the source port on a Java Socket

Ads

 
Advertisement null

Ads