zoukankan      html  css  js  c++  java
  • ios socket(基础demo)

    http://blog.sina.com.cn/s/blog_7a2f0a830101ecv4.html

    clinetSocket 
    
    
    1、viewcontroller.h
    @interface ViewController : UIViewController<</span>GCDAsyncSocketDelegate,UITextFieldDelegate>
    
    {
    
        GCDAsyncSocket *socket;
    
    }
    
    @property(strong)  GCDAsyncSocket *socket
    
    @property (strong, nonatomic) IBOutlet UITextField *host;
    
    @property (strong, nonatomic) IBOutlet UITextField *message;
    
    @property (strong, nonatomic) IBOutlet UITextField *port;
    
    @property (strong, nonatomic) IBOutlet UITextView *status;
    
    - (IBAction)connect:(id)sender;
    
    - (IBAction)send:(id)sender;
    
    @end
    
    viewcontroller.m
    
     
    
    //连接服务器
    
    - (IBAction)connect:(id)sender {
    
        socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; 
    
        socket.delegate = self;
    
        NSError *err = nil;
    
        BOOL connState=[socket connectToHost:host.text onPort:[port.text intValue] error:&err];
    
        if(!connState)
    
        { 
    
            [self addText:err.description];
    
        }else
    
        {
    
            NSLog(@"连接服务器:%@ 成功",host.text);
    
            [self addText:@"打开端口"];
    
        }
    
    }
    
    -(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
    
    {
    
        [self addText:[NSString stringWithFormat:@"连接到:%@",host]];
    
        [socket readDataWithTimeout:-1 tag:0];
    
    }
    
    - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
    
    {
    
        NSLog(@"socket did is connect:%@",err);
    
    }
    
    //发送数据
    
    - (IBAction)send:(id)sender {
    
        [socket writeData:[message.text dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0];
    
        [self addText:[NSString stringWithFormat:@"发送的数据:%@",message.text]];
    
        [message resignFirstResponder];
    
        [socket readDataWithTimeout:-1 tag:0];
    
    }
    
    //读取服务器获取的数据
    
    -(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
    
    {
    
        NSString *newMessage = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
        
    
        [self addText:[NSString stringWithFormat:@"服务器:%@:%@",sock.connectedHost,newMessage]];
    
        [socket readDataWithTimeout:-1 tag:0];
    
    } 
    
    
     
    
    
    
    #pragma mark - View lifecycle
    
    -(void)addText:(NSString *)str
    
    {
    
        status.text = [status.text stringByAppendingFormat:@"%@
    ",str];
    
    }
    
    - (void)viewDidLoad
    
    {
    
        [super viewDidLoad];
    
        //设置默认的服务器地址和端口
    
        host.text = @"192.168.2.107";
    
        port.text = @"54321";    
    
    // Do any additional setup after loading the view, typically from a nib.
    
    }
    
    2、serviceSocket
    
    1、appdelegate.h
    
     
    
    #import "GCDAsyncSocket.h"
    
    #import "GCDAsyncUdpSocket.h"
    
    @interface AppDelegate : NSObject <</span>NSApplicationDelegate,GCDAsyncSocketDelegate,GCDAsyncUdpSocketDelegate>
    
    {
    
        GCDAsyncSocket *socket;
    
        GCDAsyncSocket *s;
    
    }
    
    @property(strong)  GCDAsyncSocket *socket;
    
    - (IBAction)listen:(id)sender;
    
    @property (unsafe_unretained) IBOutlet NSTextView *status;
    
    @property (unsafe_unretained) IBOutlet NSTextField *port;
    
    @property (unsafe_unretained) IBOutlet NSTextField *host;
    
    @property (assign) IBOutlet NSWindow *window;
    
    2、appdelegate.m
    
     
    
    @implementation AppDelegate
    
    @synthesize status;
    
    @synthesize port;
    
    @synthesize host;
    
    @synthesize window = _window;
    
    @synthesize socket;
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    
    {
    
        //服务器的端口号
    
        port.stringValue = @"54321";
    
    }
    
    -(void)addText:(NSString *)str
    
    {
    
        status.string = [status.string stringByAppendingFormat:@"%@
    ",str];
    
    }
    
    //开始监听
    
    - (IBAction)listen:(id)sender {    
    
        NSLog(@" begin listen");
    
        socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    
        NSError *err = nil;
    
        if(![socket acceptOnPort:[port integerValue] error:&err]) 
    
        { 
    
            [self addText:err.description];
    
        }else
    
        {
    
            [self addText:[NSString stringWithFormat:@"开始监听%d端口.",port.integerValue]];
    
        }
    
    }
    
    
    
    
    
    
    
    - (void)socket:(GCDAsyncSocket *)sender didAcceptNewSocket:(GCDAsyncSocket *)newSocket
    
    {
    
        // The "sender" parameter is the listenSocket we created.
    
        // The "newSocket" is a new instance of GCDAsyncSocket.
    
        // It represents the accepted incoming client connection.
    
        // Do server stuff with newSocket...
    
        [self addText:[NSString stringWithFormat:@"建立与%@的连接",newSocket.connectedHost]];
    
        s = newSocket;
    
        s.delegate = self;
    
        [s readDataWithTimeout:-1 tag:0];
    
    }
    
    //向客户端写数据
    
    -(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
    
    {
    
        NSString *receive = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
        [self addText:[NSString stringWithFormat:@"%@:%@",sock.connectedHost,receive]]; 
    
     
    
        
    
    
    
        NSString *reply = [NSString stringWithFormat:@"服务器收到:%@",receive];
    
        [s writeData:[reply dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0];
    
        [s readDataWithTimeout:-1 tag:0];
    
    }
    
    @end
    

      

  • 相关阅读:
    JS笔记009
    JS笔记008
    JS笔记007
    JS笔记006
    JS笔记005
    JS笔记004
    JS笔记003
    JS笔记001
    CSS3笔记012
    expdp SYNONYM of publick and schema owner
  • 原文地址:https://www.cnblogs.com/apem/p/3966803.html
Copyright © 2011-2022 走看看