A fast, synchronous Objective-C wrapper around BSD sockets for iOS and OS X.
Send and receive raw bytes over a socket as fast as possible. Includes methods
for transferring files while optionally computing a checksum for verification.
Use this class if fast network communication is what you need. If you want to
do something else while your network operations finish, then an asynchronous
API might be better.
For more information, please visit the project homepage.
FastSocket is also available as a CocoaPod.
下载 FastSocket 的最新版本或尝试 夜间版本.
创建并连接客户端套接字。
FastSocket *client = [[FastSocket alloc] initWithHost:@"localhost" andPort:@"34567"];
[client connect];
发送文件。
long sent = [client sendFile:@"/tmp/filetosend.txt"];
接收指定长度的文件。
long received = [client receiveFile:@"/tmp/newlyreceivedfile.txt" length:1024];
发送字符串。
NSData *data = [@"test" dataUsingEncoding:NSUTF8StringEncoding];
long count = [client sendBytes:[data bytes] count:[data length]];
接收字符串。
char bytes[expectedLength];
[client receiveBytes:bytes count:expectedLength];
NSString *received = [[NSString alloc] initWithBytes:bytes length:expectedLength encoding:NSUTF8StringEncoding];
发送原始字节。
char data[] = {42};
long sent = [client sendBytes:data count:1];
接收给定限制内的可用原始字节。
char data[42];
long received = [client receiveBytes:data limit:42];
接收指定数量的精确原始字节。
char data[1000];
long received = [client receiveBytes:data count:1000];
关闭连接。
[client close];
请查看单元测试以获取更多如何使用这些类的示例。
2017年11月01日 — v1.6
• Fixed a long-standing bug where the internal socket descriptor could become closed but not zeroed out.
2017年10月28日 — v1.5
• Annotated code to improve auto-generated Swift interface.
• Fixed several documentation issues.
• Added document for contributions.
2017年10月06日 — v1.4
• Changed -[FaskSocket timeout] and -[FaskSocket setTimeout:] methods so that the timeout value is a float, in order to handle sub-second values.
• Fixed a compatibility issue with Xcode 9.
• Added several unit tests.
2015年01月27日 — v1.3
• Changed -[FaskSocket sendBytes:count:] method to return the actual number of bytes received instead of a BOOL. Now it matches the Readme.
• Fixed a compiler warning caused by returning NO instead of nil from one of the init methods.
• Added several unit tests.
2014年02月03日 — v1.2
• Added -[FastSocket connect:] method for specifying a connection timeout, which is separate from the read/write timeout.
• Added CocoaPod support with new podspec file.
2013年10月03日 — v1.1
• Converted to ARC.
• Added -[FastSocket isConnected] method.
• Added -[FastSocket receiveBytes:count:] method for receiving an exact number of bytes. This differs from -[FastSocket receiveBytes:limit:] in that the new method waits for the given number of bytes is received, or a timeout, before returning.
• Added header documentation for use in Xcode 5.
2012年06月24日 — v1.0
• Initial release.
FastSocket 在 MIT license 下可用。