Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Server with SmallSockets

Server with SmallSockets
Thread Tools
Senior User
Join Date: Jan 2002
Location: City of Beck's beer
Status: Offline
Reply With Quote
Feb 4, 2003, 08:17 AM
 
Hi, i'm trying to make a small server application using SmallSockets. But I have a few questions. To start off: do I have to create multiple (listening) sockets meaning when one (listening) socket is occupied, create a new one or does that somehow work automatically? And if it works automatically how do I get the data?

I hope anyone can help me,

cheers, Thilo
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Feb 4, 2003, 03:02 PM
 
I haven't looked at SmallSockets in awhile, but I do remember it being a very thin wrapper around BSD sockets.

You only need one listening socket. When your listening socket accepts a connection it creates a new socket that represents your endpoint of the communication.
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Feb 4, 2003, 03:04 PM
 
I think he packaged a little client/server example with the distribution. You might want to take a look at it.
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Feb 5, 2003, 12:55 PM
 
I downloaded SmallSockets today and saw that he only provided an example of writing a client, so I whipped up a small example. I'll post the most relevant parts. It's not very well designed as far as resource usage, but I wanted to keep the code as simple to follow as possible. This design handles one client connection at a time. If another client connects, it waits in a queue. If you would like the full project, just let me know and I'll post it somewhere.

From server_main.m:
Code:
// Initialize the network connection listener = [BufferedSocket bufferedSocket];[listener listenOnPort:ECHO_PORT]; NSLog(@"Listening for connections on port %d.", ECHO_PORT); // Sit in an infinite loop. while(YES) { incoming =[listener acceptConnectionAndKeepListening]; NSLog(@"Client connection established"); server = [[[EchoServer alloc] initWithSocket:incoming] autorelease]; // If we wanted to serve multiple clients, we could spawn off a thread here. // We could also register with inetd and use a process per connection. [server serviceClient]; NSLog(@"Client connection terminated"); }
From EchoServer.m:
Code:
-(void) serviceClient { NSData *data; NSString *line; while([_socket isConnected]) { //Read a line of input data = [_socket readDataUpToString:@"\n"]; line = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // Echo input to client [_socket writeString:line]; [line release]; } }
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Feb 5, 2003, 02:00 PM
 
Just made a multi-threaded version if anyone is interested.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:45 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2