Trying to connect to my server!

Discussion in 'iOS Programming' started by 0maN, Oct 8, 2008.

  1. 0maN macrumors newbie

    Joined:
    Oct 8, 2008
    #1
    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) {}
        }
    
     
  2. xnakx macrumors newbie

    Joined:
    Oct 5, 2008
    #2
    are you trying to get a simple response from the server?
    check out the NSURL Classes.
     
  3. 0maN thread starter macrumors newbie

    Joined:
    Oct 8, 2008
    #3
    I tried NSURL Classes, but I don't know how to open DataOutputStream/DataInputStream.
     
  4. jablon10 macrumors newbie

    Joined:
    Oct 8, 2008
    Location:
    Los Angeles, CA
    #4
    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;
     
  5. 0maN thread starter macrumors newbie

    Joined:
    Oct 8, 2008
    #5
    Thank you,
    But is there any functionality for writing primitives to output stream?
     
  6. jablon10 macrumors newbie

    Joined:
    Oct 8, 2008
    Location:
    Los Angeles, CA
    #6
    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.
     

Share This Page