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(); } } }