Anand
Anand

Reputation: 1973

socket closed by remote peer gcdasyncsocket

Creating an chat application using xmppframework. When ever I try to connect to the server it connects & disconnected immediately & throws the following error.

error domain=gcdasyncsocketerrordomain code=7 socket closed by remote peer

It happens the same on Adium (chat app) software. Wondering any thing set wrong while setupping the Openfire on AWS Server.

https://community.igniterealtime.org/thread/50643

Please let me know If I need to follow any steps or such.

Thanks for reading.

Upvotes: 5

Views: 10555

Answers (3)

Naresh
Naresh

Reputation: 17892

I got the same error when i try to use 1885 port numer.

I have added below code in my existing code:

mqttReference?.dispatchQueue = DispatchQueue.global(qos: .userInitiated)
        //mqttReference?.logLevel = .debug
        mqttReference?.enableSSL = true
        mqttReference?.sslSettings = [
            GCDAsyncSocketManuallyEvaluateTrust: NSNumber(booleanLiteral: true),
            GCDAsyncSocketUseCFStreamForTLS: NSNumber(booleanLiteral: false),
            kCFStreamSSLPeerName as String: "" as NSString,
            GCDAsyncSocketSSLProtocolVersionMin: NSNumber(integerLiteral: Int(SSLProtocol.tlsProtocol1.rawValue)),
            GCDAsyncSocketSSLProtocolVersionMax: NSNumber(integerLiteral: Int(SSLProtocol.tlsProtocol12.rawValue)),
        ]

My complete code now:

//  let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
    mqttReference = CocoaMQTT(clientID: "", host: mqttProfile.baseURL, port: UInt16(mqttProfile.port))
    mqttReference?.username = mqttProfile.userName
    mqttReference?.password = mqttProfile.password
    mqttReference?.allowUntrustCACertificate = true
    
    
    mqttReference?.dispatchQueue = DispatchQueue.global(qos: .userInitiated)
    //mqttReference?.logLevel = .debug
    mqttReference?.enableSSL = true
    mqttReference?.sslSettings = [
        GCDAsyncSocketManuallyEvaluateTrust: NSNumber(booleanLiteral: true),
        GCDAsyncSocketUseCFStreamForTLS: NSNumber(booleanLiteral: false),
        kCFStreamSSLPeerName as String: "" as NSString,
        GCDAsyncSocketSSLProtocolVersionMin: NSNumber(integerLiteral: Int(SSLProtocol.tlsProtocol1.rawValue)),
        GCDAsyncSocketSSLProtocolVersionMax: NSNumber(integerLiteral: Int(SSLProtocol.tlsProtocol12.rawValue)),
    ]
    
    
    mqttReference?.autoReconnect = true
    mqttReference?.backgroundOnSocket = mqttProfile.allowBackgroundMQTT
    mqttReference?.delegate = self
    do {
        if((mqttReference?.connect())!){
           ...........

Now it's working fine.

Upvotes: 1

Teson Draw
Teson Draw

Reputation: 21

You must store the new socket when the delegate method socket:didAcceptNewSocket been invoked

@property (nonatomic, strong) NSMutableArray *socketsArray;

- (void)viewDidLoad {
    _socketsArray = [[NSMutableArray alloc] init];
}

- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket {
    [_socketsArray addObject:newSocket];
}

Upvotes: 2

Badal Shah
Badal Shah

Reputation: 7602

Please check this github link,

Socket closed by remote peer" - GCDAsyncSocket Error Code 7

It is due to server might be closing the connection because of the idle period. you need to send continues ping to server.

As said in answer ,

Two reasons when the server closes the connection:

  1. You are not sending regular pings if the client idle.
  2. You are logging in from some different client with the same credentials, and in the server settings have the setting: Always kick - If there is a resource conflict, immediately kick the other resource. in Server>server settings>resource policy.

Upvotes: 0

Related Questions