Nothing Special   »   [go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多客户端连接连接至同一个服务器的时候,如果有一个客户端断开了,服务端就会停止广播,即使客户端重新连接,服务端也收不到任何的连接信息,需要服务端重启才能解决此问题 #262

Open
FlashFeiFei opened this issue Apr 12, 2024 · 6 comments

Comments

@FlashFeiFei
Copy link

go:kcp-go作为服务端
java:kcp-base作为客户端

go作为游戏数据转发

@FlashFeiFei
Copy link
Author

服务端代码
`package main

import (
"github.com/xtaci/kcp-go/v5"
room2 "kcp/demo3/room"
"log"
"time"
)

func main() {

listener, err := kcp.ListenWithOptions("127.0.0.1:12345", nil, 10, 3)
listener.SetDeadline(time.Now().Add(1 * time.Second))
if err != nil {
	log.Fatal(err)
}

room := room2.NewGameRoom()
go room.Run()
for {
	s, err := listener.AcceptKCP()
	if err != nil {
		continue
	}
	room.JoinChan() <- s
}

}
`

@FlashFeiFei
Copy link
Author

客户端代码
`package com.laughingZhu.api;

import com.backblaze.erasure.FecAdapt;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import kcp.ChannelConfig;
import kcp.KcpClient;
import kcp.KcpListener;
import kcp.Ukcp;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

/**

  • 与go版本兼容的客户端

  • Created by JinMiao

  • 2019/11/29.
    */
    public class Kcp4GoExampleClient implements KcpListener {

    private final ByteBuf data = Unpooled.buffer(200);

    private final long startTime = System.currentTimeMillis();
    private volatile int count;

    public static void main(String[] args) {
    ChannelConfig channelConfig = new ChannelConfig();
    channelConfig.nodelay(true, 10, 2, true);
    channelConfig.setSndwnd(1024);
    channelConfig.setRcvwnd(1024);
    channelConfig.setMtu(1400);
    channelConfig.setFecAdapt(new FecAdapt(10, 3));
    channelConfig.setAckNoDelay(false);
    channelConfig.setTimeoutMillis(10000);

     //禁用参数
     channelConfig.setCrc32Check(false);
     channelConfig.setAckMaskSize(0);
    
    
     KcpClient kcpClient = new KcpClient();
     kcpClient.init(channelConfig);
    
    
     Kcp4GoExampleClient kcpGoExampleClient = new Kcp4GoExampleClient();
     Ukcp ukcp = kcpClient.connect(new InetSocketAddress("127.0.0.1", 12345), channelConfig, kcpGoExampleClient);
    

    }

    @OverRide
    public void onConnected(Ukcp ukcp) {

     //链接成功发送数据
     String msg = "hello!!!!!2222222222";
     byte[] bytes = msg.getBytes();
     ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer(bytes.length);
     byteBuf.writeBytes(bytes);
     ukcp.write(byteBuf);
    

    }

    @OverRide
    public void handleReceive(ByteBuf byteBuf, Ukcp ukcp) {
    //读取数据
    byte[] bytes = new byte[byteBuf.readableBytes()];
    byteBuf.getBytes(byteBuf.readerIndex(),bytes);
    System.out.println("收到消息: "+new String(bytes,StandardCharsets.UTF_8));

     //写入数据
    

// buf.writeShort(++count);
// buf.writeInt((int) (System.currentTimeMillis() - startTime));
++count;
String msg = "--发送数据java" + count;
ByteBuf buf = Unpooled.buffer(msg.length());
buf.writeCharSequence(msg, StandardCharsets.UTF_8);
ukcp.write(buf);
}

@Override
public void handleException(Throwable ex, Ukcp ukcp) {
    ex.printStackTrace();
}

@Override
public void handleClose(Ukcp ukcp) {
    System.out.println("链接关闭: " + ukcp);
    ukcp.close();

}

}`

@FlashFeiFei
Copy link
Author

先运行服务端,然后运行客户端,一切正常
微信图片_20240413005401

@FlashFeiFei
Copy link
Author

这时候,断开客户端重新运行客户端之后,数据会发送几秒,之后就不在发送数据了,卡主了;重新开启一个客户端,debug服务端发现s, err := listener.AcceptKCP()没有链接进来
微信图片_20240413005859

@luodaoyi
Copy link

求个c++的例子

@FlashFeiFei FlashFeiFei changed the title 跨语言通讯 无法监听多个链接同时转发数据 Apr 28, 2024
@FlashFeiFei FlashFeiFei changed the title 无法监听多个链接同时转发数据 客户端的所有连接连接至同一个服务器的时候,如果有一个客户端断开了,服务端就会停止广播,即使客户端重新连接,服务端也收不到任何的连接信息,需要服务端重启才能解决此问题 Apr 28, 2024
@FlashFeiFei FlashFeiFei changed the title 客户端的所有连接连接至同一个服务器的时候,如果有一个客户端断开了,服务端就会停止广播,即使客户端重新连接,服务端也收不到任何的连接信息,需要服务端重启才能解决此问题 多客户端连接连接至同一个服务器的时候,如果有一个客户端断开了,服务端就会停止广播,即使客户端重新连接,服务端也收不到任何的连接信息,需要服务端重启才能解决此问题 Apr 28, 2024
@FlashFeiFei
Copy link
Author

微信图片_20240428142926

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants