Hello everybody!
I hope you can help me with my problem, I have to solve.
I need to set up a communication between a Java based server and an iPhone (via WiFi).
The server works perfectly and without errors, nevertheless, her is the code of it:
Code:
public static boolean initServer(int port) {
System.out.println("Starting Server on: "+getIP()+":"+ port);
boolean success = false;
try {
serverSocket = new ServerSocket(port);
success = true;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
}
} catch (IOException e) {
System.err.println("Could not listen on port: " + port+ ".");
}
return success;
}
/**
* Generates a PrintWriter for Reading the clients data
* @return The PrintWriter
*/
public static PrintWriter getPrintWriter() {
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
} catch (IOException e) {
System.err.println("Could not generate PrintWriter.");
}
return out;
}
/**
* Generates a BufferedReader for sending data to the client
* @return The BufferedReader
*/
public static BufferedReader getBufferedReader() {
try {
in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
} catch (IOException e) {
System.err.println("Could not generate BufferedReader.");
}
return in;
}
For the iPhoneApp i referred to this article by apple:
Technical Q&A QA1652: Using NSStreams For A TCP Connection Without NSHost
My App connects to the server, but when I try to send to or receive from the server, the App and the iPhoneSimulator both crush.
Has anybody an idea, where I went wrong?
Is there a better way, to set up a communication?
Thank you for your help,
NomenNescio