Socket in iOS
来源:互联网 发布:ubuntu花屏 编辑:程序博客网 时间:2024/05/24 01:51
- TCP based Socket (also can use AsynSocket lib)
NSOutputStream *outputStream;
- (void)connect
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridgeCFStringRef)(@"127.0.0.1"),80, &readStream, &writeStream);
inputStream = (__bridgeNSInputStream *)readStream;
outputStream = (__bridgeNSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStreamscheduleInRunLoop:[NSRunLoopcurrentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStreamscheduleInRunLoop:[NSRunLoopcurrentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
- (void)sendMessage:(NSString *)message
{
NSData *data = [[NSDataalloc] initWithData:[messagedataUsingEncoding:NSASCIIStringEncoding]];
[outputStreamwrite:[data bytes]maxLength:[data length]];
}
- UDP based socket (use AsyncUdpSocket lib)
- Copy AsyncUdpSocket.h and AsyncUdpSocket.m to your project.
- Conform to AsyncUdpSocketDelegate for example,
- @interface XXXViewController :UIViewController <AsyncUdpSocketDelegate>
- (void)initUdpSocket
{
asyncUdpSocket = [[AsyncUdpSocketalloc]initWithDelegate:self];
}
- (void)sendMessage:(NSString *)message
{
NSData *data = [[NSDataalloc] initWithData:[messagedataUsingEncoding:NSUTF8StringEncoding]];
BOOL success = [asyncUdpSocketsendData:data toHost:@"127.0.0.1"port:8888 withTimeout:-1 tag:1];
NSLog(@"send upd complete: %d", success);
}
- Socket in iOS
- iOS socket
- IOS Socket
- ios socket
- ios socket
- iOS socket
- iOS socket
- iOS-Socket
- ios socket
- iOS socket
- Socket Server in JavaSE
- Socket Server in JavaME
- Socket in windows【转】
- Socket In C
- socket in Android
- select in socket
- socket server in java
- socket in java -client
- 蛋白质结构与功能的预测方法总结和资料汇总
- 如果人生不得意
- git配色问题
- Cocos2d-x内存管理的一种实现 [转]
- struts2通配符
- Socket in iOS
- Xshell客户端连接Linux服务器中文显示乱码问题的解决办法
- 数组指针和指针数组的区别
- 一起来学css之text-indent用法
- 轻量级调试器神器 - mimikatz (学习了,很强大的工具)
- java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cas
- 使用poi操作导出excel代码示例
- 解决java.net.MalformedURLException: unknown protocol问题
- 嵌入式开发视频地址,经典必备