Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners socket programing
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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


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


Advertisement
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
socket programing  how can u pass data from 1-another system by socket class
socket programing in servlet
socket programing in servlet  give me a example to write socket programing in servlet
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
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
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 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  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 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
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
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
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
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
Socket connection - MobileApplications
Socket connection  Hi everyone!! I am a student in Viet Nam... file.xml in server (PC), I want to send to client by socket conection, everyone please help me!! If not use socket, can I use other conection??  Hi Friend
socket - Java Beginners
client program. i have created only one server socket for all the sockets.. is there any exception will occur if i use only one socket for both sign out and sign
 

 

 

DMCA.com