title: socket programming in objective c
body: <p>I have an app that uses socket</p>
  
  <p>In my application , i am trying to establish socket connection and after connecting socket i need to send soap request to server.i'm having  problem with soap request sending to server.   i have attached the code.  Please help me in this </p>
  
  <pre><code> BOOL status;
  
      CFReadStreamRef readStream;
  
      CFWriteStreamRef writeStream;
  
      urlString = @"hoegamestg.hogaming.info";
  
      // urlString = @"247liveagent.hointeractive.com";
  
  
  
      if (![urlString isEqualToString:@""])
  
      {
          NSURL *website = [NSURL URLWithString:urlString];
  
          if (!website)
          {
  
              NSLog(@"%@ is not a valid URL", urlString);
              status = NO;
  
  
          }
  
          else
  
          {
              NSLog(@"URL IS VALID%@",website );
  
  
  
              CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef) [website host], 5654, &amp;readStream, &amp;writeStream);
  
  
  
              //  CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef) urlString, 5654, &amp;readStream, &amp;writeStream);
  
  
              //USE TOLL-FREE BRIDGING FOR CONVERTING CORE-FOUNDATION STREAMS TO NSSTREAMS.
  
  
              self.inputStream = (__bridge_transfer NSInputStream *)readStream;
              self.outputStream = (__bridge_transfer NSOutputStream *)writeStream;
  
              //SET DELEGATES TO STREAMS.
  
              [self.inputStream setDelegate:self];
              [self.outputStream setDelegate:self];
  
              //AVOID BLOCKING OPERATIONS BY SCHEDULING THEM ON TO RUN LOOPS.
  
              [self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
              [self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  
              //FINALLY OPEN THE STREAMS.
  
              [self.inputStream open];
              [self.outputStream open];
  
  
          }
  
      }......
  </code></pre>
  
  <p>Delegate method and soap request </p>
  
  <pre><code>- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{
  
      // Start Logging events.
     // NSString *str = [NSString stringWithFormat:@"%d",eventCode];
     // NSMutableString *subscribeTableString;
  
      NSData *data;
  
      NSString *soapMessage = [[NSString alloc]init];
  
  
  
      switch (eventCode) {
  
          case NSStreamEventNone:
              NSLog(@"NSStreamEventNone");
              break;
          case NSStreamEventOpenCompleted:
              NSLog(@"NSStreamEventOpenCompleted");
              break;
          case NSStreamEventHasBytesAvailable:
  
              NSLog(@"NSStreamEventHasBytesAvailable");
  
              [self readDataFromStream];
  
              break;
  
          case NSStreamEventHasSpaceAvailable:
  
  
              NSLog(@"NSStreamEventHasSpaceAvailable");
  
  
              soapMessage=[soapMessage stringByAppendingString:[NSString stringWithFormat:@"&lt;subscribe channel=\"table-bc7ire5oi4uhetfd\" &gt;&lt;player id=\"%@\" /&gt;",socketValue ]];
  
             // soapMessage=[soapMessage stringByAppendingString:[NSString stringWithFormat:@"&lt;subscribe channel=\"table-bc7ire5oi4uhetfd\" &gt;&lt;player id= \"c2da1a80c52542dd\" /&gt;" ]];
  
  
  
  
              soapMessage =[soapMessage stringByAppendingString:[NSString stringWithFormat:@"&lt;sessionid id=\"%@\"",socketname]];
  
              soapMessage =[soapMessage stringByAppendingString:[NSString stringWithFormat:@"&gt;&lt;/sessionid&gt;&lt;/subscribe&gt;"]];
  
  
              NSLog(@"THE STRING IS : %@", soapMessage);
  
  
          data = [[NSData alloc] initWithData:[soapMessage dataUsingEncoding:NSASCIIStringEncoding]];
  
  
              [self.outputStream write:[data bytes] maxLength:[data length]];
  
  
              NSLog(@"THE STRING IS : %@", self.outputStream);
  
  
  
              [self setMWriteData:[NSData dataWithBytes:(__bridge const void*)soapMessage length:
  
                                   [soapMessage length]]];
  
  
              [self writeDataToStream];
  
  
           //   &lt;subscribe channel="table-l8i2hq4jo2hjj9ca"&gt;&lt;player id="b82fe3c52020494b" /&gt;&lt;sessionid id="246421321cc873d080b550bcc555de0e9d9d29d8cba6f243ec56d38c5785"&gt;&lt;/sessionid&gt;&lt;/subscribe&gt;
  
              break;
  
  
          case NSStreamEventErrorOccurred:
  
  
              NSLog(@"NSStreamEventErrorOccurred");
              NSLog(@"THE ERROR IS : %@", [aStream streamError]);
  
              break;
  
  
          case NSStreamEventEndEncountered:
  
              break;
  
          default:
  
              break;
  
      }
  
  }
  </code></pre>