Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
meng.wu1 committed Oct 9, 2022
1 parent 057c6e8 commit 5025e71
Show file tree
Hide file tree
Showing 57 changed files with 1,370 additions and 237 deletions.
37 changes: 37 additions & 0 deletions app/src/main/java/com/mirkowu/xsocekt/ClientDemo1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.mirkowu.xsocekt;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class ClientDemo1 {
public static void main(String[] args) throws Exception {
System.out.println("=====客户端启动======");

// 1、创建发送端对象:发送端自带默认的端口号
DatagramSocket socket = new DatagramSocket();

Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请说:");
String msg = sc.nextLine();

if("exit".equals(msg)){
System.out.println("离线成功!");
socket.close();
break;
}

// 2、创建一个数据包对象封装数据
byte[] buffer = msg.getBytes();
DatagramPacket packet = new DatagramPacket( buffer, buffer.length, InetAddress.getLocalHost() , 8888);

// 3、发送数据出去
socket.send(packet);
}

socket.close();
}
}

82 changes: 56 additions & 26 deletions app/src/main/java/com/mirkowu/xsocekt/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.mirkowu.xsocket.client.connect.IConnectManager;
import com.mirkowu.xsocket.client.IPConfig;
import com.mirkowu.xsocket.core.IPConfig;
import com.mirkowu.xsocket.client.listener.ISocketListener;
import com.mirkowu.xsocket.core.IPulseSendData;
import com.mirkowu.xsocket.core.ISendData;
import com.mirkowu.xsocket.core.IUdpSendData;
import com.mirkowu.xsocket.core.SocketType;
import com.mirkowu.xsocket.core.XLog;
import com.mirkowu.xsocket.client.XSocket;
import com.mirkowu.xsocket.server.IServerSocketListener;
Expand All @@ -20,6 +23,7 @@
import com.mirkowu.xsocket.server.IClientPool;
import com.mirkowu.xsocket.server.IServerManager;
import com.mirkowu.xsocket.core.util.ByteUtils;
import com.mirkowu.xsocket.server.ServerOptions;
import com.mirkowu.xsocket.server.XSocketServer;

import org.json.JSONException;
Expand All @@ -38,7 +42,7 @@ protected void onCreate(Bundle savedInstanceState) {
IServerManager serverManager;

public void clickServer(View view) {
serverManager = XSocketServer.getServer(8888);
serverManager = XSocketServer.getServer(8888, ServerOptions.getDefault().setSocketType(SocketType.UDP));
serverManager.registerSocketListener(new IServerSocketListener() {
@Override
public void onServerListening(int serverPort) {
Expand All @@ -47,33 +51,36 @@ public void onServerListening(int serverPort) {

@Override
public void onClientConnected(IClient client, int serverPort, IClientPool clientPool) {

XLog.e("server , onClientConnected :" + client.getHostName() + ":" + serverPort);

client.addClientIOListener(MainActivity.this);
if (client != null) {
client.addClientIOListener(MainActivity.this);

client.send(new ISendData() {
@Override
public byte[] getData() {
return ("你已进入聊天室" + " 当前共有 " + clientPool.size() + " 人").getBytes();
if (client.getSocketType() == SocketType.TCP) {
client.send(new ISendData() {
@Override
public byte[] getData() {
return ("你已进入聊天室" + " 当前共有 " + clientPool.size() + " 人").getBytes();
}
});
}
});
}

}


@Override
public void onClientDisconnected(IClient client, int serverPort, IClientPool clientPool) {
XLog.e("server , onClientDisconnected :" + client.getHostName() + ":" + serverPort);
public void onClientDisconnected(IClient client, int serverPort, IClientPool clientPool, Exception e) {
XLog.e("server , onClientDisconnected :" + client.getHostName() + ":" + serverPort + (e != null ? e.toString() : "null"));
}

@Override
public void onServerWillBeShutdown(int serverPort, IShutdown shutdown, IClientPool clientPool, Throwable e) {
public void onServerWillBeShutdown(int serverPort, IShutdown shutdown, IClientPool clientPool, Exception e) {
XLog.e("server , onServerWillBeShutdown :" + serverPort + (e != null ? e.toString() : "null"));
}

@Override
public void onServerAlreadyShutdown(int serverPort, Throwable e) {
public void onServerAlreadyShutdown(int serverPort, Exception e) {
XLog.e("server , onServerAlreadyShutdown :" + serverPort + (e != null ? e.toString() : "null"));

}
Expand All @@ -84,12 +91,33 @@ public void onServerAlreadyShutdown(int serverPort, Throwable e) {
@Override
public void onReceiveFromClient(byte[] bytes, IClient client, IClientPool<String, IClient> clientPool) {
XLog.e("server onReceiveFromClient :" + ByteUtils.bytes2String(bytes));
client.send(new ISendData() {
@Override
public byte[] getData() {
return ByteUtils.concat("我已收到:".getBytes(),bytes);
}
});
if(bytes==null || bytes.length==0) return;
if(client.getSocketType()==SocketType.TCP){
client.send(new ISendData() {
@Override
public byte[] getData() {
return ByteUtils.concat( bytes,"-我已收到:".getBytes());
}
});
}else{
client.send(new IUdpSendData() {
@Override
public String getIp() {
return client.getHostIp();
}

@Override
public int getPort() {
return client.getHostPort();
}

@Override
public byte[] getData() {
return ByteUtils.concat( bytes,"-我已收到:".getBytes());
}
});
}


}

Expand All @@ -108,12 +136,12 @@ public void clickCloseServer(View view) {
IConnectManager manager;

public void clickConnect(View view) {
if(manager!=null && manager.isConnected()) {
if (manager != null && manager.isConnected()) {
manager.disconnect();
}
manager = new XSocket().config("192.168.1.1", 80);
// manager = new XSocket().connect("127.0.0.1", 8888);
// manager = XSocket.connect("192.168.2.104", 8888);
manager = XSocket.config("192.168.1.1", 80);
// manager = XSocket.config("127.0.0.1", 8888);
// manager = XSocket.config("192.168.2.104", 8888);
manager.registerSocketListener(new ISocketListener() {

@Override
Expand Down Expand Up @@ -157,10 +185,10 @@ public void onPulseSend(IPConfig config, IPulseSendData sendData) {

@Override
protected void onDestroy() {
if(manager!=null){
if (manager != null) {
manager.disconnect();
}
if(serverManager!=null){
if (serverManager != null) {
serverManager.shutdown();
}
super.onDestroy();
Expand All @@ -180,6 +208,7 @@ public static JSONObject getWifiList() {
}
return jsonObject;
}

/**
* 发送心跳包
*
Expand All @@ -194,6 +223,7 @@ public static JSONObject getHeart() {
}
return jsonObject;
}

public void clickSend(View view) {

if (manager != null) {
Expand All @@ -214,6 +244,6 @@ public void clickDisConnect(View view) {


public void clickTCP(View view) {
startActivity(new Intent(this,TcpActivity.class));
startActivity(new Intent(this, TcpActivity.class));
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/mirkowu/xsocekt/ServerDemo1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mirkowu.xsocekt;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ServerDemo1 {
public static void main(String[] args) throws Exception {
System.out.println("=====服务端启动======");
// 1、创建接收端对象:注册端口
DatagramSocket socket = new DatagramSocket(8888);

// 2、创建一个数据包对象接收数据
byte[] buffer = new byte[1024 * 64];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

while (true) {
// 3、等待接收数据。
socket.receive(packet);
// 4、取出数据即可
// 读取多少倒出多少
int len = packet.getLength();
String rs = new String(buffer,0, len);
System.out.println("收到了来自:" + packet.getAddress() +", 对方端口是" + packet.getPort() +"的消息:" + rs);
}
}
}

11 changes: 8 additions & 3 deletions app/src/main/java/com/mirkowu/xsocekt/TcpActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import android.widget.Toast;

import com.mirkowu.xsocekt.databinding.ActivityTcpBinding;
import com.mirkowu.xsocket.client.IPConfig;
import com.mirkowu.xsocket.core.IPConfig;
import com.mirkowu.xsocket.client.Options;
import com.mirkowu.xsocket.client.XSocket;
import com.mirkowu.xsocket.client.connect.IConnectManager;
import com.mirkowu.xsocket.client.listener.ISocketListener;
import com.mirkowu.xsocket.core.IPulseSendData;
import com.mirkowu.xsocket.core.ISendData;
import com.mirkowu.xsocket.core.SocketType;
import com.mirkowu.xsocket.core.XLog;
import com.mirkowu.xsocket.core.util.ByteUtils;

public class TcpActivity extends AppCompatActivity implements ISocketListener {
Expand Down Expand Up @@ -44,7 +47,7 @@ public void clickConnect(View view) {
Toast.makeText(this, "请输入port", Toast.LENGTH_SHORT).show();
return;
}
manager = XSocket.config(ip, Integer.parseInt(port));
manager = XSocket.config(ip, Integer.parseInt(port), Options.defaultOptions().setSocketType(SocketType.UDP));
manager.registerSocketListener(this);
manager.connect();
} else {
Expand Down Expand Up @@ -84,7 +87,6 @@ public byte[] getData() {
public void sendAppend(String content) {
binding.tvContentSend.append(content + "\n");
binding.svSend.fullScroll(ScrollView.FOCUS_DOWN);

}

public void receiveAppend(String content) {
Expand All @@ -100,12 +102,15 @@ public void onConnectSuccess(IPConfig config) {

@Override
public void onConnectFail(IPConfig config, Exception e) {
XLog.e("onConnectFail :" + (e == null ? "null" : e.toString()));
binding.btnConnect.setText("点击连接");
binding.btnSend.setEnabled(false);
}

@Override
public void onDisConnect(IPConfig config, Exception e) {
XLog.e("onDisConnect :" + (e == null ? "null" : e.toString()));

binding.btnConnect.setText("点击连接");
binding.btnSend.setEnabled(false);
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/activity_tcp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
android:inputType="text"
android:digits="0123456789."
android:layout_weight="1"
android:text="127.0.0.1"
android:hint="ip" />

<TextView
Expand All @@ -64,6 +65,7 @@
android:id="@+id/et_port"
android:layout_width="80dp"
android:inputType="number"
android:text="8888"
android:layout_height="wrap_content"
android:hint="port" />
</LinearLayout>
Expand Down
39 changes: 29 additions & 10 deletions client/src/main/java/com/mirkowu/xsocket/client/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,46 @@

import com.mirkowu.xsocket.client.connect.AbsReconnectionManager;
import com.mirkowu.xsocket.client.connect.DefaultReconnectManager;
import com.mirkowu.xsocket.core.IReceiver;
import com.mirkowu.xsocket.core.ISender;
import com.mirkowu.xsocket.core.SocketType;
import com.mirkowu.xsocket.core.io.AbsReceiver;
import com.mirkowu.xsocket.core.io.AbsSender;
import com.mirkowu.xsocket.core.io.IOThreadMode;

public class Options {

private IReceiver receiver;
private ISender sender;
private SocketType socketType=SocketType.TCP;
private AbsReceiver receiver;
private AbsSender sender;
private IOThreadMode ioThreadMode;
private AbsReconnectionManager reconnectionManager;
private long pulseFrequency;
private long pulseFeedLoseTimes;
private boolean allowMultiCast;

public boolean isAllowMultiCast() {
return allowMultiCast;
}

public Options setAllowMultiCast(boolean allowMultiCast) {
this.allowMultiCast = allowMultiCast;
return this;
}

public Options setSocketType(SocketType socketType) {
this.socketType = socketType;
return this;
}

public Options setPulseFeedLoseTimes(long pulseFeedLoseTimes) {
this.pulseFeedLoseTimes = pulseFeedLoseTimes;
return this;
}

public Options setReceiver(IReceiver receiver) {
public Options setReceiver(AbsReceiver receiver) {
this.receiver = receiver;
return this;
}

public Options setSender(ISender sender) {
public Options setSender(AbsSender sender) {
this.sender = sender;
return this;
}
Expand All @@ -46,15 +61,19 @@ public Options setPulseFrequency(int pulseFrequency) {
return this;
}

public SocketType getSocketType() {
return socketType;
}

public long getPulseFeedLoseTimes() {
return pulseFeedLoseTimes;
}

public IReceiver getReceiver() {
public AbsReceiver getReceiver() {
return receiver;
}

public ISender getSender() {
public AbsSender getSender() {
return sender;
}

Expand All @@ -73,7 +92,7 @@ public long getPulseFrequency() {
public static Options defaultOptions() {
Options options = new Options();
options.ioThreadMode = IOThreadMode.DUPLEX;
options.reconnectionManager = new DefaultReconnectManager();
// options.reconnectionManager = new DefaultReconnectManager();
return options;
}
}
Loading

0 comments on commit 5025e71

Please sign in to comment.