On Feb 7, 10:25 am, Heath Raftery wrote:
> wrote:
> > I have used SetSockOpt - SO_REUSEADDR before bind function.
>
> Can you post this area of code? There are couple of minor
> variations of theme that might be significant.
>
> > In particular case i am getting SetSockOpt functions completes with No
> > error but still bind fucntion fails with error that port is in use.
>
> That may well be the case - as I think you are aware, REUSEADDR only
> bypasses some checks when binding to a port. If the port is indeed
> bound by another process, the second bind will still fail.
>
> > Case is i have three users of my MAC machine. User A logged
> > in,Application is successfull to bind the port and client also gets
> > connected successfully. Now i m closing my Client server application
> > and switching to another user and try to run my server application.
> > Here my socket cannot able to bind on port and fails with an error
> > that it is in Use. Suprisingly if i try with after 1 or 2 minutes it
> > gets binded successfully.
>
> > It is here bit difficult to judge why this is happing only when
> > switching the user. As if I run my application number of time in same
> > user it will never give me such error.
>
> Wow, that's interesting. I've never heard of multiple users causing
> problems, but then again I'd say that would be a rare situation.
> Generally anything that binds to a port and listens as a server
> will be run by one superuser and only once. Not that that should
> prevent you from doing what you want.
>
> The 1-2 minutes thing certainly does sound like REUSEADDR should
> fix it up though.
>
> > Either i force terminate the
> > process or close it smoothly.
>
> Ah, but does the process close() the socket it bound to?
>
> > I don't think this is a MACOSX problem or Objective - C problem. I
> > still looking into my code and find the solution of this.
>
> Post some relevant code if you want a few more eyes to help you
> out.
>
> --
> *--------------------------------------------------------*
> | ^Nothing is foolproof to a sufficiently talented fool^ |
> | Heath Raftery, HRSoftWorks _\|/_ |
> *______________________________________m_('.')_m_________*
Hi,
Thank you for your reply.
I am posting my code here.
int socketId;
int clientSocket;
both are class vairables
Two methods
- startOnPort
-stopListening
- WaitForConnection (Thread Function)
Code here
-(BOOL)StartOnPort:(uint32_t)port
{
struct sockaddr_in listeningAddress;
//Create a socket to listen for request
if((socketId=socket(AF_INET,SOCK_STREAM,0))<0)
{
//Unable to create the socket
return NO;
}
//Fill stucture
bzero(&listeningAddress,sizeof(listeningAddress));
listeningAddress.sin_family=AF_INET;
listeningAddress.sin_port=htons(port);
listeningAddress.sin_addr.s_addr=htonl(INADDR_ANY);
//Set option for reuse
int on=1;
if(setsockopt(socketId,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))<0)
{
NSLog(@"Unable to set reuse option.");
}
//Now bind the address
if(bind(socketId,(struct sockaddr
*)&listeningAddress,sizeof(listeningAddress))<0)
{
NSLog(@"Unable to bind socket errno=%d",errno);
close(socketId);
return NO;
}
[NSThread detachNewThreadSelector:@selector(waitForConnection:)
toTarget:self withObject:nil];
isListening=YES;
return YES;
}
-(BOOL)stopListening
{
if(!isListening)
{
return NO;
}
close(socketId);
close(clientSocket);
isListening=NO;
return YES;
}
-(void)waitForConnection:(id)anObject
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
unsigned char buffer[BUFFER_SIZE];
struct sockaddr_in clientAddr;
unsigned int addSize=sizeof(clientAddr);
//Listen for connection
listen(socketId,2);
NSLog(@"Waiting for connection..");
if((clientSocket=accept(socketId,(struct sockaddr
*)&clientAddr,&addSize))<0)
{
NSLog(@"Error in accepting connection");
return;
}
NSLog(@"Connection accepted.");
while(!isFinished)
{
int result=read(clientSocket,buffer,BUFFER_SIZE);
if(result<=0)
{
NSLog(@"Error in reading the data. %d",errno);
break;
}
//Pass data for processing
received(receiver,@selector(receive:
length:),buffer,result);
}
close(clientSocket);
[pool release];
NSLog(@"Thread exiting.");
}
Let me know if anything written in the code, cause this issue.
Thanks a lot.
Archita
>> Stay informed about: Bind fails with an error port is in use.