socket programing

socket programing

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

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.









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.ADS_TO_REPLACE_1 import java.awt.*; import javax.swing.*; class TextFieldArray extends JFrame
programing
programing
programing
programing
programing
programing
programing
programing
programing
programing
Programing Help - Java Beginners
Programing Help  how to design a filebrowser in java "witout using Swing
cnc programing course
cnc programing course  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: cnc programing... the topic "cnc programing course". Also tell me which is the good
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
socket programming
socket programming  how to create server socket in web service...it will read parameter from file
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
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
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 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
cnc programing classes near me
cnc programing classes near me  Hi, I am beginner in Data Science... programing classes near me Try to provide me good examples or tutorials links so that I can learn the topic "cnc programing classes near me". Also tell
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
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
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
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
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
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
JAVA Socket Programming
JAVA Socket Programming  i m working on a java project which has multiple client and one server instance how can i make server respond to multiple client simultaneously
java programing
programing - IoC
programing - WebSevices
java programing
Javascirpt Programing
C++ programing
c programing
c programing
java programing
java programing
Java Programing
socket programming in java - WebSevices
socket programming in java  hello i hv made a socket program using which a server and a client can communicate.can both the server side and client side program run on the same machine or 2 different computers (connected by LAN
Socket and Servert -Jsp
Socket and Servert -Jsp  I have two servers,one server we have one servlet and another sever we have one jsp when a client send the request to first server first servelt executes and inside that servlet we need to use the socket
Socket Programming in Servlets - JSP-Servlet
Socket Programming in Servlets  How to write a TCP/IP socket program inside a servlet...Is it possible....Please give your suggestions......Thanks in advance
Version of com.espertech>esperio-socket dependency
List of Version of com.espertech>esperio-socket dependency
Version of com.payneteasy>socket-nio dependency
List of Version of com.payneteasy>socket-nio dependency

Ads