62,623
社区成员
发帖
与我相关
我的任务
分享
public class SocketServer extends Thread {
private Logger log= Logger.getLogger(SocketServer.class.getName());
private ServerSocket serverSocket;
private int listenPort =8188;
public SocketServer(int listenPort) {
this.listenPort = listenPort;
}
public static Map clientMap=new HashMap();//保存socket的HashMap
public static String returnResult=null;//保存发出通信的的返回结果
private Socket socket;
private boolean isLoop= false;
public void run(){
isLoop=true;
try {
serverSocket= new ServerSocket(listenPort);
log.info("Starting listen......");
String ccsIp="";
CcsInfo ccsInfo=null;
while(isLoop){
socket = serverSocket.accept();
log.info("Listening->RemoteSocketAddress:"+socket.getRemoteSocketAddress()+" InetAddress:"+socket.getInetAddress());
ccsIp=socket.getRemoteSocketAddress().toString().trim();
ccsIp=ccsIp.substring(1,ccsIp.indexOf(":"));
log.info("RemoteSocketIp:"+ccsIp);
clientMap.put(ccsIpsocket);
log.info("Having listen the IP:" +ccsIp+" The ccsID:"+ccsInfo.getCcsId());
new SocketServerThread(socket).start();
}
} catch (Exception e) {
isLoop = false;
log.error(" "+e);
}
}
}
public class SocketServerThread extends Thread {
private Logger log= Logger.getLogger(SocketServerThread.class.getName());
private Socket socket;
private int result;
private boolean isLoop;
ByteArrayOutputStream buf = new ByteArrayOutputStream();
public SocketServerThread(Socket s){
this.socket = s;
}
public void run(){
isLoop=true;
result=0;
InputStream in=null;
try {
in =new BufferedInputStream(socket.getInputStream());
} catch (IOException e){
log.error("The CCS "+socket.getRemoteSocketAddress()+" is unusual:"+e);
}
int readLen=1024;
int count=0;
byte[] readBuf=new byte[readLen];
while(true&&isLoop){
count=0;
try{
do{
count=in.read(readBuf);
if(count==-1){
isLoop=false;
throw new IOException("The receive data length is not right");
}
buf.write(readBuf,0,count);
}while (count==readLen);
log.info("Receive:"+new String(buf.toByteArray()));
}catch(IOException ex){
log.error("The CCS "+socket.getRemoteSocketAddress()+" correspondence is unusual:"+ex);
}
SocketServer.returnResult=new String(buf.toByteArray());//保存返回结果
}
}
}
public class Communicate {
public synchronized String conferenceComm(String ip){
OutputStream out=null;
try {
out = socket.getOutputStream();
} catch (IOException e){
log.error("Connecting the CCS that ip is "+socket.getInetAddress()+" is exception:"+e);
return null;
}
//先发送
String cmdXml="request";
try {
byte[] response =cmdXml.getBytes();
out.write(response);
out.flush();
} catch (IOException e){
log.error("Sending command to CCS that ip is "+socket.getInetAddress()+" is exception:"+e);
return null;
}
log.info("Send:"+cmdXml);
String result=null;
int i=0;
while(i<5){
try {
Thread.sleep(50);
} catch (InterruptedException e){
log.error(e);
}
i++;
result=SocketServer.returnResult;
if(result!=null){
break;
}
}
return result;
}
}
public class SocketServer extends Thread {
private Logger log= Logger.getLogger(SocketServer.class.getName());
private ServerSocket serverSocket;
private int listenPort =8188;
public SocketServer(int listenPort) {
this.listenPort = listenPort;
}
public static Map clientMap=new HashMap();//保存socket的HashMap
public static String returnResult=null;//保存发出通信的的返回结果
private Socket socket;
private boolean isLoop= false;
public void run(){
isLoop=true;
try {
serverSocket= new ServerSocket(listenPort);
log.info("Starting listen......");
String ccsIp="";
CcsInfo ccsInfo=null;
while(isLoop){
socket = serverSocket.accept();
log.info("Listening->RemoteSocketAddress:"+socket.getRemoteSocketAddress()+" InetAddress:"+socket.getInetAddress());
ccsIp=socket.getRemoteSocketAddress().toString().trim();
ccsIp=ccsIp.substring(1,ccsIp.indexOf(":"));
log.info("RemoteSocketIp:"+ccsIp);
clientMap.put(ccsIpsocket);
log.info("Having listen the IP:" +ccsIp+" The ccsID:"+ccsInfo.getCcsId());
new SocketServerThread(socket).start();
}
} catch (Exception e) {
isLoop = false;
log.error(" "+e);
}
}
}
public class SocketServerThread extends Thread {
private Logger log= Logger.getLogger(SocketServerThread.class.getName());
private Socket socket;
private int result;
private boolean isLoop;
ByteArrayOutputStream buf = new ByteArrayOutputStream();
public SocketServerThread(Socket s){
this.socket = s;
}
public void run(){
isLoop=true;
result=0;
InputStream in=null;
try {
in =new BufferedInputStream(socket.getInputStream());
} catch (IOException e){
log.error("The CCS "+socket.getRemoteSocketAddress()+" is unusual:"+e);
}
int readLen=1024;
int count=0;
byte[] readBuf=new byte[readLen];
while(true&&isLoop){
count=0;
try{
do{
count=in.read(readBuf);
if(count==-1){
isLoop=false;
throw new IOException("The receive data length is not right");
}
buf.write(readBuf,0,count);
}while (count==readLen);
log.info("Receive:"+new String(buf.toByteArray()));
}catch(IOException ex){
log.error("The CCS "+socket.getRemoteSocketAddress()+" correspondence is unusual:"+ex);
}
SocketServer.returnResult=new String(buf.toByteArray());//保存返回结果
}
}
}
public class Communicate {
public synchronized String conferenceComm(String ip){
OutputStream out=null;
try {
out = socket.getOutputStream();
} catch (IOException e){
log.error("Connecting the CCS that ip is "+socket.getInetAddress()+" is exception:"+e);
return null;
}
//先发送
String cmdXml="request";
try {
byte[] response =cmdXml.getBytes();
out.write(response);
out.flush();
} catch (IOException e){
log.error("Sending command to CCS that ip is "+socket.getInetAddress()+" is exception:"+e);
return null;
}
log.info("Send:"+cmdXml);
String result=null;
int i=0;
while(i<5){
try {
Thread.sleep(50);
} catch (InterruptedException e){
log.error(e);
}
i++;
result=SocketServer.returnResult;
if(result!=null){
break;
}
}
return result;
}
}
public class SocketServer extends Thread {
private Logger log= Logger.getLogger(SocketServer.class.getName());
private ServerSocket serverSocket;
private int listenPort =8188;
public SocketServer(int listenPort) {
this.listenPort = listenPort;
}
//public static Map clientMap=new HashMap();//保存socket的HashMap //这里移到Communicate类里
//public static String returnResult=null;//保存发出通信的的返回结果 //不要用全局变量,没给线程都修改这里,这个全局变量根本没法保存正确的信息
private Socket socket;
//private boolean isLoop= false; //这个一点意义都没有
public void run(){
//isLoop=true;
try {
serverSocket= new ServerSocket(listenPort);
log.info("Starting listen......");
String ccsIp="";
CcsInfo ccsInfo=null;
//while(isLoop){ //改成while(true)就可以了
while(true){
socket = serverSocket.accept();
log.info("Listening->RemoteSocketAddress:"+socket.getRemoteSocketAddress()+" InetAddress:"+socket.getInetAddress());
ccsIp=socket.getRemoteSocketAddress().toString().trim();
ccsIp=ccsIp.substring(1,ccsIp.indexOf(":"));
log.info("RemoteSocketIp:"+ccsIp);
//clientMap.put(ccsIpsocket); //这里改成如下
Communicate.addSocket(ccsId, ccsIpsocket); //不要把map随意暴露出来,可以通过某个方法去操作
//题外话,这里用客户端IP作为key并不能保证唯一,因为同一个客户端可以启动多个程序通信,这样同一个客户端的socket的IP都是一样的,所以光用IP是不够的,这里怎么解决就留给LZ自己思考了(提示:ip+port等等)。我就当你一个客户端只启动一个进程通信了。
log.info("Having listen the IP:" +ccsIp+" The ccsID:"+ccsInfo.getCcsId()); //这里ccsInfo是什么?估计LZ省略了一些代码,我就当这里已经取到值了,否则就出现空指针错误
//new SocketServerThread(socket).start(); //这里移到Communicate类里,从你的处理流程上看,这里也收不到什么信息
yield(); //缓和一下循环,要不要自己决定
}
} catch (Exception e) {
//isLoop = false; //这个一点意义都没有,出异常,上面的循环就退出了,整个线程也结束了
log.error(" "+e);
}
}
}
public class SocketServerThread extends Thread {
private Logger log= Logger.getLogger(SocketServerThread.class.getName());
private Socket socket;
private int result;
//private boolean isLoop; //这个也是没意义
ByteArrayOutputStream buf = new ByteArrayOutputStream();
public SocketServerThread(Socket s){
this.socket = s;
}
public void run(){
//isLoop=true;
result=0;
InputStream in=null;
try {
in =new BufferedInputStream(socket.getInputStream());
} catch (IOException e){
log.error("The CCS "+socket.getRemoteSocketAddress()+" is unusual:"+e);
}
int readLen=1024;
int count=0;
byte[] readBuf=new byte[readLen];
//while(true&&isLoop){ //这里没必要循环了,除非你想要线程一直处理收信,但是那样的话,你就要做一个通信命令和返信内容相对应的map了,否则你哪次返信是哪个命令的结果
count=0;
try{
do{
count=in.read(readBuf);
if(count==-1){
isLoop=false;
throw new IOException("The receive data length is not right");
}
buf.write(readBuf,0,count);
}while (count==readLen);
log.info("Receive:"+new String(buf.toByteArray()));
}catch(IOException ex){ //这里LZ自己看情况再追加timeout异常的处理
log.error("The CCS "+socket.getRemoteSocketAddress()+" correspondence is unusual:"+ex);
}
//SocketServer.returnResult=new String(buf.toByteArray());//保存返回结果 //不要用全局变量,可以把结果保存到一个map中
Communicate.addResult(socket, new String(buf.toByteArray()));//保存返回结果
// }
}
}
public class Communicate {
private static Map socketMap = new HashMap();
private static Map resultMap = new HashMap();
public static addSocket(String key, Socket socket) {
synchronized(socketMap) {
socketMap.put(key, socket);
}
}
public static addResult(socket, String msg) {
synchronized(socketMap) {
socketMap.put(socket, msg); //题外话,如果B部分循环收信,可以在这里把msg放到一个通信指令和返信对应的map中,然后在放到这个resultMap
}
}
//public synchronized String conferenceComm(String ip){ //这里可以把synchornized去掉了,LZ所说的瓶颈就在这里吧
public String conferenceComm(String ip){
//追加处理start
Socket socket = null;
synchronized(socketMap) {
socket = (Socket)socketMap.get(ip);
}
if (socket == null) {
log.error("socket not found. Ip=" + ip);
return null;
} //追加处理end
OutputStream out=null;
try {
out = socket.getOutputStream();
} catch (IOException e){
log.error("Connecting the CCS that ip is "+socket.getInetAddress()+" is exception:"+e);
return null;
}
//先发送
String cmdXml="request";
try {
byte[] response =cmdXml.getBytes();
out.write(response);
out.flush();
} catch (IOException e){
log.error("Sending command to CCS that ip is "+socket.getInetAddress()+" is exception:"+e);
return null;
}
log.info("Send:"+cmdXml);
String result=null;
/*以下处理不要
int i=0;
while(i<5){
try {
Thread.sleep(50);
} catch (InterruptedException e){
log.error(e);
}
i++;
result=SocketServer.returnResult;
if(result!=null){
break;
}
}
*/ //到此不要
//追加处理start
socket.setSoTimeout(1000*60); //timeout时间自己定
Thread thread = new SocketServerThread(socket); //这里开始收信
thread.start();
thread.join(); //等待收信,实际上收信处理慢的话,这里还是会发生瓶颈
//获取返信结果并返回;
synchronized (resultMap) {
result = resultMap.get(socket);
} //追加处理end
return result;
}
}