Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6904453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:03:47+00:00 2026-05-27T08:03:47+00:00

I’m trying to create a simple chat application for iOS. It works for receiving

  • 0

I’m trying to create a simple chat application for iOS. It works for receiving messages, but not for sending messages. I use Ruby and a TCPServer as the chat server (as of: twgr), it looks like this:

require 'socket'                                   

server = TCPServer.new("localhost", 80)             
chatters = []

def welcome(chatter)                               
  chatter.puts "Welcome Chatter!"
end

def broadcast(message, chatters)                   
  chatters.each do |chatter|
    chatter.puts message
  end
end

while (chatter = server.accept)                         
  Thread.new(chatter) do |c|
    welcome(chatter)                        
    chatters << chatter                            
    begin                                            
      loop do
        line = c.readline                            
        broadcast("#{line}", chatters)       
      end
    rescue EOFError                                  
      c.close
      chatters.delete(c)                             
      broadcast("Chatter has left", chatters)
    end
  end
end 

The server seems to be working ok, since I can send messages between two local telnet connections. I think the problem is my iOS client. The code I use for sending/reciving messages looks like this (as of:http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server):

- (void)messageReceived:(NSString *)message 
{
    [messages insertObject:message atIndex:0];

    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  
    withRowAnimation:UITableViewRowAnimationTop];
    [self.tableView endUpdates];
}

/*
 Method for creating messages. 
 New messages are added above older messages.
 It gets called when the user hits the "Send" button.
 */
- (IBAction)sendMessage:(id)sender {
    NSString *response  = [NSString stringWithFormat:messageField.text];
    NSData *data = [[NSData alloc] initWithData:[response  
    dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
    messageField.text = @"";
}

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {    
    switch (streamEvent) {
        case NSStreamEventHasBytesAvailable:

            if (theStream == inputStream) {

                // This NSLog message dosn't get called when I want to send messages from 
                // my iOS client.
                NSLog(@"Hello");  
                uint8_t buffer[1024];
                int len;

                while ([inputStream hasBytesAvailable]) {
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0) {

                        NSString *output = [[NSString alloc] initWithBytes:buffer 
                        length:len 
                        encoding:NSASCIIStringEncoding];                        
                        if (nil != output) {
                            NSLog(@"server said: %@", output);
                            [self messageReceived:output];
                        }
                    }
                }
            }
        break;        
        case NSStreamEventEndEncountered:
            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop]  
            forMode:NSDefaultRunLoopMode];
        break;
    }
}

I can receive messages on the iOS client when I send messages but nothing happens when I try to send messages. The client currently looks like this:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:03:48+00:00Added an answer on May 27, 2026 at 8:03 am

    This was a though nut to crack, but I think that I solved it (the trial and error way). The message receiving part seemed to work because Ruby’s puts method automatically adds a newline to the strings, which is not the case for NSStrings. This change made it work:

    NSString *response = [NSString stringWithFormat: @"%@\n", messageField.text];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.