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

Question: [acceptOnPort: error:] is not accepting #330

Closed
oxrider opened this issue Aug 3, 2015 · 6 comments
Closed

Question: [acceptOnPort: error:] is not accepting #330

oxrider opened this issue Aug 3, 2015 · 6 comments

Comments

@oxrider
Copy link

oxrider commented Aug 3, 2015

I have created two sockets with the following code in viewDidLoad:

socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
listenSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

and hoping that socket would upload data to my server and listenSocket would listen to connection from another process from the server. So I made listenSocket start listening with this block of code:

NSError *err = nil;
if (![listenSocket acceptOnPort:19920 error:&err]) {
    NSLog(@"Listening port %hu failed to open: %@", listenSocket.localPort, err);
}

I then added NSLog in socket: didAcceptNewSocket: function to see if the socket had accepted, it never accepts. I have tried using the connect() in C on server, telnet and using the IP address and port number directly in a browser and nothing triggered the NSLog in socket: didAcceptNewSocket:.

Can I get some help on this? Thank you very much!

Node: I specified the accepting port number to a constant (19920) and got the IP address from the process on server that socket connects to.

@oxrider oxrider changed the title Question: [acceptOnPort: error:] is not working Question: [acceptOnPort: error:] is not accepting Aug 3, 2015
@michbil
Copy link

michbil commented Sep 19, 2015

Having same problem on iOS and tvOS using both AsyncSocket and GCDAsyncSocket, connections were'nt accepted at all. Did some comparison with Apple guide

Found, if working with AsyncSocket, and change

- (CFSocketRef)newAcceptSocketForAddress:(NSData *)addr error:(NSError **)errPtr
{
    struct sockaddr *pSockAddr = (struct sockaddr *)[addr bytes];
    int addressFamily = pSockAddr->sa_family;



    CFSocketRef theSocket = CFSocketCreate(kCFAllocatorDefault,
                                           addressFamily,
                                           SOCK_STREAM,
                                           IPPROTO_TCP,
                                           kCFSocketAcceptCallBack,                // Callback flags
                                           (CFSocketCallBack)&MyCFSocketCallback,  // Callback method
                                           &theContext);

to

- (CFSocketRef)newAcceptSocketForAddress:(NSData *)addr error:(NSError **)errPtr
{
    struct sockaddr *pSockAddr = (struct sockaddr *)[addr bytes];
    int addressFamily = pSockAddr->sa_family;



    CFSocketRef theSocket = CFSocketCreate(kCFAllocatorDefault,
                                           addressFamily,
                                           SOCK_STREAM,
                                           IPPROTO_TCP,
                                           kCFSocketAcceptCallBack,                // Callback flags
                                           (CFSocketCallBack)&MyCFSocketCallback,  // Callback method
                                           NULL);

socket at least starting to accept incoming connections, but connect handler not called
Doing further investigation.

@wl33
Copy link

wl33 commented Jun 6, 2016

oxrider:
had you solved the problem? I came across the same problem and could not find the solution so far. If you see this message, could you tell me?

@oxrider
Copy link
Author

oxrider commented Jun 7, 2016

@wl33
I can't remember exactly because i haven't touched this project for a long time, but here's the rough idea. When you work on phones that operate either on WiFi or cellular network, you don't usually have a public IP to be connected to. The IP address I saw on my server when my phone connects to it was the IP address of the service provider (more or less, I can't remember the exact entity that owns the IP addresses, but things like AT&T base station or the main router in your building if you are using a WiFi). What you should do is you should always start your connection from client (phone) to server. If you really need to initiate a connection from server, try using a push notification that tells the client to connect to server. But I highly recommend you to rethink about your system design if that is the case.

@wl33
Copy link

wl33 commented Jun 7, 2016

Thank you very much.


From: Tengyu Liu notifications@github.com
Sent: 07 June 2016 14:09:18
To: robbiehanson/CocoaAsyncSocket
Cc: Weishuo Lin; Mention
Subject: {Disarmed} Re: [robbiehanson/CocoaAsyncSocket] Question: [acceptOnPort: error:] is not accepting (#330)

@wl33https://github.com/wl33
I can't remember exactly because i haven't touched this project for a long time, but here's the rough idea. When you work on phones that operate either on WiFi or cellular network, you don't usually have a public IP to be connected to. The IP address I saw on my server when my phone connects to it was the IP address of the service provider (more or less, I can't remember the exact entity that owns the IP addresses, but things like AT&T base station or the main router in your building if you are using a WiFi). What you should do is you should always start your connection from client (phone) to server. If you really need to initiate a connection from server, try using a push notification that tells the client to connect to server. But I highly recommend you to rethink about your system design if that is the case.

You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com//issues/330#issuecomment-224134319, or mute the threadhttps://github.com/notifications/unsubscribe/AQEuy1e11vMWhMRgQbO1-IrSmGlUUsiEks5qJMS-gaJpZM4FkUgJ.

@MadisonRong
Copy link

I got the same problem. IOS app on iPhone is server, and also is a AccessPoint(AP).And Android app is client.When client trying to connect to server, it got an error below:

java.net.ConnectException: failed to connect to /172.20.10.1(port 8090)  connect failed: ECONNREFUSED (Connection refused)

And, of course, it doesn't run acceptNewSocket callback. It seems like server didn't accept a connection.
Besides, I have been tried two working test below:

  1. write an Android app as server, communicate with my Android client app
  2. write an iOS app using GCDAsyncSocket as client, communicate with my iOS server app

Is that could be a bug hiding very deep?And, any help will be appreciated.

@github-actions
Copy link

This issue has been marked as stale, it will be closed automatically if there is no further activity.

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

No branches or pull requests

4 participants