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];
}

Just a guess - but is an iPhone int 16 bits? (because a Java int is 32)

Just a guess - but is an iPhone int 16 bits? (because a Java int is 32)

It is a normal C integer since objective-c is a superset of C

It is a normal C integer since objective-c is a superset of C

My 1978 version of K&R says ints may be 16 or 32 bits, depending on the hardware etc. I haven't used C since then, so I still don't know how big an objective-c int is. (blushes)

Update: latest info on objective-c still defines int as being the word size for whatever the hardware is. Perhaps you should change the iphone code to write a long, rather than an int. That way you can be sure they are both 32 bits.

I made my java code read a short from the socket and I'm still getting the wrong value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.