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

0maN

macrumors newbie
Original poster
Oct 8, 2008
3
0
Hi guys,
I am new to Objective-C, I was wondering if you guys can give me some examples or tutorials on how to connect to server. I am traditional OO (Java, .NET) coder.
This is the way I connect to my server using Java. I am looking for something similar to this.

Code:
HttpConnection connection = (HttpConnection) javax.microedition.io.Connector.open(serviceURL, Connector.READ_WRITE, true);
    connection.setRequestProperty("User-Agent", userAgent);
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("Connection", "keep-alive");
    connection.setRequestMethod(HttpConnection.POST);
    DataOutputStream outputStream = connection.openDataOutputStream();
    outputStream.writeByte(requestType);
    outputStream.writeUTF(value);
    int responseCode = connection.getResponseCode();
    if (responseCode == HttpConnection.HTTP_OK || responseCode == HttpConnection.HTTP_CREATED) {
      DataInputStream iStream = connection.openDataInputStream();
      int returnCode = iStream.readByte();
    } else if (responseCode == HttpConnection.HTTP_GATEWAY_TIMEOUT) {
      throw new SessionException("Invalid session");
    } else if (responseCode == HttpConnection.HTTP_INTERNAL_ERROR) {
      throw new ApplicationException(-1, "Application error. Please contact support");
    }
    byte method = inputStream.readByte();
    String name = inputStream.readUTF();
    int count = inputStream.readInt();
    boolean status = inputStream.readBoolean();
    if (outputStream != null) {
      try {
        outputStream.close();
      } catch (IOException ioe) {}
    }

    if (inputStream != null) {
      try {
        inputStream.close();
      } catch (IOException ioe) {}
    }

    if (connection != null) {
      try {
        connection.close();
      } catch (IOException ioe) {}
    }
 

xnakx

macrumors newbie
Oct 5, 2008
17
0
are you trying to get a simple response from the server?
check out the NSURL Classes.
 

0maN

macrumors newbie
Original poster
Oct 8, 2008
3
0
are you trying to get a simple response from the server?
check out the NSURL Classes.

I tried NSURL Classes, but I don't know how to open DataOutputStream/DataInputStream.
 

jablon10

macrumors newbie
Oct 8, 2008
5
0
Los Angeles, CA
I tried NSURL Classes, but I don't know how to open DataOutputStream/DataInputStream.

Take a look at the ImageClient sample from Apple:

http://developer.apple.com/samplecode/ImageClient/index.html

Code:
    // Start a fresh data to hold the downloaded image
    data = CFDataCreateMutable(NULL, 0);
    
    readStream = CFReadStreamCreateForHTTPRequest(NULL, request);
    
    if (CFReadStreamOpen(readStream)) {
        BOOL done = NO;
        do {
            UInt8 buf[BUFSIZE];
            int bytesRead = CFReadStreamRead(readStream, buf, BUFSIZE);
            if (bytesRead > 0) {
                CFDataAppendBytes(data, buf, bytesRead);
            } else if (bytesRead == 0) {
                // All done
                [imageClient setImageData:(NSData *)data];
                done = YES;
            } else {
                // Error occurred
                [imageClient errorOccurredLoadingImage];
                done = YES;
            }
        } while (!done);
    } else {
        [imageClient errorOccurredLoadingImage];
    }

    // Clean up
    CFReadStreamClose(readStream);
    CFRelease(readStream);
    readStream = nil;

    CFRelease(data);
    data = NULL;
 

0maN

macrumors newbie
Original poster
Oct 8, 2008
3
0
Take a look at the ImageClient sample from Apple:

http://developer.apple.com/samplecode/ImageClient/index.html

Code:
    // Start a fresh data to hold the downloaded image
    data = CFDataCreateMutable(NULL, 0);
    
    readStream = CFReadStreamCreateForHTTPRequest(NULL, request);
    
    if (CFReadStreamOpen(readStream)) {
        BOOL done = NO;
        do {
            UInt8 buf[BUFSIZE];
            int bytesRead = CFReadStreamRead(readStream, buf, BUFSIZE);
            if (bytesRead > 0) {
                CFDataAppendBytes(data, buf, bytesRead);
            } else if (bytesRead == 0) {
                // All done
                [imageClient setImageData:(NSData *)data];
                done = YES;
            } else {
                // Error occurred
                [imageClient errorOccurredLoadingImage];
                done = YES;
            }
        } while (!done);
    } else {
        [imageClient errorOccurredLoadingImage];
    }

    // Clean up
    CFReadStreamClose(readStream);
    CFRelease(readStream);
    readStream = nil;

    CFRelease(data);
    data = NULL;

Thank you,
But is there any functionality for writing primitives to output stream?
 

jablon10

macrumors newbie
Oct 8, 2008
5
0
Los Angeles, CA
Thank you,
But is there any functionality for writing primitives to output stream?

I'm not sure what you want to do exactly, but normally sending data to a web site is done using an HTTP post.

All-Seeing Interactive has a nice library called ASIHTTPRequest to simplify some of this stuff. Look at the source and you should find what your looking for.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.