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 2 of 2

Thread: Writing to Socket from another method or class

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Writing to Socket from another method or class

    Hey everyone,

    I've hit a wall with a project that I'm working on and I was hoping you guys might be able to help me out.

    I am creating a socket and keep the socket open to read/write so that the program can react as data is changed live. I created a second class running in a seperate thread that I would like to be able to use the socket in the first class to write data from time. Does any body know how I can do that?

    Kind of what I'm trying to do:

    public class ClassA {
       public void MethodA() {
         Socket socket = new Socket(server, port);
         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
         BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));	
       }
    }
    public class ClassB {
     public void MethodB() {
      // Here I'd like to either use the BufferedWriter to write to the socket, or some how call a method from ClassA to write to the existing socket
     }
    }
    Last edited by sync0s; July 26th, 2014 at 09:46 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,776
    My Mood
    Mellow
    Thanks
    235
    Thanked 715 Times in 701 Posts

    Default Re: Writing to Socket from another method or class

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Pass the Socket object to the other class(es) that need it either:

    1. Through each subsequent class' constructor,
    2. Using a mutator method in each class after the Socket exists, or
    3. Using a accessor method in the class that owns the Socket object.

Similar Threads

  1. Replies: 2
    Last Post: May 27th, 2014, 12:36 PM
  2. Writing a class - class not found
    By nepperso in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 23rd, 2013, 12:51 AM
  3. Problems in reading/writing from/to a socket
    By Fabgio in forum Java Networking
    Replies: 5
    Last Post: July 2nd, 2013, 01:33 PM
  4. Replies: 1
    Last Post: January 17th, 2013, 06:01 AM
  5. how to get url, from browser in the socket class?
    By chinni in forum Java Networking
    Replies: 0
    Last Post: November 6th, 2009, 09:34 AM