1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| import java.io.*;
import java.util.Date;
import java.net.*;
import java.sql.*;
import oracle.jdbc.*;
public class TCPServer
{
public static void main(String args[])
{
ServerSocket server = null;
Socket connected = null;
byte[] v_msg_in;
String url = "jdbc:oracle:thin:@192.168.140.20:1521:orcl";
String usager ="****";
String motdepasse="*****";
Connection connexion = null;
int i;
int k;
int j = 0;
String v_msg_out_hex;
String HEXES = "0123456789ABCDEF";
try
{
Class.forName ("oracle.jdbc.driver.OracleDriver");
System.out.println("Pilote chargé");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("ERREUR : Driver manquant.");
}
try
{
connexion = DriverManager.getConnection(url,usager, motdepasse);
System.out.println("connécté");
}
catch (SQLException se)
{
System.out.println("ERREUR : Connection.");
}
try{
server = new ServerSocket (1245);
System.out.println("socket créé");
}
catch(Exception e){
System.out.println("erreur lors de la creation de sockets: " + e);
}
try{
connected = server.accept();
System.out.println("client accepté");
}
catch(Exception e){
System.out.println("Incountered an error in accepting the client: " + e);
}
System.out.println( " LE CLIENT"+" "+ connected.getInetAddress() +":"+connected.getPort()+" EST CONNECTE ");
//Echange de données
try{
OutputStream OutToClient = new BufferedOutputStream(connected.getOutputStream());
InputStream inFromClient = new BufferedInputStream (connected.getInputStream());
while ( true )
{
v_msg_in = new byte[500];
k = inFromClient.read(v_msg_in);
System.out.println(new String(v_msg_in, "UTF-8"));
String sql = "{? = call mo_interface(?)}" ;
final StringBuilder v_mes_hex = new StringBuilder( 2 * v_msg_in.length );
for ( final byte b : v_msg_in ) {
v_mes_hex.append(HEXES.charAt((b & 0xF0) >> 4))
.append(HEXES.charAt((b & 0x0F)));
}
System.out.println("mes_hex: "+v_mes_hex.toString());
CallableStatement stm = connexion.prepareCall(sql);
stm.registerOutParameter(1,OracleTypes.RAW);//java.sql.Types.VARCHAR);
stm.setString(2,v_mes_hex.toString());
stm.execute();
byte[] v_msg_out =stm.getBytes(1) ;
System.out.println(new String(v_msg_out, "UTF-8"));
try{
OutToClient.write(v_msg_out,0,v_msg_out.length);
OutToClient.flush();
}
catch(IOException e) {
System.out.println("erreur: "+e);
}
}
}
catch(Exception e){
System.out.println("There was an error in receiving or sending data: " + e);
}
try{
connected.close();
}
catch(Exception e){
System.out.println("Was unable to close, either the connections where cut or connection timed out " + e);
}
}
} |