Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Filini

macrumors newbie
Original poster
Jan 17, 2009
25
0
Hi people, i have simple network scanner (C code) and i wanna convert this piece of code to Objective-C program. How convert c-function in class method obj-c and add their to header and implementation file...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>

#define DEF_STR_PORT 1
#define DEF_STP_PORT 1024
#define OXO 1

struct sockaddr_in addr;
struct hostent *rh;
struct servent *rp;

int sock,i;
int str_ptr, stp_ptr;
int Usage(char *ARG);
int CONNECTION(int port);
                        
int main(int argc, char *argv[])
{

	if (argc != 4)
		Usage(argv[0]);
	
		str_ptr = atoi(argv[2]);
		stp_ptr = atoi(argv[3]);
	if (strcmp(argv[2],"-")==0 && strcmp(argv[3],"-")==0){
		str_ptr = DEF_STR_PORT;
		stp_ptr = DEF_STP_PORT;
	}

	if ( str_ptr > stp_ptr){
		fprintf(stderr,"Detect error\n");
		Usage(argv[0]);
		exit(OXO);
	}

	if ((rh=gethostbyname(argv[1])) == NULL){		
		fprintf(stderr,"Can't Resolve Host\n");
		Usage(argv[0]);
		exit(OXO);
	}
	
	for (i=str_ptr; i <= stp_ptr; i++)
	{
		if (CONNECTION(i)==0)
		{
			rp=getservbyport(htons(i),"tcp");
			printf("Port %d open !!! <%s> Service.-\n",i,(rp == NULL)?"Uknown":rp->s_name);
		}
	close(sock);
	}

return 0;
}

int CONNECTION(int port)
{

	if ((sock=socket(AF_INET,SOCK_STREAM,0)) == -1){
		perror("Socket error");
		exit(OXO);
	}
	        
        addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr = *((struct in_addr *)rh->h_addr);

	if ((connect(sock,(struct sockaddr *) &addr, sizeof(addr))) == 0)
	        return 0;
	else
		return 1;
}

int Usage(char *ARG)
{
	fprintf(stderr,"Usage: %s <Remote-Host> <Start-Port> <Stop-Port> \n",ARG);
	exit(OXO);
}
 
Hi people, i have simple network scanner (C code) and i wanna convert this piece of code to Objective-C program. How convert c-function in class method obj-c and add their to header and implementation file...

C is a subset of Objective-C, so you shouldn't *have* to change any of it.

However, there's plenty in this program that is generally considered bad practice--even in C programs. Your variables are global--even the counting variable for your "for" loop.

You need to reshuffle your priorities. Before you try to convert this to Objective-C, you need to actually *learn* Objective-C... or even C for that matter.

http://cocoadevcentral.com/articles/000081.php
http://cocoadevcentral.com/d/learn_objectivec/


I feel that I should also point out that a port scan is considered a security intrusion, so some computers' firewalls will be set not to reply to this kind of thing. Also, if all you really want to do is a port scan, Apple's "Network Utility" will do it for you.
 
Thx for advice about learn C and obj-c:) but in this example, I want to understand how remake C in obj-c "surfactant", without going into deep details programmer...for example
Code:
#include<stdio.h>

//external function
int function1(int x, int y)
{
int sum;
sum=x+y;
return sum;
}

//main code
int main(int argc, char* argv[])
{
int a=5, b=10, aaa;
aaa=function1(a,b);
printf("Sum %d", aaa);
return 0;
}
In obj-c code:

Header:
Code:
+(int)sumInt:(int)a and:(int)b;
The implementation would be:
Code:
+(int)sumInt:(int)a and:(int)b {
  return a+b;
}
Let's say this is in a class called mathUtils.
Code:
#import "mathUtils.h"

int main(int argc, char *argv[]) {
  int x = 5;
  int y = 6;
  int w = 0;

  w = [mathUtils sumInt:x and:y];
  NSLog(@"The value of w is: %d",w);
}
 
okay, okay. Here's what you have, roughly:

Code:
int global1, global2, should_have_been_local;
int function(int input);

int main(void)
{
    global1 = 1;
    global2 = 2;
    should_have_been_local = function( 42 );
    return 0;
}

int function(int input)
{
    return input+global1+global2;
}

It should look more like this:

Code:
@class MyObject;
{
    int global1;
    int global2;
}

-(int)function: int input;

@end


int main(void)
{
    MyObject obj = [[MyObject alloc] init];
    obj->global1 = 1;
    obj->global2 = 2;

    int local = [obj function: 42];

    [obj release];

    return 0;
}

Granted, that's very rough almost pseudo-code. It's been a long time since I've written Objective-C, so I probably screwed up a lot, and I'm drawing a complete blank on the syntax of the class implementation.
 
Ок, I'll try to "convert" C code to obj-c and you show me where I'm wrong, see you tomorrow
 
okay, okay. Here's what you have, roughly:

Code:
int global1, global2, should_have_been_local;
int function(int input);

int main(void)
{
    global1 = 1;
    global2 = 2;
    should_have_been_local = function( 42 );
    return 0;
}

int function(int input)
{
    return input+global1+global2;
}

It should look more like this:

Code:
@class MyObject;
{
    int global1;
    int global2;
}

-(int)function: int input;

@end


int main(void)
{
    MyObject obj = [[MyObject alloc] init];
    obj->global1 = 1;
    obj->global2 = 2;

    int local = [obj function: 42];

    [obj release];

    return 0;
}

Granted, that's very rough almost pseudo-code. It's been a long time since I've written Objective-C, so I probably screwed up a lot, and I'm drawing a complete blank on the syntax of the class implementation.
Yeah, that is a little off. You would have to define accessor/setter methods for MyObject for the globals, like

Code:
- (void)setGlobal1:(int)aValue {
global1 = aValue; 
}

and then use something like [obj setGlobal1:1];

Instance variables are generally hidden from the sender (caller), they are not directly accessed like structure elements. You define and use getter methods similarly

- (int)global1 { return global1; }

This of course can be shortened using the @property and @synthesize shortcut macros which automatically create the getter and setter methods for you.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.