11 - 網絡編程之設備間通訊原理

1、網絡編程編程

1表現形式:一臺機子上的應用程序和另一個設備的程序之間可以互相交換數據。api

2 7層網絡結構服務器

硬件層:解決硬件鏈接問題網絡

數據鏈路層:解決硬件之間可以向指定設備傳輸數據框架

IP:爲設備提供一個虛擬的網絡邏輯地址(門牌號)socket

MAC:設備的物理地址(網卡)編碼

3 TCP / UDPatom

是兩個不一樣的數據傳輸協議,就是解析字節的方式不一樣spa

目的:保證數據在設備和設備之間可以傳輸code

協議:有結構的字節串

TCP 一對一傳輸 可靠

UDP 一對多傳輸 不可靠(如電視機)

4 socket(套接字)

會話握手

設備與設備的基本通訊原理

步驟:A->B

1B設置監聽端口(大一點8000+由於小號已經被佔用了)

2A->B要兩大要素:

    ①、找到BIP地址

    ②、找到B的監聽端口

3B監聽到端口進行保留

4、進行傳輸和接收數據。。。

注意:傳輸的數據是以字節爲單位(NSData

 

首先確認跟另外一臺機子試下可否ping成功

ping 192.168.1.xx

ctrl + c 中止

 

準備:(clientserver

導入AsyncSocket

AsyncSocket.m 標記爲 -fno-objc-arc

導入CFNetWork框架

 

server代碼:

//
// MXViewController.m
// day1102_TCPSocketServer
//
// Created by tarena on 14-3-7.
// Copyright (c) 2014年 tarena. All rights reserved.
//

#import "MXViewController.h"
#import "AsyncSocket.h"


@interface MXViewController () <AsyncSocketDelegate>

@property (nonatomic, strong) AsyncSocket *serverSocket;
@property (nonatomic, strong) AsyncSocket *inSocket;
- (IBAction)tap:(id)sender;

@end

@implementation MXViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)tap:(id)sender {

self.serverSocket = [[AsyncSocket alloc] init];
self.serverSocket.delegate = self;

// 監聽端口
[self.serverSocket acceptOnPort:8000 error:Nil];


}

-(void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{
self.inSocket = newSocket;
NSLog(@"鏈接成功");
// 讀取數據
[self.inSocket readDataWithTimeout:-1 tag:0]; // -1表示沒有時間限制 一直接收

}

-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
// NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(@"%@", text);
//
// 對象反歸檔
NSKeyedUnarchiver *unarc = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
id playerObj = [unarc decodeObjectForKey:@"player"];



}

@end

 

client代碼:

 

//
//  MXViewController.m
//  day1101_TCPSocketClient
//
//  Created by tarena on 14-3-7.
//  Copyright (c) 2014年 tarena. All rights reserved.
//

#import "MXViewController.h"
#import "AsyncSocket.h"
#import "MXPlayer.h"

@interface MXViewController ()

@property (nonatomic, strong) AsyncSocket *clientSocket;
- (IBAction)tap:(id)sender;
- (IBAction)send:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *textField;
- (IBAction)sendMessage:(id)sender;


@end

@implementation MXViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)tap:(id)sender {
    
    self.clientSocket = [[AsyncSocket alloc] init];
    
    // 鏈接服務器
    [self.clientSocket connectToHost:@"192.168.1.202" onPort:8000 withTimeout:-1 error:Nil];
    
}

- (IBAction)send:(id)sender {
    
    NSData *data = [@"hello" dataUsingEncoding:NSUTF8StringEncoding];
    
    [self.clientSocket writeData:data withTimeout:-1 tag:0];
    
    
}
- (IBAction)sendMessage:(id)sender {
    MXPlayer *player = [[MXPlayer alloc] init];
    player.name = self.textField.text;
    
    // 對象歸檔  對象->data
    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *arc = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    // 編碼
    [arc encodeObject:player forKey:@"player"];
    
    [arc finishEncoding];
    
    
    [self.clientSocket writeData:data withTimeout:-1 tag:0];
    
}
@end

 

傳輸數據

對象 -> NSString ->Data 跨平臺

對象 -> Data

用途:

和服務器進行數據交互

調用遠程服務器應用程序的api,並傳輸對象

 

傳輸圖像

client端有一個textField和一個獲取按鈕

server端什麼都沒有

server端的程序上的documents目錄下,存放圖片到documents

client端輸入圖片名稱,發送到server

server端將一樣名稱的圖片返回給client

1.若是圖片再也不,

在客戶端的另一個label上顯示沒有這張圖片

 

2.支持二次搜索

相關文章
相關標籤/搜索