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: server socket variable cannot pass to diff function within the same class

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default server socket variable cannot pass to diff function within the same class

    I use below code to establish socket and write back string to out1 stream to client .

    I accept the socket socket and out1 stream in run() , I use another function p_output_socket() to write back the string to out1 stream , but I find there is nul exception . probably, it is indicate that the server socket variable is null in function
    p_output_socket() . It is ok in run function .
    Pleae advice how I can pass the current server socket variable to function p_output_socket and use it ???
    Below is the code
    import java.io.File;
     import java.io.FileInputStream;
     import java.io.*;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import java.net.*;
     
    import org.apache.commons.httpclient.HttpClient;
     import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
     import org.apache.commons.httpclient.methods.PostMethod;
     import org.apache.commons.httpclient.methods.DeleteMethod;
    import org.apache.commons.httpclient.methods.PutMethod;
    import org.apache.commons.httpclient.methods.*;
    import org.apache.commons.logging.LogFactory;
    import org.apache.commons.logging.*;
    import java.lang.Thread.*;
    /* below use for http get */
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import org.apache.http.*;
     
    import java.io.*;
    import java.nio.channels.*;
    import java.util. * ;
    import java.io. * ;
    import java.io.RandomAccessFile.*;
    import java.io.RandomAccessFile;
     
     
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.impl.*;
    import java.net.URL;
    import java.net.URI;
    import java.io.*;
     
     
    import java.sql.*;
    import javax.sql.*;
    import com.mysql.jdbc.Driver;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
     
     
    public class doComms implements Runnable   {
        public static Socket server;
        public static ServerSocket listener;
        Thread t;
     
    public static Statement stmt ;
     
     
     
        public String line,input,testabc;
       // public  DataOutputStream out1 ;
        public DataInputStream in;
        public static PrintWriter out1;
       doComms() {
     //Socket server;
      //   ServerSocket listener;
     //PrintWriter out1;
     
         //t = new Thread (this,"socket server 4444 run ");
     
         // t.start(); // Start the thread
      try {
    listener = new ServerSocket(4444);
     
           }
         catch ( Exception y )  { y.printStackTrace();  }
     
        }
     
        public void run () {
     
     
     
        testabc="123";
          input="";
     
          try {
     
     
          Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection ("jdbc:mysql://10.1.1.89:3306/mysql", "root", "123456");
    Statement stmt = con.createStatement();
     
           //listener = new ServerSocket(4444);
          //Socket server;
     
     
            doComms connection;
     
              server = listener.accept();
     
     
              // Get input from the client
              System.out.println("Start up socket input stream !!!");
     
              in = new DataInputStream (server.getInputStream());
     
              System.out.println("Start up socket output stream !!!");
              //out1 = new PrintStream(server.getOutputStream());
     
              PrintWriter out1 = new PrintWriter(
                                 new BufferedOutputStream(server.getOutputStream(), 1024), false);
     
              //out1 = new DataOutputStream(server.getOutputStream());
     
          ClientHandler abc = new ClientHandler(server);
     
          Thread socserver = new Thread(abc);
     
               socserver.start();
     
            while((line = in.readLine()) != null && !line.equals(".")) {
              input=input + line;
              if ( line.startsWith("abc") )
               {
                System.out.println("put ok in out socket in docomms");
                out1.println("OK" );
              out1.flush();
            p_output_socket("testing ok");
                }
              else
              {
              System.out.println("put echo  in out socket in docomms");
                out1.println("echo what I get only :" + line);
              out1.flush();
            p_output_socket("testing echo ok");
                }
     
     
     
     
     
     
     
     
     
     
     
     
     
     
            }
     
            // Now write to the client
     
            System.out.println("Overall message is:" + input);
            p_output_socket("Overall message is:" + input);
     
            server.close();
          } catch (Exception e) {
            System.out.println("Exception on socket listen: " + e);
            e.printStackTrace();
          }
        }
       //below print string to output socket
       public  void p_output_socket(String a )
       {
     
         String input1 = this.input;
         String line1 = this.line ;
     
     
         try
         {
         //System.out.println("this testabc " + this.testabc );
         //System.out.println("this input " + this.input );
         //System.out.println("this line " + this.line );
         //System.out.println("a is " + a );
     
     
     
            String aa =a ;
     
              out1.println( aa );
              out1.flush();
     
     
     
           }
          catch ( Exception e) {
            System.out.println("Exception on socket output in DoComms " + e);
            e.printStackTrace();
          }
     
     
     
        }
     
       // above print string to output socket
     
     public static void insert_order_sql (Statement stmt,String v)
      {
        try {
        stmt.executeUpdate("INSERT INTO `o1` (`data` ) " + "VALUES ( '"+v+"' )");
            }
       catch ( Exception w )  { w.printStackTrace();      }
     
       }
     
     
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: server socket variable cannot pass to diff function within the same class

    Find the line the exception is thrown, then backtrack to find how it can be null. Since you have not posted the stack trace, and this code is not an SSCCE, you don't make it easy for anyone to help you out.

    Edit: thread moved from What's wrong with my code

    This thread has been cross posted here:

    http://www.java-forums.org/networking/62126-server-socket-variable-cannot-pass-diff-function-within-same-class.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. trying to pass default params to tail recursion function
    By johnmerlino in forum Algorithms & Recursion
    Replies: 1
    Last Post: July 16th, 2012, 03:21 AM
  2. Replies: 1
    Last Post: January 17th, 2012, 10:25 AM
  3. Diff between spring created been and instance of class
    By tcstcs in forum Web Frameworks
    Replies: 0
    Last Post: April 5th, 2011, 12:54 AM
  4. How to Pass unlimited Arguments to a Function
    By neo_2010 in forum Java Programming Tutorials
    Replies: 2
    Last Post: July 8th, 2009, 11:39 AM
  5. How to Pass unlimited Arguments to a Function
    By neo_2010 in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: July 8th, 2009, 11:39 AM