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 bindToPort

ort 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 port

ort 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?!??!?!?!