I am trying to make a java sever code communicate with and iphone hello world app. When I run the iphone app it sends the length of the string first then sends the string. the Length of the string should be 2 but my server code prints out 33554432. Also when my program moves onto where it reads the string it hangs there. When I go and debug the java code in XCode my debugger tells me that it can not find the file of the object that needs to be debugged. Could all my troubles be caused by Java update 4 for Mac OS X 10.5 or is it something else.
Any input is much appreciated.
Java Server Code:
//
// HelloWorldServer.java
// HelloWorldServer
//
// Created by Sharon White on 7/4/09.
// Copyright (c) 2009 __MyCompanyName__. All rights reserved.
//
import java.util.*;
import java.net.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.PrintWriter;
public class HelloWorldServer {
public static void main (String args[]) throws IOException {
ServerSocket HWServerSocket = null;
System.out.println("Running....");
try {
HWServerSocket = new ServerSocket(12345);
System.out.println("Socket created");
} catch (IOException e) {
System.out.println("Could not listen on port: 12345");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = HWServerSocket.accept();
System.out.println("Connection Made");
} catch (IOException e) {
System.out.println("Accept Failed");
System.exit(1);
}
DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
String outputLine, inputLine;
int size = 0;
try {
size = in.readInt();
System.out.println("Size of strin is " + Integer.toString(size));
while((inputLine = in.readUTF()) != null) {
System.out.println(inputLine);
if(inputLine.equals("hi")) {
out.writeUTF("Hello World");
} else {
out.writeUTF("Bad input");
}
}
} catch(UTFDataFormatException e) {
System.out.println("Bad format");
}
}
}
iphone code:
- (void)viewDidLoad {
socket = [[LXSocket alloc] init];
if ([socket connect: @"10.104.10.169" port: 12345] == NO) {
[socket release];
NSLog(@"cannot create socket");
} else {
NSLog(@"Created Socket");
}
[super viewDidLoad];
}
- (IBAction)buttonPressed:(id)sender {
[socket sendString:@"hi"];
NSString *received = [socket readString];
if(received == nil) {
NSLog(@"nothing received");
} else {
Hello.text = received;
}
}
- (void)sendString:(NSString*)string {
const char* s = [string UTF8String];
int len = strlen(s);
[self sendInt: len];
[self sendBytes: s length: len + 1];
}