Write an application, but not perfect. Please heroes pointing pointing! My client is written in an iPhone application, socket is CFsocket to write. Server under Windows, using MFC to write. Follow the TCP protocol. Has now passed the test! But there is a problem is when the server disconnects, the client does not listen to disconnect the server, would like to ask how to write a listener, when the server is disconnected, the client timely exit and return to re-connect interface ! Thank you first ~
Connection diagram and send diagram:
http://www.cocoachina.com/bbs/read.php?tid -48722.html
Connection code is as follows:
- (Void) doConnect {
CFSocketContext CTX = {0, self, NULL, NULL, NULL};
/ / Create the socket of TCP
_socket = CFSocketCreate (kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketConnectCallBack, TCPServerConnectCallBack, & CTX);
/ / Create Error
if (NULL == _socket) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "" message: @ "Create socket error" delegate: nil cancelButtonTitle: @ "Close" otherButtonTitles: nil];
[Alert show];
[Alert release];
}
struct sockaddr_in Server_addr;
memset (& Server_addr, 0, sizeof (Server_addr));
Server_addr.sin_len = sizeof (Server_addr);
Server_addr.sin_family = AF_INET;
Server_addr.sin_addr.s_addr = htonl (0xc0a800c8L); / / Only 192.168.2.202 allowed
Server_addr.sin_port = htons (70); / / Only Port 70 allowed
/ / Server_addr.sin_addr.s_addr = inet_addr ([connController.textField.text UTF8String]);
CFDataRef address = CFDataCreate (kCFAllocatorDefault, (UInt8 *) & Server_addr, sizeof (Server_addr));
/ / Connect the the server's socket
CFSocketConnectToAddress (_socket, address, -1);
CFRunLoopRef cfrl = CFRunLoopGetCurrent ();
CFRunLoopSourceRef source = CFSocketCreateRunLoopSource (kCFAllocatorDefault, _socket, 0);
CFRunLoopAddSource (cfrl, source, kCFRunLoopCommonModes);
CFRelease (source);
CFRunLoopRun ();
while (1) {
struct timeval tv;
tv.tv_usec = 0;
tv.tv_sec = 1;
select (-1, NULL, NULL, NULL, & tv);
}
}
Send code below:
- (Void) sendMessage {
NSString * stringToSend = [sendController.textField.text stringByAppendingString: @ "\ n"];
const char * data = [stringToSend UTF8String];
send (CFSocketGetNative (_socket), data, strlen (data) + 1, 0);
NSRange endRange;
endRange.location = [sendController.textView.text length];
endRange.length = [stringToSend length];
sendController.textView.text = [sendController.textView.text stringByAppendingString: [@ "Client:" stringByAppendingString: stringToSend]];
[SendController.textView scrollRangeToVisible: endRange];
sendController.textField.text = @ "";
}
Reply:
Your question, in fact, TCP / IP has to say, please go see TCP / IP Elaborates. Server is broken, you will receive something. Go look at the agreement.
Reply:
gooood
Reply:
Reply:
Problem solved ~
Reply:
Friends of the solution to give it! Brother also encountered this problem, thank you! ! ! !
Reply:
Can I look at your landlord server and client code? Lend me refer oh, if you can send to my mailbox Oh
wangminqing10@yahoo.cn
Oh extremely grateful
Reply:
This can not give it, sorry!
Reply:
Now really study how the iPhone sends the request to the visual studio project under the windows system and put pictures to visual studio window system under the? You have to know
No comments:
Post a Comment