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

Connection failure on some cases #213

Open
xhzzhang opened this issue Nov 4, 2022 · 0 comments
Open

Connection failure on some cases #213

xhzzhang opened this issue Nov 4, 2022 · 0 comments

Comments

@xhzzhang
Copy link

xhzzhang commented Nov 4, 2022

Hi, thanks for your great project, but I tested and found that fail to connect mNetAssist.deb or TcpUdp.exe, but success to connect server program was implement enet.

client code

`#include
#define ENET_DEBUG
#include <enet/enet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

void run()
{
ENetHost * client;
client = enet_host_create (NULL /* create a client host /,
1 /
only allow 1 outgoing connection /,
2 /
allow up 2 channels to be used, 0 and 1 /,
0 /
assume any amount of incoming bandwidth /,
0 /
assume any amount of outgoing bandwidth */);
if (client == NULL)
{
fprintf (stderr,
"An error occurred while trying to create an ENet client host.\n");
exit (EXIT_FAILURE);
}

ENetAddress address;
ENetEvent event;
ENetPeer *peer;
/* Connect to some.server.net:1234. */
enet_address_set_host (& address, "172.16.33.189");
address.port = 12345;
/* Initiate the connection, allocating the two channels 0 and 1. */
peer = enet_host_connect (client, & address, 2, 0);    
if (peer == NULL)
{
   fprintf (stderr, 
			"No available peers for initiating an ENet connection.\n");
   return ;
}
ENetPacket * packet = enet_packet_create ("packet", 
										  strlen ("packet") + 1, 
										  ENET_PACKET_FLAG_RELIABLE);
/* Extend the packet so and append the string "foo", so it now */
/* contains "packetfoo\0"                                      */
enet_packet_resize (packet, strlen ("packetfoo") + 1);
strcpy ((char*)(& packet -> data [strlen ("packet")]), "foo");

/* Wait up to 5 seconds for the connection attempt to succeed. */
if (enet_host_service (client, & event, 5000) > 0 &&
	event.type == ENET_EVENT_TYPE_CONNECT)
{
	printf ("Connection to some.server.net:1234 succeeded.");
	/* Create a reliable packet of size 7 containing "packet\0" */
	/* Send the packet to the peer over channel id 0. */
	/* One could also broadcast the packet by         */
	/* enet_host_broadcast (host, 0, packet);         */
	enet_peer_send (peer, 0, packet);
	/* One could just use enet_host_service() instead. */
	enet_host_flush (client);
	event.peer -> data = new char[10];
	strcpy ((char*)(event.peer -> data), "foo2");
}
else
{
	enet_peer_reset (peer);
	printf ("Connection to some.server.net:1234 failed.");
	return ;
}
char msg[200] = {0};
/* Wait up to 5 seconds for the connection attempt to succeed. */
while (enet_host_service (client, & event, 5000) >= 0)
{
    switch (event.type)
    {
    case ENET_EVENT_TYPE_RECEIVE:
        printf ("From %s,%u[len=%u]: %s\n",
                event.peer -> data,
                event.channelID,
                event.packet -> dataLength,
                event.packet -> data);
        /* Clean up the packet now that we're done using it. */
        //enet_packet_destroy (event.packet);
   
        break;
       
    case ENET_EVENT_TYPE_DISCONNECT:
        printf ("%s disconnected.\n", event.peer -> data);
        /* Reset the peer's client information. */
        event.peer -> data = NULL;
    }
	/************* 
	// enet应该放在单独的线程里跑,scanf阻塞后会,影响enet_host_service发送心跳
	// 心跳1s一次,如果5s内没有心跳,服务端会提示连接断开
	scanf("%s",msg);
	enet_packet_resize (packet, strlen (msg) + 1);
	strcpy ((char*)( packet -> data ), msg);
	enet_peer_send (peer, 0, packet);
	enet_host_flush (client);*/
}

}

int main()
{
if (enet_initialize () != 0)
{
fprintf (stderr, "An error occurred while initializing ENet.\n");
return EXIT_FAILURE;
}
run();

return 0;

}`

Hope someone can give me some advice, thanks.

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

1 participant