 |
 |
Server with SmallSockets
|
 |
|
 |
|
Senior User
Join Date: Jan 2002
Location: City of Beck's beer
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
Just made a multi-threaded version if anyone is interested.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |