messageReceiveList;
60 |
61 | public UDPSocket(Context context) {
62 |
63 | this.mContext = context;
64 |
65 | int cpuNumbers = Runtime.getRuntime().availableProcessors();
66 | // 根据CPU数目初始化线程池
67 | mThreadPool = Executors.newFixedThreadPool(cpuNumbers * Config.POOL_SIZE);
68 | // 记录创建对象时的时间
69 | lastReceiveTime = System.currentTimeMillis();
70 |
71 | messageReceiveList = new ArrayList<>();
72 |
73 | Log.d(TAG, "创建 UDP 对象");
74 | // createUser();
75 | }
76 |
77 | public void addOnMessageReceiveListener(OnMessageReceiveListener listener) {
78 | messageReceiveList.add(listener);
79 | }
80 |
81 | /**
82 | * 创建本地用户信息
83 | */
84 | private void createUser() {
85 | if (localUser == null) {
86 | localUser = new Users();
87 | }
88 | if (remoteUser == null) {
89 | remoteUser = new Users();
90 | }
91 |
92 | localUser.setImei(DeviceUtil.getDeviceId(mContext));
93 | localUser.setSoftVersion(DeviceUtil.getPackageVersionCode(mContext));
94 |
95 | if (WifiUtil.getInstance(mContext).isWifiApEnabled()) {// 判断当前是否是开启热点方
96 | localUser.setIp("192.168.43.1");
97 | } else {// 当前是开启 wifi 方
98 | localUser.setIp(WifiUtil.getInstance(mContext).getLocalIPAddress());
99 | remoteUser.setIp(WifiUtil.getInstance(mContext).getServerIPAddress());
100 | }
101 | }
102 |
103 |
104 | public void startUDPSocket() {
105 | if (client != null) return;
106 | try {
107 | // 表明这个 Socket 在设置的端口上监听数据。
108 | client = new DatagramSocket(CLIENT_PORT);
109 | client.setReuseAddress(true);
110 | if (receivePacket == null) {
111 | // 创建接受数据的 packet
112 | receivePacket = new DatagramPacket(receiveByte, BUFFER_LENGTH);
113 | }
114 |
115 | startSocketThread();
116 | } catch (SocketException e) {
117 | e.printStackTrace();
118 | }
119 | }
120 |
121 | /**
122 | * 开启接收数据的线程
123 | */
124 | private void startSocketThread() {
125 | clientThread = new Thread(new Runnable() {
126 | @Override
127 | public void run() {
128 | receiveMessage();
129 | }
130 | });
131 | isThreadRunning = true;
132 | clientThread.start();
133 | Log.d(TAG, "开启 UDP 数据接收线程");
134 |
135 | startHeartbeatTimer();
136 | }
137 |
138 | /**
139 | * 处理接受到的消息
140 | */
141 | private void receiveMessage() {
142 | while (isThreadRunning) {
143 | try {
144 | if (client != null) {
145 | client.receive(receivePacket);
146 | }
147 | lastReceiveTime = System.currentTimeMillis();
148 | Log.d(TAG, "receive packet success...");
149 | } catch (IOException e) {
150 | Log.e(TAG, "UDP数据包接收失败!线程停止");
151 | stopUDPSocket();
152 | e.printStackTrace();
153 | return;
154 | }
155 |
156 | if (receivePacket == null || receivePacket.getLength() == 0) {
157 | Log.e(TAG, "无法接收UDP数据或者接收到的UDP数据为空");
158 | continue;
159 | }
160 |
161 | String strReceive = new String(receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength());
162 | Log.d(TAG, strReceive + " from " + receivePacket.getAddress().getHostAddress() + ":" + receivePacket.getPort());
163 |
164 | //解析接收到的 json 信息
165 | notifyMessageReceive(strReceive);
166 | // 每次接收完UDP数据后,重置长度。否则可能会导致下次收到数据包被截断。
167 | if (receivePacket != null) {
168 | receivePacket.setLength(BUFFER_LENGTH);
169 | }
170 | }
171 | }
172 |
173 | /**
174 | * 将消息通过接口发送到每个页面
175 | *
176 | * @param strReceive
177 | */
178 | private void notifyMessageReceive(String strReceive) {
179 | for (OnMessageReceiveListener listener : messageReceiveList) {
180 | if (listener != null) {
181 | listener.onMessageReceived(strReceive);
182 | }
183 | }
184 | }
185 |
186 | public void stopUDPSocket() {
187 | isThreadRunning = false;
188 | receivePacket = null;
189 | stopHeartbeatTimer();
190 | if (clientThread != null) {
191 | clientThread.interrupt();
192 | }
193 | if (mThreadPool != null) {
194 | mThreadPool.shutdown();
195 | }
196 | if (client != null) {
197 | client.close();
198 | client = null;
199 | }
200 | if (timer != null) {
201 | timer.exit();
202 | }
203 | }
204 |
205 | /**
206 | * 启动心跳,timer 间隔十秒
207 | */
208 | public void startHeartbeatTimer() {
209 | if (timer == null) {
210 | timer = new HeartbeatTimer();
211 | }
212 | timer.setOnScheduleListener(new HeartbeatTimer.OnScheduleListener() {
213 | @Override
214 | public void onSchedule() {
215 | Log.d(TAG, "timer is onSchedule...");
216 | long duration = System.currentTimeMillis() - lastReceiveTime;
217 | Log.d(TAG, "duration:" + duration);
218 | if (duration > TIME_OUT) {//若超过两分钟都没收到我的心跳包,则认为对方不在线。
219 | Log.d(TAG, "超时,对方已经下线");
220 | // 刷新时间,重新进入下一个心跳周期
221 | lastReceiveTime = System.currentTimeMillis();
222 | } else if (duration > HEARTBEAT_MESSAGE_DURATION) {//若超过十秒他没收到我的心跳包,则重新发一个。
223 | JSONObject jsonObject = new JSONObject();
224 | try {
225 | jsonObject.put(Config.MSG, Config.HEARTBREAK);
226 | } catch (JSONException e) {
227 | e.printStackTrace();
228 | }
229 | sendMessage(jsonObject.toString());
230 | }
231 | }
232 |
233 | });
234 | timer.startTimer(0, 1000 * 5);
235 | }
236 |
237 | public void stopHeartbeatTimer() {
238 | if (timer != null) {
239 | timer.exit();
240 | timer = null;
241 | }
242 | }
243 |
244 | /**
245 | * 发送心跳包
246 | *
247 | * @param message
248 | */
249 | public void sendMessage(final String message) {
250 | mThreadPool.execute(new Runnable() {
251 | @Override
252 | public void run() {
253 | try {
254 | BROADCAST_IP = WifiUtil.getBroadcastAddress();
255 | Log.d(TAG, "BROADCAST_IP:" + BROADCAST_IP);
256 | InetAddress targetAddress = InetAddress.getByName(BROADCAST_IP);
257 |
258 | DatagramPacket packet = new DatagramPacket(message.getBytes(), message.length(), targetAddress, CLIENT_PORT);
259 |
260 | client.send(packet);
261 |
262 | // 数据发送事件
263 | Log.d(TAG, "数据发送成功");
264 |
265 | } catch (UnknownHostException e) {
266 | e.printStackTrace();
267 | } catch (IOException e) {
268 | e.printStackTrace();
269 | }
270 |
271 | }
272 | });
273 | }
274 |
275 |
276 | }
277 |
--------------------------------------------------------------------------------
/AppSocket/src/main/java/melo/com/androidsocket/utils/DeviceUtil.java:
--------------------------------------------------------------------------------
1 | package melo.com.androidsocket.utils;
2 |
3 | import android.app.Service;
4 | import android.content.Context;
5 | import android.content.pm.PackageInfo;
6 | import android.content.pm.PackageManager;
7 | import android.content.pm.PackageManager.NameNotFoundException;
8 | import android.net.wifi.WifiInfo;
9 | import android.net.wifi.WifiManager;
10 | import android.os.PowerManager;
11 | import android.provider.Settings;
12 | import android.provider.Settings.SettingNotFoundException;
13 | import android.telephony.TelephonyManager;
14 | import android.text.TextUtils;
15 | import android.util.DisplayMetrics;
16 |
17 | import java.io.BufferedReader;
18 | import java.io.File;
19 | import java.io.FileFilter;
20 | import java.io.IOException;
21 | import java.io.InputStreamReader;
22 | import java.lang.reflect.Method;
23 | import java.net.NetworkInterface;
24 | import java.util.Collections;
25 | import java.util.List;
26 | import java.util.regex.Pattern;
27 |
28 |
29 | /**
30 | * 获取设备的信息
31 | *
32 | * @author melo
33 | */
34 | public final class DeviceUtil {
35 |
36 | /**
37 | * IMEI.
Returns the unique device ID, for example, the IMEI for GSM and the MEID
38 | * or ESN for CDMA phones. Return null if device ID is not available.
39 | *
40 | * Requires Permission: READ_PHONE_STATE
41 | *
42 | * @param context
43 | * @return
44 | */
45 | public synchronized static String getDeviceId(Context context) {
46 | if (context == null) {
47 | return "";
48 | }
49 |
50 | String imei = "";
51 |
52 | try {
53 | TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
54 | if (tm == null || TextUtils.isEmpty(tm.getDeviceId())) {
55 | // 双卡双待需要通过phone1和phone2获取imei,默认取phone1的imei。
56 | tm = (TelephonyManager) context.getSystemService("phone1");
57 | }
58 |
59 | if (tm != null) {
60 | imei = tm.getDeviceId();
61 | }
62 | } catch (SecurityException e) {
63 | e.printStackTrace();
64 | }
65 |
66 |
67 | return imei;
68 | }
69 |
70 | /**
71 | * Returns the serial number of the SIM, if applicable. Return null if it is
72 | * unavailable.
73 | *
74 | * Requires Permission: READ_PHONE_STATE
75 | *
76 | * @param context
77 | * @return
78 | */
79 | public synchronized static String getSimSerialNumber(Context context) {
80 | if (context == null) {
81 | return "";
82 | }
83 | final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
84 | return tm.getSimSerialNumber();
85 | }
86 |
87 | /**
88 | * A 64-bit number (as a hex string) that is randomly generated on the
89 | * device's first boot and should remain constant for the lifetime of the
90 | * device. (The value may change if a factory reset is performed on the
91 | * device.)
92 | *
93 | * @param context
94 | * @return
95 | */
96 | public synchronized static String getAndroidID(Context context) {
97 | return Settings.Secure.getString(context.getContentResolver(),
98 | Settings.Secure.ANDROID_ID);
99 | }
100 |
101 | /**
102 | * 操作系统版本
103 | *
104 | * @return
105 | */
106 | public static String getOSversion() {
107 | return android.os.Build.VERSION.RELEASE;
108 | }
109 |
110 | /**
111 | * 设备商
112 | *
113 | * @return
114 | */
115 | public static String getManufacturer() {
116 | return android.os.Build.MANUFACTURER;
117 | }
118 |
119 | /**
120 | * 设备型号
121 | *
122 | * @return
123 | */
124 | public static String getModel() {
125 | return android.os.Build.MODEL;
126 | }
127 |
128 | /**
129 | * 序列号
130 | *
131 | * @return
132 | */
133 | public static String getSerialNumber() {
134 | String serial = null;
135 | try {
136 | Class> c = Class.forName("android.os.SystemProperties");
137 | Method get = c.getMethod("get", String.class);
138 | serial = (String) get.invoke(c, "ro.serialno");
139 | } catch (Exception ignored) {
140 | }
141 | return serial;
142 | }
143 |
144 | /**
145 | * SD CARD ID
146 | *
147 | * @return
148 | */
149 | public static synchronized String getSDcardID() {
150 | try {
151 | String sdCid = null;
152 | String[] memBlkArray = new String[]{"/sys/block/mmcblk0", "/sys/block/mmcblk1", "/sys/block/mmcblk2"};
153 | for (String memBlk : memBlkArray) {
154 | File file = new File(memBlk);
155 | if (file.exists() && file.isDirectory()) {
156 | Process cmd = Runtime.getRuntime().exec("cat " + memBlk + "/device/cid");
157 | BufferedReader br = new BufferedReader(new InputStreamReader(cmd.getInputStream()));
158 | sdCid = br.readLine();
159 | if (!TextUtils.isEmpty(sdCid)) {
160 | return sdCid;
161 | }
162 | }
163 | }
164 | return null;
165 | } catch (IOException e) {
166 | // TODO Auto-generated catch block
167 | e.printStackTrace();
168 | return null;
169 | }
170 | }
171 |
172 | /**
173 | * 获取mac地址
174 | *
175 | * @param context
176 | * @return
177 | */
178 | public static String getMac(Context context) {
179 | if (context == null) {
180 | return "";
181 | }
182 | String mac = null;
183 | try {
184 | final WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
185 | if (wifi != null) {
186 | WifiInfo info = wifi.getConnectionInfo();
187 | if (null != info && info.getMacAddress() != null) {
188 | mac = info.getMacAddress();
189 | }
190 | }
191 | } catch (Exception e) {
192 | e.printStackTrace();
193 | }
194 | return mac;
195 | }
196 |
197 | /**
198 | * 获取mac地址
199 | * 可以突破android6.0的限制
200 | *
201 | * @return
202 | */
203 | public static String getWifiMacAddress() {
204 | try {
205 | String interfaceName = "wlan0";
206 | List interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
207 | for (NetworkInterface intf : interfaces) {
208 | if (!intf.getName().equalsIgnoreCase(interfaceName)) {
209 | continue;
210 | }
211 |
212 | byte[] mac = intf.getHardwareAddress();
213 | if (mac == null) {
214 | return "";
215 | }
216 |
217 | StringBuilder buf = new StringBuilder();
218 | for (byte aMac : mac) {
219 | buf.append(String.format("%02X:", aMac));
220 | }
221 | if (buf.length() > 0) {
222 | buf.deleteCharAt(buf.length() - 1);
223 | }
224 | return buf.toString();
225 | }
226 | } catch (Exception ex) {
227 | } // for now eat exceptions
228 | return "";
229 | }
230 |
231 | /**
232 | * 获取IMSI
233 | *
234 | * @param context
235 | * @return
236 | */
237 | public static String getIMSI(Context context) {
238 |
239 | TelephonyManager tm = (TelephonyManager) context
240 | .getSystemService(Context.TELEPHONY_SERVICE);
241 |
242 | return tm.getSubscriberId();
243 |
244 | }
245 |
246 | /**
247 | * get sim serial number
248 | */
249 | public static String getSimSerialNum(Context context) {
250 | TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
251 | return tm.getSimSerialNumber();
252 | }
253 |
254 | /**
255 | * 获取屏幕的分辨率
256 | *
257 | * @param context
258 | * @return int array with 2 items. The first item is width, and the second is height.
259 | */
260 | public static int[] getScreenResolution(Context context) {
261 | DisplayMetrics dm = context.getResources().getDisplayMetrics();
262 |
263 | int[] resolution = new int[2];
264 | resolution[0] = dm.widthPixels;
265 | resolution[1] = dm.heightPixels;
266 |
267 | return resolution;
268 | }
269 |
270 | /**
271 | * 获取WIFI的Mac地址
272 | *
273 | * @param context
274 | * @return Wifi的BSSID即mac地址
275 | */
276 | public static String getWifiBSSID(Context context) {
277 | if (context == null) {
278 | return null;
279 | }
280 |
281 | String mac = null;
282 | WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
283 | WifiInfo info = wm.getConnectionInfo();
284 | if (info != null) {
285 | mac = info.getBSSID();// 获得本机的MAC地址
286 | }
287 |
288 | return mac;
289 | }
290 |
291 | public static String getPackageVersion(Context context) {
292 | PackageManager packageManager = context.getPackageManager();
293 | PackageInfo packInfo;
294 | try {
295 | packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
296 | return packInfo.versionName;
297 | } catch (NameNotFoundException e) {
298 | e.printStackTrace();
299 | }
300 |
301 | return null;
302 | }
303 |
304 | public static int getPackageVersionCode(Context context) {
305 | PackageManager packageManager = context.getPackageManager();
306 | PackageInfo packInfo;
307 | try {
308 | packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
309 | return packInfo.versionCode;
310 | } catch (NameNotFoundException e) {
311 | e.printStackTrace();
312 | }
313 |
314 | return 0;
315 | }
316 |
317 | /**
318 | * 获取系统休眠时间。
319 | *
320 | * @return
321 | */
322 | public static int getScreenOffTimeOut(Context context) {
323 | int sleepTime;
324 | try {
325 | sleepTime = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);
326 | } catch (SettingNotFoundException e) {
327 | e.printStackTrace();
328 | sleepTime = 15 * 1000;
329 | }
330 | return sleepTime;
331 | }
332 |
333 |
334 | public static boolean isScreenOn(Context context) {
335 | PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
336 | return powerManager.isScreenOn();
337 | }
338 |
339 | /**
340 | * Gets the number of cores available in this device, across all processors.
341 | * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"
342 | *
343 | * @return The number of cores, or 1 if failed to get result
344 | */
345 | public static int getCPUNumCores() {
346 | try {
347 | //Get directory containing CPU info
348 | File dir = new File("/sys/devices/system/cpu/");
349 | //Filter to only list the devices we care about
350 | File[] files = dir.listFiles(new FileFilter() {
351 | @Override
352 | public boolean accept(File pathname) {
353 | //Check if filename is "cpu", followed by a single digit number
354 | if (Pattern.matches("cpu[0-9]", pathname.getName())) {
355 | return true;
356 | }
357 | return false;
358 | }
359 | });
360 | //Return the number of cores (virtual CPU devices)
361 | return files.length;
362 | } catch (Exception e) {
363 | return 1;
364 | }
365 | }
366 |
367 | /**
368 | * 获取系统参数
369 | *
370 | * @param configName
371 | * @return
372 | */
373 |
374 | public static String getSystemConf(String configName) {
375 | try {
376 | Process process = Runtime.getRuntime().exec("getprop " + configName);
377 | InputStreamReader ir = new InputStreamReader(process.getInputStream());
378 | BufferedReader input = new BufferedReader(ir);
379 | String value = input.readLine();
380 | input.close();
381 | ir.close();
382 | process.destroy();
383 | return value;
384 | } catch (Exception e) {
385 | e.printStackTrace();
386 | }
387 | return "";
388 | }
389 |
390 | /**
391 | * 获取硬件版本
392 | *
393 | * @return
394 | */
395 | public static String getHardwareVersion() {
396 | return getSystemConf("ro.hardware");
397 | }
398 |
399 | /**
400 | * 获取rom版本
401 | */
402 | public static String getRomVersion() {
403 | return getSystemConf("ro.mediatek.version.release");
404 | }
405 |
406 | /**
407 | * 获取hq rom版本
408 | */
409 | private static String gethqRomVersion() {
410 | return getSystemConf("ro.huaqin.version.release");
411 | }
412 |
413 | public static String getShowhqRomVersion() {
414 | String showHq = "hq";
415 | String hqRomVer = gethqRomVersion();
416 | if (TextUtils.isEmpty(hqRomVer) == false) {
417 | String[] s = hqRomVer.split("_");
418 | if (s != null && s.length >= 3) {
419 | showHq = s[2];
420 | }
421 | }
422 | return showHq;
423 | }
424 |
425 | /**
426 | * 获取installed apk版本
427 | */
428 | public static PackageInfo getInstalledAppInfo(Context context, String pname) {
429 | try {
430 | List packages = context.getPackageManager().getInstalledPackages(0);
431 | if (packages != null) {
432 | for (PackageInfo pinfo : packages) {
433 | if (pinfo != null && pinfo.packageName.equals(pname)) {
434 | return pinfo;
435 | }
436 | }
437 | }
438 | } catch (Exception e) {
439 | e.printStackTrace();
440 | }
441 | return null;
442 | }
443 |
444 | }
--------------------------------------------------------------------------------
/AppSocket/src/main/java/melo/com/androidsocket/utils/HeartbeatTimer.java:
--------------------------------------------------------------------------------
1 | package melo.com.androidsocket.utils;
2 |
3 | import java.util.Timer;
4 | import java.util.TimerTask;
5 |
6 | /**
7 | * Created by melo on 2017/9/21.
8 | */
9 |
10 | public class HeartbeatTimer {
11 |
12 | private Timer timer;
13 | private TimerTask task;
14 | private OnScheduleListener mListener;
15 |
16 | public HeartbeatTimer() {
17 | timer = new Timer();
18 | }
19 |
20 | public void startTimer(long delay, long period) {
21 | task = new TimerTask() {
22 | @Override
23 | public void run() {
24 | if (mListener != null) {
25 | mListener.onSchedule();
26 | }
27 | }
28 | };
29 | timer.schedule(task, delay, period);
30 | }
31 |
32 | public void exit() {
33 | if (task != null) {
34 | task.cancel();
35 | }
36 | if (timer != null) {
37 | timer.cancel();
38 | }
39 | }
40 |
41 | public interface OnScheduleListener {
42 | void onSchedule();
43 | }
44 |
45 | public void setOnScheduleListener(OnScheduleListener listener) {
46 | this.mListener = listener;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/AppSocket/src/main/java/melo/com/androidsocket/utils/WifiUtil.java:
--------------------------------------------------------------------------------
1 | package melo.com.androidsocket.utils;
2 |
3 | import android.content.Context;
4 | import android.net.DhcpInfo;
5 | import android.net.wifi.WifiInfo;
6 | import android.net.wifi.WifiManager;
7 |
8 | import java.lang.reflect.Method;
9 | import java.net.InetAddress;
10 | import java.net.InterfaceAddress;
11 | import java.net.NetworkInterface;
12 | import java.util.Enumeration;
13 | import java.util.Iterator;
14 | import java.util.List;
15 |
16 | /**
17 | * Created by melo on 2017/9/23.
18 | */
19 |
20 | public class WifiUtil {
21 |
22 | private static final String TAG = "LocationUtils";
23 |
24 | private static volatile WifiUtil instance = null;
25 |
26 | private WifiManager mWifiManager;
27 |
28 | private Context mContext;
29 |
30 | private WifiUtil(Context context) {
31 | mContext = context;
32 | mWifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
33 | }
34 |
35 | public static WifiUtil getInstance(Context context) {
36 | if (instance == null) {
37 | synchronized (WifiUtil.class) {
38 | if (instance == null) {
39 | instance = new WifiUtil(context);
40 | }
41 | }
42 | }
43 | return instance;
44 | }
45 |
46 | public boolean isWifiApEnabled() {
47 | try {
48 | Method method = mWifiManager.getClass().getMethod("isWifiApEnabled");
49 | method.setAccessible(true);
50 | return (Boolean) method.invoke(mWifiManager);
51 |
52 | } catch (NoSuchMethodException e) {
53 | // TODO Auto-generated catch block
54 | e.printStackTrace();
55 | } catch (Exception e) {
56 | e.printStackTrace();
57 | }
58 |
59 | return false;
60 | }
61 |
62 | public String getLocalIPAddress() {
63 | WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
64 | return intToIp(wifiInfo.getIpAddress());
65 | }
66 |
67 | public String getServerIPAddress() {
68 | DhcpInfo mDhcpInfo = mWifiManager.getDhcpInfo();
69 | return intToIp(mDhcpInfo.gateway);
70 | }
71 |
72 | private static String intToIp(int i) {
73 | return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "."
74 | + ((i >> 24) & 0xFF);
75 | }
76 |
77 | /**
78 | * @return 优先获取网卡地址
79 | */
80 | public static String getBroadcastAddress() {
81 | String broadcast = getBroadcastAddress("p2p");
82 | if (broadcast == null) {
83 | return getBroadcastAddress("wlan0");
84 | }
85 | return broadcast;
86 | }
87 |
88 | /**
89 | * @param netCardName 网卡名称
90 | * @return 获取的广播地址
91 | */
92 | public static String getBroadcastAddress(String netCardName) {
93 | try {
94 | Enumeration eni = NetworkInterface
95 | .getNetworkInterfaces();
96 | while (eni.hasMoreElements()) {
97 | NetworkInterface networkCard = eni.nextElement();
98 | if (networkCard.getDisplayName().startsWith(netCardName)) {
99 | List ncAddrList = networkCard
100 | .getInterfaceAddresses();
101 | Iterator ncAddrIterator = ncAddrList.iterator();
102 | while (ncAddrIterator.hasNext()) {
103 | InterfaceAddress networkCardAddress = ncAddrIterator.next();
104 | InetAddress address = networkCardAddress.getAddress();
105 | if (!address.isLoopbackAddress()) {
106 | String hostAddress = address.getHostAddress();
107 | if (hostAddress.indexOf(":") > 0) {
108 | // case : ipv6
109 | continue;
110 | } else {
111 | // case : ipv4
112 | String broadcastAddress = networkCardAddress.getBroadcast().getHostAddress();
113 | return broadcastAddress;
114 | }
115 | }
116 | }
117 | }
118 | }
119 | } catch (Exception e) {
120 | e.printStackTrace();
121 | }
122 |
123 | return null;
124 | }
125 |
126 |
127 | }
128 |
--------------------------------------------------------------------------------
/AppSocket/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/AppSocket/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/AppSocket/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/AppSocket/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/AppSocket/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/AppSocket/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/AppSocket/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/AppSocket/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/AppSocket/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/AppSocket/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/AppSocket/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/AppSocket/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/AppSocket/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/AppSocket/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/AppSocket/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/AppSocket/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/AppSocket/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/AppSocket/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/AppSocket/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/AppSocket/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/AppSocket/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/AppSocket/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/AppSocket/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/AppSocket/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/AppSocket/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/AppSocket/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AndroidSocket
3 |
4 |
--------------------------------------------------------------------------------
/AppSocket/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/AppSocket/src/test/java/melo/com/androidsocket/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package melo.com.androidsocket;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AndroidSocket
2 |
3 | 项目介绍地址:
4 |
5 | http://www.jianshu.com/p/61de9478c9aa
6 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 |
6 |
7 |
8 | defaultConfig {
9 | applicationId "melo.com.app"
10 | minSdkVersion 21
11 | targetSdkVersion 26
12 | versionCode 1
13 | versionName "1.0"
14 |
15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16 |
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 |
26 | }
27 |
28 | dependencies {
29 | implementation fileTree(dir: 'libs', include: ['*.jar'])
30 |
31 | implementation 'com.android.support:appcompat-v7:26.1.0'
32 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
33 | implementation 'com.android.support:design:26.1.0'
34 | testImplementation 'junit:junit:4.12'
35 | androidTestImplementation 'com.android.support.test:runner:1.0.1'
36 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
37 |
38 | implementation project(':AppSocket')
39 | }
40 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/melo/com/app/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package melo.com.app;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("melo.com.app", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/java/melo/com/app/MainActivity.java:
--------------------------------------------------------------------------------
1 | package melo.com.app;
2 |
3 | import android.os.Bundle;
4 | import android.support.design.widget.FloatingActionButton;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.support.v7.widget.Toolbar;
7 |
8 | import butterknife.BindView;
9 | import butterknife.ButterKnife;
10 | import butterknife.OnClick;
11 | import melo.com.androidsocket.socket.SocketManager;
12 |
13 | public class MainActivity extends AppCompatActivity {
14 |
15 | @BindView(R.id.toolbar)
16 | Toolbar toolbar;
17 | @BindView(R.id.fab)
18 | FloatingActionButton fab;
19 | private SocketManager manager;
20 |
21 | @Override
22 | protected void onCreate(Bundle savedInstanceState) {
23 | super.onCreate(savedInstanceState);
24 | setContentView(R.layout.activity_main);
25 | ButterKnife.bind(this);
26 | manager = SocketManager.getInstance(this);
27 | manager.startUdpConnection();
28 | }
29 |
30 | @OnClick(R.id.fab)
31 | public void onViewClicked() {
32 |
33 | }
34 |
35 | @Override
36 | protected void onDestroy() {
37 | super.onDestroy();
38 | manager.stopSocket();
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
20 |
21 |
22 |
23 |
24 |
25 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/content_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
24 |
25 |
38 |
39 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
5 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 | 16dp
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | App
3 | Settings
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/test/java/melo/com/app/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package melo.com.app;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 |
5 | repositories {
6 | google()
7 | jcenter()
8 | mavenCentral()
9 | }
10 | dependencies {
11 | classpath 'com.android.tools.build:gradle:3.0.0'
12 |
13 | // ButterKnife
14 | classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
15 | // NOTE: Do not place your application dependencies here; they belong
16 | // in the individual module build.gradle files
17 | }
18 | }
19 |
20 | allprojects {
21 | repositories {
22 | google()
23 | jcenter()
24 | }
25 | }
26 |
27 | task clean(type: Delete) {
28 | delete rootProject.buildDir
29 | }
30 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itsMelo/AndroidSocket/525513e25e8cae78f1518b4d2d8d68e82db18fb6/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Nov 27 10:59:32 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':AppSocket', ':app'
2 |
--------------------------------------------------------------------------------