2011-12-05~12-30北京电科院一体化项目Socket
服务端:CalcConsoleServer.java
package socket;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
/**
* socket服务端
* @author tanfei
*
*/
public class CalcConsoleServer extends Thread {
private static ServerSocket _server = null;
/**
* 关闭服务器
* @throws IOException
*/
public static void closeServer() throws IOException {
if (_server != null) {
_server.close();
_server = null;
}
}
public void run() {
try {
_server = new ServerSocket(9100);
System.out.println("socket服务已经启动...");
while (true) {
Socket _client = _server.accept();
new Thread(new ConsoleClient(_client)).start();
}
} catch (Exception err) {
err.printStackTrace();
} finally {
try {
closeServer();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class ConsoleClient implements Runnable {
private Socket _client;
public ConsoleClient(Socket _client) {
this._client = _client;
}
public void run() {
// 当前线程
Thread _nowThread = Thread.currentThread();
// 输出流
PrintWriter out = null;
// 数据输入流
DataInputStream inStream = null;
try {
Map<String, String> inputMap = new HashMap<String, String>();
out = new PrintWriter(_client.getOutputStream());
inStream = new DataInputStream(_client.getInputStream());
byte[] tmpBuf = new byte[4096];
// recv 接收数据
int r = inStream.read(tmpBuf);
String result =null;
if(r!=-1){
result=new String(tmpBuf, 0, r, "UTF-8");
}
// 根据注册码调用不同的业务 [此处客户端发送过来的报文解析可以放到存储过程去做,然后由解析到的交易码得到调用不同业务的方法]
// 交易码与不同业务操作的对应关系要由开发人员事先录入到数据库中去。
// 如 交易码111902, 对应的业务操作方法为: findCusInfoByConsNo
// 交易码111901, 对应的业务操作方法为: modifyCusPwd
// 交易码111903, 对应的业务操作方法为: findConsInfoByConsNo
// 交易码110525, 对应的业务操作方法为: dsTransCheck
// 存储过程返回的内容为,业务类对应的方法名,解析后的字段串(即要传给对应业务类操作方法的参数)
businessCallSel(out, inputMap, result);
} catch (Exception err) {
err.printStackTrace();
} finally {
if (_nowThread.isAlive()) {
_nowThread = null;
}
if (null != inStream) {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
out.close();
}
}
}
/**
* 业务调用
*
* @param out
* @param inputMap
* @param requestStr
* 请求string内容
* @throws IOException
*/
public void businessCallSel(PrintWriter out,
Map<String, String> inputMap, String requestStr) throws IOException {
System.out.println("输入报文:"+requestStr);
StringBuffer responseStr = new StringBuffer("");
responseStr.append("服务器已经收到报文:"+requestStr);
System.out.println("输出报文:"+responseStr);
//以字节流数组传输数据比较安全
_client.getOutputStream().write(responseStr.toString().getBytes("UTF-8"));
//out.print(responseStr.toString());
//out.flush();
}
}
public static void main(String[] args) {
CalcConsoleServer server = new CalcConsoleServer();
server.start();
}
}
客户端:CalcConsoleClient.java
package socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
/**
* socket客户端
* @author tanfei
*
*/
public class CalcConsoleClient {
public int timeout=16000;
/**
* 向服务端发送数据及其接收数据
* @param reqString
* @param ip
* @param port
* @return
*/
public String sendAndReceive(String reqString, String ip, int port)
{
Socket clientSocket = null ;
DataOutputStream outStream = null ;
DataInputStream inStream = null ;
String receiveStr = "" ;
try
{
if (clientSocket == null)
{
clientSocket = new Socket(ip, port) ;
clientSocket.setSoTimeout(timeout) ;
outStream = new DataOutputStream(clientSocket.getOutputStream()) ;
inStream = new DataInputStream(clientSocket.getInputStream()) ;
}
outStream.write(reqString.getBytes()) ;
outStream.flush() ;
byte[] tmpBuf = new byte[4096] ;
//接收数据
int r = inStream.read(tmpBuf) ;
receiveStr = new String(tmpBuf, 0, r, "UTF-8");
System.out.println("客户端收到报文:" + receiveStr);
}catch(java.net.ConnectException ce){
ce.printStackTrace();
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
outStream.close() ;
inStream.close() ;
clientSocket.close() ;
}
catch (Exception err)
{
err.printStackTrace();
}
}
return receiveStr ;
}
public static void main(String[] args) {
CalcConsoleClient client = new CalcConsoleClient();
client.sendAndReceive("客户端报文","127.0.0.1",9100);
}
}