Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Access the Same Socket

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Access the Same Socket

    hey all;
    please i have a problem...and wana your help and support

    - I made client application to connect to a server
    - Client application contains multiclass:
    1.Main Class
    Open In It Socket With Server

    package GlobalSocketPackage;

    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;

    public class MainClass implements Runnable
    {

    public static Socket server;
    public static BufferedReader br;
    public static BufferedWriter bw;

    /////////////////////////////////////////////////////////
    public MainClass() {
    System.out.println("Default Con.");
    }
    /////////////////////////////////////////////////////////
    public MainClass(String m) {

    try{
    server = new Socket("10.10.10.1",5);
    }

    }catch(Exception ex){System.out.println("error"+ex);}

    }
    /////////////////////////////////////////////////////////
    public void WriteRead() throws IOException, InterruptedException

    {
    br = new BufferedReader(new InputStreamReader(server.getInputStream()));
    bw = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
    Thread th=new Thread();
    th.start();
    }
    //////////////////////////////////////////////////////////

    public static void main(String arg[])throws IOException, InterruptedException
    {
    MainClass Client=new MainClass("");
    Client.WriteRead();
    while(true)
    try{
    System.out.println("Connected");
    }catch(Exception exx){}
    }

    public void run() {
    throw new UnsupportedOperationException("Not supported yet.");
    }
    }


    2.Call Class
    Try through it to Access The Same Socket Of Main Class

    package GlobalSocketPackage;

    //Problem1
    //Note: by using this class i cannot send any data on the opened socket ("10.10.10.1",5)

    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.OutputStreamWriter;


    public class CallClass1 {

    public static void main(String args[])throws IOException, InterruptedException
    {
    try{

    //Just To Knew Current Opened Port
    MainClass mc= new MainClass();
    mc.WriteRead.bw = new BufferedWriter(new OutputStreamWriter(MainClass.server.getOutputStrea m()));
    mc.WriteRead.bw.write("Send On the Same Socket");
    mc.WriteRead.bw.flush();

    }catch(Exception exx){}
    }
    }

    // Need to send Data on the same Opened Socket from another Class In same Package Or Application
    --> What I want is to make 2 classes.....
    One has the Opened Socket
    and the other one use this Socket to send output Streams through it without make reconnection on the same socket with the server

    plz help .. thanks so much
    Last edited by ahmmnhwa; October 29th, 2009 at 10:09 AM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Access the Same Socket

    Id have a look at Writing the Server Side of a Socket (The Java™ Tutorials > Custom Networking > All About Sockets) at specifically at the bottom of that page.

    // Json

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Access the Same Socket

    THANKS json for your concern...
    but iam only able to write the client side because i didnot have the access to modify anything on the server
    so i need only to have 2 classes "as Client" one open in it the socket with server
    and the other class access this socket and write / read anything to.from the server through it.....plz help...i appreciate you so much

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Access the Same Socket

    I take it these two classes are in the same client and not two different programs on start up?

    Something like this would possibly work.

    public class MainClass {
     
        public static void main(final String... arguments) {
            try{
                final Socket socket = new Socket("10.10.10.1",5);  // Open the connection to the server
                final Client client = new Client(socket);  // Create a client using our client class passing in the socket
                final Thread thread = new Thread(client);  // Create a thread to run the client
                thread.start();  // Start the client thread
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
     
    public class Client implements Runnable {
     
        private Socket socket;
     
        public Client(final Socket socket) {
            this.socket = socket; // Store a reference to the socket
        }
     
        public void run() {
            // Write to the socket and do whatever you like
        }
    }

    // Json

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Access the Same Socket

    Really thanks sooooooooooo much Json...u r very good person

    but simply i want two .java classes

    1.MainClass.java
    Create Socket with Server

    2.ClientCaller.java
    Access the Opened Socket Of MainClass.java

    these 2 .java are separately in 2 files but in the same package....i knew that i talk alot about my problem..but really wana its solution
    and thaaaaaaaaaaaaaaaaaaaank u very much for your help

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Access the Same Socket

    Just use the code I posted above then, or even do it without the threading.

    // Json

Similar Threads

  1. access database connectivity from outside an application
    By suchirag in forum JDBC & Databases
    Replies: 0
    Last Post: October 29th, 2009, 03:03 AM
  2. Random Access File
    By silver_unicorn in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 12th, 2009, 02:51 AM
  3. Random Access File
    By silver_unicorn in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 10th, 2009, 11:44 PM
  4. Replies: 1
    Last Post: May 7th, 2009, 05:31 AM
  5. Replies: 2
    Last Post: October 8th, 2008, 12:03 AM