自己写的socket 多线程 通讯
生活随笔
收集整理的這篇文章主要介紹了
自己写的socket 多线程 通讯
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1?SocketClient.java ?代碼
2 ?SocketServer.java 代碼
package com.cpcnet.socket;import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import com.cpcnet.util.ListUtil; import com.cpcnet.util.MyLogger; import com.cpcnet.util.PropertiesUtil;public class SocketServer {private ServerSocket serverSocket = null;private Socket socket = null;private BufferedReader br = null;private PrintWriter pw = null;private MyLogger logger = new MyLogger(SocketServer.class);private String CMD=PropertiesUtil.getProp("socket.properties", "CMD");private String cmd = null;private String gidListString = null;private String error = PropertiesUtil.getProp("socket.properties", "error");public boolean init(){boolean flag = true;int port = Integer.parseInt(PropertiesUtil.getProp("socket.properties", "port")); //設(shè)定服務(wù)端的端口號(hào) try {serverSocket = new ServerSocket(port);} catch (IOException e) {this.logger.error("SocketServer.java serverSocket = new ServerSocket(PORT); port = " + port);flag = false;e.printStackTrace();} System.out.println("ServerSocket Start:"+serverSocket); return flag;}public boolean start(){boolean flag = true;if(!this.init()){this.logger.error("SocketServer.java init() failed "); }else{while(true){try {socket = serverSocket.accept();System.out.println("Connection accept socket:" + socket);Thread thread = this.initThread(socket, logger);thread.start(); // thread.stop();} catch (IOException e) {this.logger.error("SocketServer.java start() init the br and pw error");try {socket.close();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}e.printStackTrace();return false;} }}return flag;}public Thread initThread( final Socket socket, final MyLogger logger){Thread thread = new Thread(new Runnable(){public void run(){try {//用于接收客戶端發(fā)來的請(qǐng)求 br = new BufferedReader(new InputStreamReader(socket.getInputStream()));//用于發(fā)送返回信息,可以不需要裝飾這么多io流使用緩沖流時(shí)發(fā)送數(shù)據(jù)要注意調(diào)用.flush()方法 pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); } catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}// while(true){if(!this.getMessage()){logger.error("SocketServer.java getMessage() failed " );}if(!this.sendMessage()){logger.error("SocketServer.java sendMessage() failed");}try {Thread.sleep(1);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("close socket and socket = " + socket); // this.close();// }} public void close(){try {br.close();pw.close();socket.close(); // serverSocket.close();} catch (IOException e) {logger.error("SocketServer.java close() error");e.printStackTrace();} }public boolean getMessage(){boolean flag = true;String cmdAndGidListAndMonth = null;try {cmdAndGidListAndMonth = br.readLine();} catch (IOException e) {logger.error("SocketServer.java getMessage() error");e.printStackTrace();return false;} if(cmdAndGidListAndMonth != null){System.out.println("SocketServer.java cmdAndGidListAndMonth = " + cmdAndGidListAndMonth);cmd = cmdAndGidListAndMonth.split("and")[0];gidListString = cmdAndGidListAndMonth.split("and")[1];}return flag;}public boolean sendMessage(){boolean flag = true;if (CMD.equals(cmd)) {String fileBasicPath = PropertiesUtil.getProp("socket.properties", "fileBasicPath");Map<String,List<String>> map_gid_list = new HashMap<String,List<String>>();List gidList = ListUtil.StringToList(gidListString);if(gidList!= null && gidList.size() > 0){for(int i = 0 ; i < gidList.size() ; i ++ ){String filePath = fileBasicPath + gidList.get(i);List<String> gid_list = this.getFilesByGid(filePath);map_gid_list.put((String) gidList.get(i), gid_list);}System.out.println("map_gid_list.toString() = " + map_gid_list.toString());pw.println(map_gid_list.toString());pw.flush(); }else{flag =false;logger.error("SocketServer.java sendMessage error : this.cmd = " + cmd + " this.gidList = " + gidList );}}return flag;}public List<String> getFilesByGid(String filePath){List<String> list = new ArrayList<String>();File sumamryGidFile = new File(filePath);if(!sumamryGidFile.exists()) sumamryGidFile.mkdir();File[] childFiles = sumamryGidFile.listFiles();if(childFiles != null &&childFiles.length > 0){ for(int j = 0 ; j < childFiles.length ; j ++){if(!childFiles[j].isDirectory()){String childFileName = childFiles[j].getName();childFileName = childFileName.split("\\.")[0];childFileName = childFileName.split("_")[4];list.add(childFileName);}}}return list;}});return thread;}public static void main(String[] agrs) { boolean flag = new SocketServer().start();} }
總結(jié)
以上是生活随笔為你收集整理的自己写的socket 多线程 通讯的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java邮件发送
- 下一篇: liferay6.2 struts2