Wednesday, January 15, 2014

Use socket third-party libraries to communicate AsyncSocket Mobile PC client and server side

Ios application in a third-party libraries I use socket to communicate AsyncSocket mobile client and PC server. The server can receive information sent by the client, but also return information, but the client did not receive this information returned, this why? Here is part of the code



# Import "ViewController.h"

@ Implementation ViewController
@ Synthesize socket;
@ Synthesize host;
@ Synthesize message;
@ Synthesize port;
@ Synthesize status;
@ Synthesize button;


- (Void) didReceiveMemoryWarning
{
[Super didReceiveMemoryWarning];
/ / Release any cached data, images, etc that aren't in use.
}

# Pragma mark - View lifecycle
- (Void) addText: (NSString *) str
{
status.text = [status.text stringByAppendingFormat: @ "% @ \ n", str];
}

- (Void) viewDidLoad
{
[Super viewDidLoad];
host.text = @ "61.191.200.214";
port.text = @ "9631";

[Button setTitle: @ "connection" forState: UIControlStateNormal];
}
- (BOOL) textFieldShouldReturn: (UITextField *) textField
{
[TextField resignFirstResponder];
return YES;
}
- (Void) viewDidUnload
{
[Self setHost: nil];
[Self setMessage: nil];
[Self setStatus: nil];
[Self setPort: nil];
[Super viewDidUnload];
}

- (Void) viewWillAppear: (BOOL) animated
{
[Super viewWillAppear: animated];
}

- (Void) viewDidAppear: (BOOL) animated
{
[Super viewDidAppear: animated];
}

- (Void) viewWillDisappear: (BOOL) animated
{
[Super viewWillDisappear: animated];
}

- (Void) viewDidDisappear: (BOOL) animated
{
[Super viewDidDisappear: animated];
}

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
{
return (interfaceOrientation = UIInterfaceOrientationPortraitUpsideDown!);
}

- (Void) socket: (GCDAsyncSocket *) sock didConnectToHost: (NSString *) host port: (uint16_t) port
{
[Self addText: [NSString stringWithFormat: @ "connected to:% @", host]];
[Socket readDataWithTimeout: -1 tag: 0];
}

- (Void) socketDidDisconnect: (GCDAsyncSocket *) sock withError: (NSError *) err
{
}
- (IBAction) send: (id) sender {/ / Start button
/ / Connect server
socket = [[GCDAsyncSocket alloc] initWithDelegate: self delegateQueue: dispatch_get_main_queue ()];
NSError * err = nil;
if ([socket connectToHost: host.text onPort: [port.text intValue] error: & err]!)
{
[Self addText: err.description];
} Else
{
[Self addText: [NSString stringWithFormat: @ "% @ open port", socket.localHost]];
}

/ / Send data
if (message.text.length! = 0) {
NSString * str_send = [[NSString alloc] initWithString: [NSString stringWithFormat: @ "send% @", message.text]];
[Socket writeData: [str_send dataUsingEncoding: NSUTF8StringEncoding] withTimeout: -1 tag: -1];
[Self addText: [NSString stringWithFormat: @ "Send:% @", message.text]];

} Else {
NSString * str_send = @ "receive";
[Socket writeData: [str_send dataUsingEncoding: NSUTF8StringEncoding] withTimeout: -1 tag: -1];
[Message resignFirstResponder];
[Socket 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]];
[Socket readDataWithTimeout: -1 tag: 0];
}

@ End
Reply:
(Void) socket: (GCDAsyncSocket *) sock didReadData: (NSData *) data withTag: (long) tag
Commissioning of the data is empty
I NSString * receive = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; sentence followed by sending data to the server content, the server can still receive. Why do I get the data to the server to return information is empty it? ?
Server where the display is returned to the client has specified the information. Pro, emergency ah!
Reply:
Check it out everyone
Reply:
Use socket third-party libraries to communicate AsyncSocket Mobile PC client and server side. . . . I would not want to come to see a solution, the result no one answered uh. . . Top. . With demand! ! !

No comments:

Post a Comment