iOS Development Socket Communication

Source: Internet
Author: User

Write the socket communication thing. Socket communications company is used to send some instructions to the server for the control of smart home devices. The socket is nothing more than send over the past, as to the specific content with the server to negotiate. Let's talk about the Mac tool for the socket tool used with the socket. First open the tools ( Tool connection and file used by socket: sockettoolfor_mac_and_third_for_xocde_socket.zip) Select TCP Server and then click Create Port number to enter a number, 60000. Note may be completed after this data will change, get the computer IP address is not OK, you go to the notebook settings in the network to view their computer IP. For OC code authoring, the MAC Socket Tools tool opens, then the code creates the connection, then sends the message, Sockettool The tool needs to click on TCP Servier and then click on the IP that is sent to receive the data. You can also reply to the phone client/complete, directly on the code

#import "ControlDeviceViewController.h"

#import "GNASocket.h"

@interface Controldeviceviewcontroller ()

{

BOOL IsON;

}

@property (nonatomic, strong) Gcdasyncsocket *socket;

@property (Strong, nonatomic) Iboutlet Uiimageview *devicestatus;

@property (Strong, nonatomic) Iboutlet UIButton *turnonoroff;

@end

@implementation Controldeviceviewcontroller

-(void) Viewdidload {

[Super Viewdidload];

IsON = NO;

[Self connect];

Do any additional setup after loading the view from its nib.

}

Linking with the server

-(void) Connect

{

1. Create socket

Self.socket = [[Gcdasyncsocket alloc] initwithdelegate:self delegatequeue:dispatch_get_main_queue ()];

2. Connect to the server's socket

Nserror *error = nil;

BOOL result = [Self.socket connecttohost:@ "IP" OnPort: port (int) error:&error];

3. Determine if the link is successful

if (result) {

NSLog (@ "Client Link server succeeded");

} else {

NSLog (@ "Client Link server failed");

}

}

Send Message

-(Ibaction) Sendmassage: (UIButton *) sender

{

if (IsON) {//Open state

Self.deviceStatus.image = [UIImage imagenamed:@ "CP"];

[Self.turnonoroff setimage:[uiimage imagenamed:@ "Normal"] forstate:uicontrolstatenormal];

}else{

Self.deviceStatus.image = [UIImage imagenamed:@ "Cp_kai"];

[Self.turnonoroff setimage:[uiimage imagenamed:@ "Active"] forstate:uicontrolstatenormal];

}

[Self.socket writedata:[@ "Data Sent" datausingencoding:nsutf8stringencoding] withtimeout:-1 tag:0];

IsON =!ison;

}

#pragma mark-gcdasyncsocketdelegate

Client Link server side succeeds, client gets address and port number

-(void) Socket: (Gcdasyncsocket *) sock didconnecttohost: (NSString *) host port: (uint16_t) port

{

NSLog (@ "%@", [nsstring stringwithformat:@ "link Server%@", host]);

Gnasocket *socket = [Gnasocket defaultscocket];

Socket.mysocket = Self.socket;

[Self.socket readdatawithtimeout:-1 tag:0];

}

The client has acquired the content

-(void) Socket: (Gcdasyncsocket *) sock didreaddata: (NSData *) data withtag: (long) tag

{

NSString *content = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];

NSLog (@ "%@", content);

[Self.socket readdatawithtimeout:-1 tag:0];

}

iOS Development Socket Communication

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.