gbaor
gbaor

Reputation: 1427

Capturing traffic on TCP port programmatically

I'm writing a mac application what should record the traffic on the port 37265 of localhost. While recording the traffic the port 37265 is used by calabash to run automated test of iPhone apps in iPhone simulator. Calabash tests are generating http communication on port 37265 and I would like to process that communication after the test finish.

Can you give me hints how to code such a port traffic recorder?

It is very important that I need to capture the communication programmatically not by Wireshark or other software!

Upvotes: 1

Views: 547

Answers (2)

gbaor
gbaor

Reputation: 1427

So far it looks the best solution will be to call tcpdump from my code with a filter on the port 37265 and on TCP packets. The final solution looks like this:

_dumpTask = [[NSTask alloc] init];
[_dumpTask setLaunchPath: @"/usr/sbin/tcpdump"];
[_dumpTask setArguments: @[@"-i", @"lo0", @"-s", @"0", @"-B", @"52428", @"-w", @"/Users/user/Desktop/dump.pcap", @"-v", @"port", @"37265", @"and", @"tcp"]];

NSPipe *pipe;
pipe = [NSPipe pipe];
[_dumpTask setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[_dumpTask launch];

NSData *data;
data = [file readDataToEndOfFile];
NSString *response =  [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"%@", response);

Upvotes: 0

Pepijn
Pepijn

Reputation: 468

If I understand you correctly, you want to process the HTTP communication when testing with calabash.

Instead of coding this functionality, you could use a packet sniffer for this, e.g. WireShark.

After completing your testing with calabash, you can stop the network capture and you can filter on your specified port, e.g. with DisplayFilters.

Upvotes: 1

Related Questions