Results 1 to 2 of 2
  1. #1
    chuikingman is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

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

    I try below code .
    I use this class to accpet server socket and write back string to the socket connection .
    The server socket is accept in the functon run and out stream is established in it .
    But when I call this out1 stream in another function p_output_scoket(string a ) , there is null exception .
    Probably, it is show the server is null within this function p_output_socket .
    But the server is ok in the function run .

    So, please advice how I can pass the current value server to the function p_output_socket from run ???

    Below is the code I use
    Java 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
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

Similar Threads

  1. Replies: 0
    Last Post: 10-23-2011, 10:49 PM
  2. How to pass a javascript function into APPLET PARAM
    By Ambergahill in forum Java Applets
    Replies: 0
    Last Post: 11-28-2010, 04:57 PM
  3. pass a variable from one object to another
    By kev670 in forum New To Java
    Replies: 3
    Last Post: 11-25-2010, 01:20 AM
  4. Pass multiple object types to one function
    By desmo in forum New To Java
    Replies: 3
    Last Post: 10-08-2010, 07:51 AM
  5. How to pass a variable to another frame.
    By DJCali in forum New To Java
    Replies: 10
    Last Post: 10-14-2009, 03:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •