Hi,
I have three application. The code of all three applications are given below.
All three application are bind on single port and I used the udp socket.
My application1 and application 2 can send the message and application 3 only received the message. Applicaiton 1 can also received the message.
When i send the messages from application1 and 2 then they both send the message on single bind port(Means they share the port).
But the problem is only 1 application can received the message from share port. And other applications cannot received the messages while they bind on the same port.
For example.
If i run my application3 first and then application1 and application2 are run. Then only application 3 received the messages from share port(because they bind first) and application 1 cannot received the message.
similarly if i run applicaiton1 first then application2 and 3 are run. Then application1 only received the messages and application3 cannot received the message.
Actually i want that multiple application are bind on single port and they can send and received the data from this single port. My code work for sending the messages from multiple application on single port. But it not work for receiving the message on single port for multiple application. Please help me how multiple application received the message on single port that is share between them.
The code of all 3 applications are given below.
////////////////////////////////////////////////////////////
application 1 code is
////////////////////////////////////////////////////////////
Application 2 code
Application 3 code
//////////////////////////////////////////////////
I have three application. The code of all three applications are given below.
All three application are bind on single port and I used the udp socket.
My application1 and application 2 can send the message and application 3 only received the message. Applicaiton 1 can also received the message.
When i send the messages from application1 and 2 then they both send the message on single bind port(Means they share the port).
But the problem is only 1 application can received the message from share port. And other applications cannot received the messages while they bind on the same port.
For example.
If i run my application3 first and then application1 and application2 are run. Then only application 3 received the messages from share port(because they bind first) and application 1 cannot received the message.
similarly if i run applicaiton1 first then application2 and 3 are run. Then application1 only received the messages and application3 cannot received the message.
Actually i want that multiple application are bind on single port and they can send and received the data from this single port. My code work for sending the messages from multiple application on single port. But it not work for receiving the message on single port for multiple application. Please help me how multiple application received the message on single port that is share between them.
The code of all 3 applications are given below.
////////////////////////////////////////////////////////////
application 1 code is
Code:
#import <Cocoa/Cocoa.h>
@interface TestController : NSObject
{
IBOutlet NSButton *sendButton;
}
-(IBAction)btnSendMessage:(id)sender;
@end
#import "TestController.h"
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
NSSocketPort *app1Socket;
struct sockaddr_in app1socketAddr;
@implementation TestController
-(void)awakeFromNib
{
app1Socket = [NSSocketPort new];
app1Socket = socket(AF_INET, SOCK_DGRAM, 0);
int iOnOFF = 1;
if(setsockopt(app1Socket, SOL_SOCKET, SO_REUSEPORT,(char*)&iOnOFF, sizeof (int) ) )
printf("\n SOCKET OPTION FAILED \n");
else
printf("\n SET REUSE PORT TO TRUE \n");
//Address Structure
app1socketAddr.sin_family=AF_INET;
app1socketAddr.sin_port= htons(30000);
if (bind((int) app1Socket,(struct sockaddr *)(&app1socketAddr),sizeof(struct sockaddr)))
printf("\n...App1 Bind SOCKET FAILED...");
else
printf("\n...App1 Bind Socket SUCCESSFULLY AT PORT %d... ",30000);
printf("\n----------------------------------------------------------------------\n");
//Create Thread
[NSThread detachNewThreadSelector:@selector(MessageReciever:) toTarget:self withObject:nil];
}
-(IBAction)btnSendMessage:(id)sender
{
SInt16 uCSKeepAliveMsgCode = 1;
SInt16 iSwapCSKeepAliveMsgCode;
// Keep Alive Message to send
unsigned char* tempMsg = NewPtr(2);
//Stuff Protocol Message (Bytes 1 and 2 for Message Code)
iSwapCSKeepAliveMsgCode = htons(uCSKeepAliveMsgCode);
memcpy(tempMsg,&iSwapCSKeepAliveMsgCode,2);
sendto((int) app1Socket, tempMsg,2, 0,(struct sockaddr *)&app1socketAddr, sizeof(struct sockaddr));
printf("\n << Message Send >>\n");
}
-(void )MessageReciever:(id)anobject
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int addr_len = sizeof(struct sockaddr);
unsigned char *recievedMessage = NewPtr(3);
while(1)
{
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
//Message Recieved
int iNoOfBytes = recvfrom((int) app1Socket,recievedMessage,3,0,(struct sockaddr *)&app1socketAddr,&addr_len);
int iRxDataPortNo = ntohs( app1socketAddr.sin_port );
printf("\n<< Message RECEIVED >> \n");
printf("\n<< BYTES READ %d >>",iNoOfBytes);
printf("\n<< FROM PORT %d >>", iRxDataPortNo);
printf("\n<< FROM IP %s >>", inet_ntoa(app1socketAddr.sin_addr.s_addr) );
printf("\n----------------------------------------------------------------------\n");
UInt16 uimsgCode;
memcpy(&uimsgCode,recievedMessage,2);
printf("\n<< Message Code is %d >>", uimsgCode);
[loopPool release];
}
DisposePtr(recievedMessage);
[pool release];
}
@end
////////////////////////////////////////////////////////////
Application 2 code
Code:
//header file
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject
{
}
-(IBAction)btnSend:(id)sender;
@end
//Implementation file
#import "AppController.h"
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
NSSocketPort *app1Socket;
struct sockaddr_in app1socketAddr;
@implementation AppController
-(void)awakeFromNib
{
//Create and Bind System Port
app1Socket = [NSSocketPort new];
app1Socket = socket(AF_INET, SOCK_DGRAM, 0);
int iOnOFF = 1;
if(setsockopt(app1Socket, SOL_SOCKET, SO_REUSEPORT,(char* &iOnOFF, sizeof (int) ) )
printf("\n SOCKET OPTION FAILED \n");
else
printf("\n SET REUSE PORT TO TRUE \n");
//Address Structure
app1socketAddr.sin_family=AF_INET;
app1socketAddr.sin_port= htons(30000);
if (bind((int) app1Socket,(struct sockaddr *)(&app1socketAddr),sizeof(struct sockaddr)))
printf("\n...App1 Bind SOCKET FAILED...");
else
printf("\n...App1 Bind Socket SUCCESSFULLY AT PORT %d... ",30000);
printf("\n----------------------------------------------------------------------\n");
}
-(IBAction)btnSend:(id)sender
{
SInt16 uCSKeepAliveMsgCode = 2;
SInt16 iSwapCSKeepAliveMsgCode;
// Keep Alive Message to send
unsigned char* tempMsg = NewPtr(2);
//Stuff Protocol Message (Bytes 1 and 2 for Message Code)
iSwapCSKeepAliveMsgCode = htons(uCSKeepAliveMsgCode);
memcpy(tempMsg,&iSwapCSKeepAliveMsgCode,2);
sendto((int) app1Socket, tempMsg,2, 0,(struct sockaddr *)&app1socketAddr, sizeof(struct sockaddr));
printf("\n << Message Send >>\n");
}
@end
////////////////////////////////////////////////////////////////////
Application 3 code
Code:
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject
{
}
@end
#import "AppController.h"
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
NSSocketPort *app3Socket;
struct sockaddr_in app3socketAddr;
@implementation AppController
-(void)awakeFromNib
{
//Create and Bind System Port
app3Socket = [NSSocketPort new];
app3Socket = socket(AF_INET, SOCK_DGRAM, 0);
int iOnOFF = 1;
if(setsockopt(app3Socket, SOL_SOCKET, SO_REUSEPORT,(char*)&iOnOFF, sizeof (int) ) )
printf("\n SOCKET OPTION FAILED \n");
else
printf("\n SET REUSE PORT TO TRUE \n");
//Address Structure
app3socketAddr.sin_family=AF_INET;
app3socketAddr.sin_port= htons(30000);
//app3socketAddr.sin_addr.s_addr = inet_addr("192.168.3.1");
if (bind((int) app3Socket,(struct sockaddr *)(&app3socketAddr),sizeof(struct sockaddr)))
printf("\n...App3 Bind SOCKET FAILED...");
else
printf("\n...App3 Bind Socket SUCCESSFULLY AT PORT %d... ",30000);
printf("\n----------------------------------------------------------------------\n");
//Create Thread
[NSThread detachNewThreadSelector:@selector(MessageReciever:) toTarget:self withObject:nil];
}
-(void )MessageReciever:(id)anobject
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int addr_len = sizeof(struct sockaddr);
//Message to Decode (Memory Buffer)
unsigned char *recievedMessage = NewPtr(3);
//Thread Loop
while(1)
{
//Starting the Pool for Network Schedular Thread Loop
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
//Message Recieved
int iNoOfBytes = recvfrom((int) app3Socket,recievedMessage,3,0,(struct sockaddr *)&app3socketAddr,&addr_len);
//Get Port Number from DataGram
int iRxDataPortNo = ntohs( app3socketAddr.sin_port );
printf("\n<< Message RECEIVED >> \n");
printf("\n<< BYTES READ %d >>",iNoOfBytes);
printf("\n<< FROM PORT %d >>", iRxDataPortNo);
printf("\n<< FROM IP %s >>", inet_ntoa(app3socketAddr.sin_addr.s_addr) );
printf("\n----------------------------------------------------------------------\n");
UInt16 uimsgCode;
//Message Code
memcpy(&uimsgCode,recievedMessage,2);
printf("\n<< Message Code is %d >>", uimsgCode);
[loopPool release];
}
DisposePtr(recievedMessage);
[pool release];
}
@end