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-08-2011, 02:29 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 1
shapemeister is on a distinguished road
Default UDP testing

Hi,

I'm attempting to write working code that can pass strings over UDP. I am attempting to do this in objective C.

Here is the code for the server, written in java:


import java.net.*;

public class UDPServer {
public static void main(String[] args) throws Exception {
int port = 8000;
DatagramSocket sock = new DatagramSocket(port);
byte[] buf = new byte[1024];
DatagramPacket recvPacket = new DatagramPacket(buf, buf.length);
DatagramPacket sendPacket = null;

while (true) {
sock.receive(recvPacket);
String msg = new String(recvPacket.getData(), 0, recvPacket.getLength());
System.out.println("receive = " + msg + ", port = " + recvPacket.getPort());

if (msg.equals("end"))
break;

if (sendPacket == null) {
sendPacket = new DatagramPacket(msg.getBytes(), msg.getBytes().length, recvPacket.getAddress(), recvPacket.getPort());
}
else {
sendPacket.setData(msg.getBytes());
sendPacket.setAddress(recvPacket.getAddress());
}
sock.send(sendPacket);
}
}
}

Here is the code I am supposed to be writing that handles the client portion of the communication, written in java:

public class UDPClient {
public static void main(String[] args) throws Exception {
int port = 8000;
InetAddress addr = InetAddress.getByName("127.0.0.1");
DatagramSocket sock = new DatagramSocket();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
byte[] buf = new byte[1024];
DatagramPacket recvPacket = new DatagramPacket(buf, buf.length);
DatagramPacket sendPacket = null;

while ((line = reader.readLine()) != null) {
if (sendPacket == null) {
sendPacket = new DatagramPacket(line.getBytes(), line.getBytes().length, addr, port);
}
else {
sendPacket.setData(line.getBytes());
}
sock.send(sendPacket);

if (line.equals("end"))
break;

sock.receive(recvPacket);
String msg = new String(recvPacket.getData(), 0, recvPacket.getLength());
System.out.println("echo = " +msg);
}
}
}



Here is my rendering of the client code in Objective c:

- (void)viewDidLoad
{
[super viewDidLoad];
int port = 8000; // 포트번호
NSString *addr = @"127.0.0.1";
sock = [[AsyncUdpSocket alloc] initWithDelegate:self];
[sock bindToPortort error:nil];
[sock enableBroadcast:YES error:nil];
Byte buffer[1024];
NSData *recvPacket = [[NSData alloc] init];
recvPacket = [NSData dataWithBytesNoCopy:buffer length: 1024];
NSData * sendPacket = [[NSData alloc] initWithBytesNoCopy:buffer length:1024];
NSInputStream * stream = [[NSInputStream alloc] init];
while([stream read: buffer maxLength: 1024] != 0) {
NSString* string = [[NSString alloc] initWithBytes:buffer length:1024 encoding: NSASCIIStringEncoding];
if (string == @"end") break;
sendPacket = [NSData dataWithContentsOfFile:string];
[sock sendData:sendPacket toHost:addr portort withTimeout:-1 tag:1];
NSLog(@"1" );

NSString *msg = [NSString stringWithFormat:@"%@", recvPacket]; //forcing the NSData to become NSString
NSLog(@"%@",msg);
[sock receiveWithTimeout:-1 tag:1];
[string release];

}
[stream release];
}

sock is an instance variable in the header file, of type AsyncUdpSocket.


Why isn't this working?!??!?!?!
shapemeister is offline   Reply With Quote
Old 07-08-2011, 11:05 AM   #2 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 16
mungbeans is on a distinguished road
Default

I've never used UDP before so can't answer that specifically.

However you've just posted a big chunk of code and said its not working.
What's not working? - you can't establish a connection?, you can but data doesn't get transmitted?, data does get transmitted but not the content you were expecting?, ....

If it were me running this code, the first thing I'd do is replace the error:nil lines with an error object and see if its successful.If not, look why not, if yes, move onto the next line and see if that works or not.

Can you even bind? You don't tell us. If not, look at why not. If yes, then what's the actual specific problem?

Last edited by mungbeans; 07-08-2011 at 01:52 PM.
mungbeans is offline   Reply With Quote
Old 07-08-2011, 03:54 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 97
Bopols is on a distinguished road
Default

Save yourself a lot of headaches and use AsyncUdpSocket
It's a wrapper class to hide all those low level CF socket calls.

AsyncUdpSocket.m - cocoaasyncsocket - AsyncSocket is an asynchronous socket networking library for Cocoa. - Google Project Hosting
Bopols is offline   Reply With Quote
Reply

Bookmarks

Tags
asyncudpsocket, objective c, socket, udp

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: 356
9 members and 347 guests
apatsufas, chemistry, lendo, leostc, Leslie80, lzwasyc, MarkC, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

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