Java sockets listener server program
/*================================================================= */
import java.net.*;
import java.io.*;
import java.util.*;
public class lslist01 extends Thread{
protected Socket ls_socket;
protected InputStream is;
protected OutputStream os;
public lslist01(Socket s){ls_socket = s;}
public static void main(String[] args){
try{
ServerSocket ses = new ServerSocket(3457);
while(true){
lslist01 ddd = new lslist01(ses.accept());
ddd.start();
}
}
catch (IOException e){
System.out.println("lslist01 ERROR accept("+e+")");
System.exit(1);
}
}
public void run(){
System.out.println("Thread started " + new Date() );
try{is = new DataInputStream (ls_socket.getInputStream());
os = new DataOutputStream(ls_socket.getOutputStream());
String bom = ls_get_value_comma(is,"BOM");
String sid = ls_get_value_comma(is,"SID=");
String uid = ls_get_value_comma(is,"UID=");
String cust = ls_get_value_comma(is,"CUST=");
String func = ls_get_value_comma(is,"FUNC=");
String fid = ls_get_value_comma(is,"FID=");
String fi_rec = ls_get_value_rest(is,"DATA=");
if (!fi_rec.endsWith(",EOM")){
fi_rec="ERROR Expected(,EOM)";
System.out.println(fi_rec);
try{os.write(fi_rec.getBytes());}
catch (IOException e){System.out.println("lslist01t WNO("+e+")");}
}
ls_write_file(fi_rec.substring(0,fi_rec.length()-4)
,sid,uid,cust,func,fid);
try{os.write("OK".getBytes());}
catch (IOException e){System.out.println("lslist01t WNO("+e+")");}
try{
is.close();
os.close();
ls_socket.close();
}
catch (IOException e){System.out.println("ER close WNO("+e+")");}
System.out.println("Thread ended");
}
catch (IOException e){
ls_write_log("ERROR Unable to get streams "+e);
return;
}
// try{Thread.currentThread().setPriority(MIN_PRIORITY);}
// catch(Exception e){System.out.println("ER Prior "+e);}
// try{Thread.currentThread().sleep(100);}
// catch(Exception e){System.out.println("ER sleep "+e);}
}
// =========================================================
String ls_get_value_rest(InputStream i1,String sss){
String line = ls_get_rest(i1);
ls_write_log("Read ("+line+")");
if (!line.startsWith(sss)){
String mm="ERROR Expected("+sss+")";
ls_write_log(mm);
System.exit(16);
}
return line.substring(sss.length());
}
// =========================================================
String ls_get_rest(InputStream i2){
byte b[] = new byte[500004];
int ct =0;
int pt =0;
try{ct = i2.read(b,pt,b.length-pt);
if (ct < 1) return null;
pt=ct;
}
catch (IOException e){return null;}
while(b[pt-1] != 0){
try{ct = i2.read(b,pt,b.length-pt);
if (ct < 1) return null;
pt=pt+ct;
System.out.print("("+ct+")");
}
catch (IOException e){return null;}
}
return (new String(b, 0, pt-1));
}
// =========================================================
String ls_get_value_comma(InputStream i3,String sss){
byte bb[] = new byte[304];
int cc =0;
int pp =0;
try{cc = i3.read(bb,pp,1);
if (cc < 1) return null;
pp=cc;
}
catch (IOException e){return null;}
while(bb[pp-1] != ','){
try{cc = i3.read(bb,pp,1);
if (cc < 1) return null;
pp=pp+cc;
}
catch (IOException e){return null;}
}
String lll = new String(bb,0, pp-1);
ls_write_log("Read ("+lll+")");
if (!lll.startsWith(sss)){
String mmm="ERROR Expected("+sss+")";
ls_write_log(mmm);
System.exit(16);
}
return lll.substring(sss.length());
}
// =========================================================
void ls_write_log(String s){
FileWriter ls_log;
PrintWriter flog;
try{ls_log = new FileWriter("LSLOG.TXT",true);
flog = new PrintWriter(ls_log);
flog.println(s);
if (!s.startsWith("Read (DATA=")) System.out.println(s);
else System.out.println("DATA length("+s.length()+")");
flog.close();
}
catch (IOException e) {
System.out.println("ERROR log file "+e);
System.exit(1);}
}
// =========================================================
void ls_write_file(String s_data
,String s_sid
,String s_uid
,String s_cust
,String s_func
,String s_fid
){
FileWriter ls_file;
PrintWriter fo;
boolean open_type;
open_type=s_func.equals("APPEND");
try{ls_file = new FileWriter("d:/LAIS_DEVT/"
+s_sid +"_"
+s_uid +"_"
+s_cust +"_"
+s_fid
,open_type);
fo = new PrintWriter(ls_file);
}
catch (IOException e) {
System.out.println("ERROR file write "+e);
return;}
fo.println(s_data);
fo.close();
}
// =========================================================
}
/*================================================================= */
Contact marshall_alan@hotmail.com
Recycled Books Shed main page
Recycled Books Shed page 2 - photos, links, other things