Created
September 18, 2014 07:35
-
-
Save egraldlo/9d1f73e11e45bb534c1c to your computer and use it in GitHub Desktop.
client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cmysqlclient; | |
import java.io.*; | |
import java.net.*; | |
import java.nio.ByteBuffer; | |
public class Client { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO 自动生成的方法存根 | |
/* | |
{ | |
byte b[] = new byte[4]; | |
b[0] = b[1] = 0; | |
b[2] = 2; | |
b[3] = 4; | |
ByteArrayInputStream bintput = new ByteArrayInputStream(b); | |
DataInputStream dintput = new DataInputStream(bintput); | |
int i = -1; | |
try { | |
i = dintput.readInt(); | |
} catch (IOException e) { | |
// TODO 自动生成的 catch 块 | |
e.printStackTrace(); | |
} | |
System.out.println(i); | |
// int t = Integer.valueOf(b.toString()); | |
// System.out.println(t); | |
char c[] = new char[1024]; | |
c[0] = 56; | |
c[1] = c[2] = 0; | |
c[3] = 94; | |
System.out.println(c); | |
}*/ | |
Connection c = new Connection(); | |
// String s = "58.198.176.153"; | |
String s = "10.11.1.192"; | |
// int port = 34568; | |
int port = 8001; | |
c.init(s, port); | |
System.out.println(s); | |
// get sql statement from system in | |
String sql = ""; | |
try { | |
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); | |
sql = sin.readLine(); | |
} catch (IOException e) { | |
// TODO 自动生成的 catch 块 | |
e.printStackTrace(); | |
} | |
c.send_sql(sql); | |
String result_set = c.receive_claims_result(); | |
// String result_set = c.receive_result_set(); | |
System.out.println("receive from server: "); | |
System.out.println(result_set); | |
c.close(); | |
} | |
} | |
class Connection { | |
Socket socket; | |
// BufferedReader sin; | |
InputStream in; | |
OutputStream out; | |
// private ServerSocket server; | |
public Connection() { | |
} | |
public int init(String s, int port) { | |
int ret = 0; | |
try { | |
socket = new Socket(s, port); | |
// sin = new BufferedReader(new InputStreamReader(System.in)); //由系统标准输入设备构造BufferedReader对象 | |
// in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //由Socket对象得到输入流,并构造相应的BufferedReader对象 | |
in = socket.getInputStream(); | |
// out = new PrintWriter(socket.getOutputStream(), true); //由Socket对象得到输出流,并构造PrintWriter对象 | |
out = socket.getOutputStream(); | |
} catch (IOException e) { | |
// TODO 自动生成的 catch 块 | |
e.printStackTrace(); | |
} | |
// s = "12345456"; | |
return ret; | |
} | |
public int send_sql(String s) { | |
int ret = 1; | |
if (s == null) { | |
return -1; | |
} | |
else { | |
try { | |
out.write(s.getBytes()); | |
} catch (IOException e) { | |
// TODO 自动生成的 catch 块 | |
e.printStackTrace(); | |
} | |
} | |
System.out.println("send to server:"+s); | |
return ret; | |
} | |
public String receive_claims_result() { | |
String ret = null; | |
System.out.println(Integer.SIZE+" "+Integer.MAX_VALUE); | |
char c; | |
byte int_buf[] = new byte[4]; | |
int status = -1; | |
int length = -1; | |
int read_count = 0; | |
if ((status = read_int()) == -1) { | |
System.out.println("read status error"); | |
return ret; | |
} | |
System.out.println("status is: "+ status); | |
if ((length = read_int()) != -1) { | |
System.out.println("read length error"); | |
return ret; | |
} | |
System.out.println("length is: "+ length); | |
return ret; | |
} | |
/* | |
public String receive_result_set(){ | |
String ret = null; | |
char int_buf[] = new char[1024]; | |
int read_char = 0; | |
// char a[] = new char[1000]; | |
try { | |
//This method will block until some input is available, an I/O error occurs, or the end of the stream is reached. | |
// ret = in.read(a); | |
// ret = in.readLine(); | |
// in.read(); | |
if ((read_char = in.read(int_buf)) != -1) { | |
//System.out.println(read_char+"=="+ (char)read_char); | |
System.out.print(int_buf); | |
// for (int i = 8 ; i < 124; ++i) { | |
// System.out.print(int_buf[i]); | |
// } | |
} | |
} catch (IOException e) { | |
// TODO 自动生成的 catch 块 | |
e.printStackTrace(); | |
} | |
return new String(int_buf, 0, read_char); | |
} | |
*/ | |
public void close() { | |
try { | |
socket.close(); | |
in.close(); | |
out.close(); | |
// sin.close(); | |
} catch (IOException e) { | |
// TODO 自动生成的 catch 块 | |
e.printStackTrace(); | |
} | |
} | |
int read_int() { | |
byte buf[] = new byte[4]; | |
int value = -1; | |
int read_count = 0; | |
try { | |
System.out.println(in.available()); | |
if (in.available() > 0 && (read_count = in.read(buf, 0, 4)) != -1) { | |
ByteArrayInputStream bintput = new ByteArrayInputStream(buf); | |
DataInputStream dintput = new DataInputStream(bintput); | |
try { | |
value = dintput.readInt(); | |
System.out.println(buf + "-" + value); | |
} catch (IOException e) { | |
// TODO 自动生成的 catch 块 | |
e.printStackTrace(); | |
} | |
} | |
} catch (IOException e) { | |
// TODO 自动生成的 catch 块 | |
e.printStackTrace(); | |
} | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment