package org.hao;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class SocketClient {
public static void main(String args[]) {
test1();
}
public static void test1(){
try{
String str="cmd=update&key=124&data1=101&data2=123&data3=1233&data4=111&table=testhao";//修改好使
ByteArrayInputStream innstream = new ByteArrayInputStream(str.getBytes());
//Socket socket=new Socket("127.0.0.1",4700);
//192.168.229.129
//Socket socket=new Socket("192.168.229.129", 19111);
Socket socket=new Socket("192.168.229.129", 19111);
//BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
BufferedReader sin=new BufferedReader(new InputStreamReader(innstream));
PrintWriter os=new PrintWriter(socket.getOutputStream());
//由Socket对象得到输出流,并构造PrintWriter对象
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//由Socket对象得到输入流,并构造相应的BufferedReader对象
String readline;
os.println(sin.readLine());//将从系统标准输入读入的字符串输出到Server
os.flush();//刷新输出流,使Server马上收到该字符串
readline=is.readLine(); //从系统标准输入读入一字符串
//while(!readline.equals("bye")){//若从标准输入读入的字符串为 "bye"则停止循环
while(readline!=null){
//System.out.println("Client:"+readline);//在系统标准输出上打印读入的字符串
System.out.println("from Server:"+readline);//从Server读入一字符串,并打印到标准输出上
System.out.println("from Server:"+is.readLine());
readline=is.readLine(); //从系统标准输入读入一字符串
} //继续循环
os.close(); //关闭Socket输出流
is.close(); //关闭Socket输入流
socket.close(); //关闭Socket
}catch(Exception e) {
e.printStackTrace();
//System.out.println("Error"+e); //出错,则打印出错信息
}
}
public static void test(){
try{
//String str="cmd=clearall&table=testhao";//清空好使,会换log文件
//String str="cmd=createtable&field=key_data1_data2_data3_data4&tablename=testhao";//建空好使
//String str="cmd=insert&key=124&data1=12&data2=123&data3=1233&data4=123&table=testhao";//好使
String str="cmd=update&key=124&data1=101&data2=123&data3=1233&data4=111&table=testhao";//修改好使
ByteArrayInputStream innstream = new ByteArrayInputStream(str.getBytes());
//Socket socket=new Socket("127.0.0.1",4700);
//192.168.229.129
//Socket socket=new Socket("192.168.229.129", 19111);
Socket socket=new Socket("10.10.83.225", 19111);
//BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
BufferedReader sin=new BufferedReader(new InputStreamReader(innstream));
PrintWriter os=new PrintWriter(socket.getOutputStream());
//由Socket对象得到输出流,并构造PrintWriter对象
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//由Socket对象得到输入流,并构造相应的BufferedReader对象
String readline;
readline=sin.readLine(); //从系统标准输入读入一字符串
//while(!readline.equals("bye")){//若从标准输入读入的字符串为 "bye"则停止循环
while(readline!=null){
os.println(readline);//将从系统标准输入读入的字符串输出到Server
os.flush();//刷新输出流,使Server马上收到该字符串
System.out.println("Client:"+readline);//在系统标准输出上打印读入的字符串
System.out.println("Server:"+is.readLine());//从Server读入一字符串,并打印到标准输出上
System.out.println("Server:"+is.readLine());
readline=sin.readLine(); //从系统标准输入读入一字符串
} //继续循环
os.close(); //关闭Socket输出流
is.close(); //关闭Socket输入流
socket.close(); //关闭Socket
}catch(Exception e) {
e.printStackTrace();
//System.out.println("Error"+e); //出错,则打印出错信息
}
}
}
package org.hao;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServer {
public static void main(String[] args) {
try {
ServerSocket server = null;
try {
server = new ServerSocket(4700);
// 创建一个ServerSocket在端口4700监听客户请求
} catch (Exception e) {
System.out.println("can not listen to:" + e);
// 出错,打印出错信息
}
Socket socket = null;
try {
socket = server.accept();
// 使用accept()阻塞等待客户请求,有客户
// 请求到来则产生一个Socket对象,并继续执行
} catch (Exception e) {
System.out.println("Error." + e);
// 出错,打印出错信息
}
String line;
BufferedReader is = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
// 由Socket对象得到输入流,并构造相应的BufferedReader对象
PrintWriter os = new PrintWriter(socket.getOutputStream());
// 由Socket对象得到输出流,并构造PrintWriter对象
BufferedReader sin = new BufferedReader(new InputStreamReader(
System.in));
// 由系统标准输入设备构造BufferedReader对象
System.out.println("Client:" + is.readLine());
// 在标准输出上打印从客户端读入的字符串
line = sin.readLine();
// 从标准输入读入一字符串
while (!line.equals("bye")) {
// 如果该字符串为 "bye",则停止循环
os.println(line);
// 向客户端输出该字符串
os.flush();
// 刷新输出流,使Client马上收到该字符串
System.out.println("Server:" + line);
// 在系统标准输出上打印读入的字符串
System.out.println("Client:" + is.readLine());
// 从Client读入一字符串,并打印到标准输出上
line = sin.readLine();
// 从系统标准输入读入一字符串
} // 继续循环
os.close(); // 关闭Socket输出流
is.close(); // 关闭Socket输入流
socket.close(); // 关闭Socket
server.close(); // 关闭ServerSocket
} catch (Exception e) {
System.out.println("Error:" + e);
// 出错,打印出错信息
}
}
}