Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-10-2010, 08:47 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 1
xasha is on a distinguished road
Default Socket transmission

Hi all and thx for your attention,

I'm from Switzerland, so please forgive my english errors !

I'm trying to communicate with a java server on a PC from my iPhone. I use actually the smallsocket wrapper (http://smallsockets.sourceforge.net/) but have tried to do it with this sample of code :
Code:
-(IBAction) connect_button {
 
	CFWriteStreamRef writeStream = NULL;
 
	CFStringRef host = CFSTR("X.X.X.X");
	UInt32 port = X;
 
	CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, NULL, &writeStream);
 
	CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
 
	if(!CFWriteStreamOpen(writeStream)) {
 		NSLog(@"Error Opening Socket");
	}
	else{
 		UInt8 buf[] = "Hello WOrld!";
 		int bytesWritten = CFWriteStreamWrite(writeStream, buf, strlen((char*)buf));
 		NSLog(@"Written: %d", bytesWritten);
 
		if (bytesWritten < 0) {
 			 CFStreamError error = CFWriteStreamGetError(writeStream);
 			 /** How do I print out description? All i get is -1 here. i.e What is perror() equivalent? **/
 		}
  
	}
 
	CFWriteStreamClose(writeStream);
  
}
This is my code with smallsocket :

Code:
-(BOOL) connectionTo:(NSString*)host port:(int)port {
	
	socket = [Socket socket];
	@try{
		[socket connectToHostName:host port:port];
		[socket writeString:@"Hello World!"];
		[socket retain];
	}
	@catch (NSException* exception) {
		NSLog(@"socket error");
		socket = nil;
		[socket release];
		return NO;
	}
	
	//** Disconnect **//
	[socket close];
	 
	return YES;
}
In both case, if I don't write the red lines, my server receives the data only when I quit the iphone app... If i close the stream or the socket, it'll receive the data immediately... but I don't want to close it... Is there something wrong in my code that I omitted ?

Thx for reading and for your comment if you have any idea about that !

Last edited by xasha; 07-10-2010 at 12:51 PM.
xasha is offline   Reply With Quote
Old 07-21-2010, 11:33 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 3
ivenoclue is on a distinguished road
Default

Your code is fine. The -1 means that the port you are trying to connect to is rejecting the push..


K.
Quote:
Originally Posted by xasha View Post
Hi all and thx for your attention,

I'm from Switzerland, so please forgive my english errors !

I'm trying to communicate with a java server on a PC from my iPhone. I use actually the smallsocket wrapper (http://smallsockets.sourceforge.net/) but have tried to do it with this sample of code :
Code:
-(IBAction) connect_button {
 
	CFWriteStreamRef writeStream = NULL;
 
	CFStringRef host = CFSTR("X.X.X.X");
	UInt32 port = X;
 
	CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, NULL, &writeStream);
 
	CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
 
	if(!CFWriteStreamOpen(writeStream)) {
 		NSLog(@"Error Opening Socket");
	}
	else{
 		UInt8 buf[] = "Hello WOrld!";
 		int bytesWritten = CFWriteStreamWrite(writeStream, buf, strlen((char*)buf));
 		NSLog(@"Written: %d", bytesWritten);
 
		if (bytesWritten < 0) {
 			 CFStreamError error = CFWriteStreamGetError(writeStream);
 			 /** How do I print out description? All i get is -1 here. i.e What is perror() equivalent? **/
 		}
  
	}
 
	CFWriteStreamClose(writeStream);
  
}
This is my code with smallsocket :

Code:
-(BOOL) connectionTo:(NSString*)host port:(int)port {
	
	socket = [Socket socket];
	@try{
		[socket connectToHostName:host port:port];
		[socket writeString:@"Hello World!"];
		[socket retain];
	}
	@catch (NSException* exception) {
		NSLog(@"socket error");
		socket = nil;
		[socket release];
		return NO;
	}
	
	//** Disconnect **//
	[socket close];
	 
	return YES;
}
In both case, if I don't write the red lines, my server receives the data only when I quit the iphone app... If i close the stream or the socket, it'll receive the data immediately... but I don't want to close it... Is there something wrong in my code that I omitted ?

Thx for reading and for your comment if you have any idea about that !
ivenoclue is offline   Reply With Quote
Reply

Bookmarks

Tags
nsstream, smallsocket, socket

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 328
15 members and 313 guests
akphyo, alexP, cgokey, EXOPTENDAELAX, flamingliquid, guusleijsten, mariano_donati, ohmniac, Paul Slocum, PavelSea, SLIC, Sloshmonster, Sonuye857, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,653
Threads: 94,115
Posts: 402,888
Top Poster: BrianSlick (7,990)
Welcome to our newest member, ohmniac
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:32 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0