Getting network connection for a specified PID
Hi!
I´m currently writing an application that needs to find out, to which IP a specified process is connected to.
For example, when I have opened Quake 3 and connected to a server, then my app should tell me the IP of this server, but i dont have a clue how to do that.
I allready tried to get the IP like that:
This works for apps like adium or something like that. It prints the server, process name and process id.
But for Quake 3 or other games I only get something like that:
Why doesn´t it tell me the IP, only the port?
And how do i get the IP?
Thanks in advance
I´m currently writing an application that needs to find out, to which IP a specified process is connected to.
For example, when I have opened Quake 3 and connected to a server, then my app should tell me the IP of this server, but i dont have a clue how to do that.
I allready tried to get the IP like that:
Code:
lsof -iThis works for apps like adium or something like that. It prints the server, process name and process id.
But for Quake 3 or other games I only get something like that:
Code:
Quake3 3037 Flo 28u IPv4 0x02943040 0t0 UDP *:27960Why doesn´t it tell me the IP, only the port?
And how do i get the IP?
Thanks in advance
UDP isn't a connection-based protocol.
So there is no simple way of getting the Server-IP?
Only by watching the traffic to see where it's going.
Hi!
Watching the traffic with tcpdump works quite good, but I ant to start tcpdump from my app and get the output. Since tcpdump must be startet as superuser, i cant do it via NSTask. Here is my code:
That prints the output of tcpdump in the Console, but not as NSLog. Very rarely I get something via NSLog, but normaly its just printed in the Console. But why?!
I cant use the Output, if I dont get it via NSLog...
Has anyone an idea?
Thanks in advance
Watching the traffic with tcpdump works quite good, but I ant to start tcpdump from my app and get the output. Since tcpdump must be startet as superuser, i cant do it via NSTask. Here is my code:
Code:
- (void)awakeFromNib
{
OSStatus myStatus;
AuthorizationFlags myFlags=kAuthorizationFlagDefaults;
AuthorizationRef myAuthorizationRef;
myStatus=AuthorizationCreate(NULL,kAuthorizationEmptyEnvironment,myFlags,&myAuthorizationRef);
if(myStatus==errAuthorizationSuccess){
AuthorizationItem myItems={kAuthorizationRightExecute,0,NULL,0};
AuthorizationRights myRights={1,&myItems};
myFlags=kAuthorizationFlagDefaults| kAuthorizationFlagInteractionAllowed| kAuthorizationFlagPreAuthorize| kAuthorizationFlagExtendRights;
myStatus=AuthorizationCopyRights(myAuthorizationRef,&myRights,NULL,myFlags,NULL);
if(myStatus==errAuthorizationSuccess){
FILE*myCommunicationsPipe=NULL;
char*myArguments[]={ "-t",NULL};
NSString *tool=@"/usr/sbin/tcpdump";
myFlags=kAuthorizationFlagDefaults;
myStatus=AuthorizationExecuteWithPrivileges(myAuthorizationRef,[tool cString],myFlags,myArguments,&myCommunicationsPipe);
NSFileHandle *myfilehandle=[[NSFileHandle alloc] initWithFileDescriptor:fileno(myCommunicationsPipe)];
[NSThread detachNewThreadSelector:@selector(copyData:) toTarget:self withObject:myfilehandle];
fflush(myCommunicationsPipe);
}
}
return;
}
- (void)copyData:(NSFileHandle*)handle {
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
NSData *data;
while([data=[handle availableData] length]) {
NSString *string=[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"%@", string);
[string release];
}
[pool release];
}That prints the output of tcpdump in the Console, but not as NSLog. Very rarely I get something via NSLog, but normaly its just printed in the Console. But why?!
I cant use the Output, if I dont get it via NSLog...
Has anyone an idea?
Thanks in advance
Maybe it's printing to standard error?
OneSadCookie Wrote:Maybe it's printing to standard error?
Hm. But AuthorizationExecuteWithPrivileges doenst give a file handle to stderr back... How to get it then? And why prints NSLog something sometimes?
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Is it possible to implement http connection for iPhone Development? | joyjit_b | 2 | 5,186 |
Aug 8, 2008 09:13 AM Last Post: Taxxodium |
|
| java network api | San_Andreas | 1 | 2,865 |
May 1, 2008 08:05 AM Last Post: AndyKorth |
|
| Ways to Network. | BinarySpike | 28 | 11,706 |
May 10, 2005 07:44 PM Last Post: kodex |
|
| Network Code Design | Bachus | 5 | 4,212 |
Sep 26, 2004 06:54 PM Last Post: arcnon |
|
| Network Servers - whose connected to me? | Zenith | 3 | 4,280 |
Oct 2, 2003 04:29 PM Last Post: Steven |
|

