Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 105 lines (89 sloc) 3.462 kB
dc5be1c Initial check-in.
uli authored
1 //
2 // NSFileHandle+SocketConnection.m
3 // thiSwaY
4 //
5 // Created by Sander Niemeijer on Sun Dec 23 2001.
720b0e7 @uliwitness Added licensing information to the source files.
authored
6 // Copyright 2001-2003 by Uli Kusterer and Sander Niemeijer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
dc5be1c Initial check-in.
uli authored
26 //
27
28 /* -----------------------------------------------------------------------------
29 Headers:
30 -------------------------------------------------------------------------- */
31
32 #import <netdb.h>
33 #import <netinet/in.h>
34 #import <sys/socket.h>
35 #import <unistd.h>
36 #import <fcntl.h>
37
38 #import "NSFileHandle+SocketConnection.h"
39
40
41 @implementation NSFileHandle (UKFileHandleSocketConnection)
42
43 /* -----------------------------------------------------------------------------
44 fileHandleForConnectionToHost:atPort:
45 Uses Unix socket APIs to create a file descriptor to a socket, and
46 then generates an NSFileHandle for that file descriptor that can be
47 used for actual asynchronous data transfer.
48 -------------------------------------------------------------------------- */
49
50 +(id) fileHandleForConnectionToHost: (NSString*)hostname atPort: (unsigned short)port
51 {
52 return [self fileHandleForConnectionToHost: hostname atPort: port type: SOCK_STREAM];
53 }
54
55 +(id) fileHandleForDatagramToHost: (NSString*)hostname atPort: (unsigned short)port
56 {
57 return [self fileHandleForConnectionToHost: hostname atPort: port type: SOCK_DGRAM];
58 }
59
60 +(id) fileHandleForConnectionToHost: (NSString*)hostname atPort: (unsigned short)port type: (int)typ
61 {
62 struct hostent *hostinfo;
63 struct sockaddr_in remoteAddr;
64 int fd;
65 int i;
66
67 // Resolve the hostname
68 hostinfo = gethostbyname( [hostname cString] );
69 if( hostinfo == NULL )
70 {
71 [NSException raise:@"NSFileHandleSocketConnection"
72 format:@"Could not find host"];
73 }
74
75 // Retrieve a socket
76 fd = socket( PF_INET, typ, 0 );
77 if( fd == -1 )
78 {
79 [NSException raise:@"NSFileHandleSocketConnection"
80 format:@"Could not open a socket"];
81 }
82
83 // Try to make a connection to the host by trying all possible addresses
84 bzero( (char*) &remoteAddr, sizeof(remoteAddr) );
85 remoteAddr.sin_family = AF_INET;
86 remoteAddr.sin_port = htons(port);
87 for( i=0; hostinfo->h_addr_list[i] != NULL; i++ )
88 {
89 remoteAddr.sin_addr = *(struct in_addr *)hostinfo->h_addr_list[i];
90 if( connect(fd,(struct sockaddr *)&remoteAddr,sizeof(remoteAddr)) == 0 )
91 {
92 // Connection was succesful:
93 return [[[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:YES] autorelease];
94 }
95 }
96
97 // No connection could be made
98 [NSException raise:@"NSFileHandleSocketConnection" format:@"Could not connect to host"];
99
100 // Should never be reached
101 return nil;
102 }
103
104
105 @end
Something went wrong with that request. Please try again.