package com.actiview.asynclib;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.koushikdutta.async.AsyncServer;
import com.koushikdutta.async.AsyncServerSocket;
import com.koushikdutta.async.AsyncSocket;
import com.koushikdutta.async.ByteBufferList;
import com.koushikdutta.async.DataEmitter;
import com.koushikdutta.async.Util;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.callback.DataCallback;
import com.koushikdutta.async.callback.ListenCallback;
import com.koushikdutta.async.callback.WritableCallback;
import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.AsyncHttpGet;
import com.koushikdutta.async.http.AsyncHttpResponse;
import com.koushikdutta.async.http.WebSocket;
import com.koushikdutta.async.http.callback.HttpConnectCallback;
import com.koushikdutta.async.http.server.AsyncHttpServer;
import com.koushikdutta.async.http.server.AsyncHttpServerRequest;
import com.koushikdutta.async.http.server.AsyncHttpServerResponse;
import com.koushikdutta.async.http.server.HttpServerRequestCallback;
import com.koushikdutta.async.util.Charsets;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
/**
* 异步网络框架工具类
* Created by xubaolun on 2018/7/26.
*/
public class AsynUtils {
private static final String TAG="AsynUtils";
private static final int PORT=15234;
public static String path=Environment.getExternalStorageDirectory().toString();
private static final String FILE_FLAG="/fileTrans";
private AsynUtils() {
}
static AsyncHttpServer asyncHttpServer=new AsyncHttpServer();
static AsyncServer asyncServer=new AsyncServer();
private static Handler handler=new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
return false;
}
});
public static void startServer(){
asyncServer.listen(null, PORT, new ListenCallback() {
@Override
public void onAccepted(final AsyncSocket socket) {
Log.i(TAG,"------------startServer--------onAccepted--------------------");
socket.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
Log.i(TAG,"------------startServer--------onAccepted--------------------"+bb.readString(Charsets.UTF_8));
final String order="响应测试信息";
Util.writeAll(socket, order.getBytes(), new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if (ex!=null) {
Log.d("Socket", "writeAll出错");
return;
}
Log.d("Socket", "发送了:"+order);
}
});
}
});
}
@Override
public void onListening(AsyncServerSocket socket) {
Log.i(TAG,"------------startServer--------onListening--------------------");
}
@Override
public void onCompleted(Exception ex) {
Log.i(TAG,"------------startServer--------onCompleted--------------------");
}
});
}
public <
AndroidAsync框架实现局域网通信
最新推荐文章于 2024-04-02 01:03:39 发布