Hello To everyone, I would like to request more help as tutorial for network programming if possible!
I was searching a lot and not only on this forum for some examples of cross-platform connection.
to be more clear I would like to Establish Connection from iPhone to PC where PC is linux base server and iPhone Client
The server side on PC written on JAVA and is ready for use(enough for start to accept connection)
Here is my Code:
//Class - MyServer.java
Code:
package server;
import java.net.*;
import java.io.*;
import java.util.concurrent.*;
public class MyServer implements Runnable {
protected int serverPort = 9050;
protected ServerSocket serverSocket = null;
protected boolean isStopped = false;
protected Thread runningThread = null;
protected ExecutorService threadPool = Executors.newFixedThreadPool(10);
protected static int counter = 1;
public MyServer(int port) {
this.serverPort = port;
}
public void run() {
synchronized (this) {
this.runningThread = Thread.currentThread();
}
openServerSocket();
while (!isStopped()) {
Socket clientSocket = null;
try {
clientSocket = this.serverSocket.accept();
if (clientSocket != null)
{
System.out.println("User Connected, Starting Thread with client ID:" + counter);
this.threadPool.execute(new ThreadAI(clientSocket, counter));
counter++;
clientSocket = null;
}
} catch (IOException e) {
if (isStopped()) {
System.out.println("Server Stopped.");
return;
}
throw new RuntimeException("Error accepting client connection",
e);
}
}
this.threadPool.shutdown();
System.out.println("Server Stopped.");
}
private synchronized boolean isStopped() {
return this.isStopped;
}
public synchronized void stop() {
this.isStopped = true;
try {
this.serverSocket.close();
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
private void openServerSocket() {
try {
this.serverSocket = new ServerSocket(this.serverPort);
} catch (IOException e) {
throw new RuntimeException("Cannot open port 9050", e);
}
}
public static void main(String[] args) {
MyServer server = new MyServer(9000);
new Thread(server).start();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please Enter Q for exit:\n");
try
{
if (br.readLine().equals("q"))
{
server.stop();
}
else
{
System.out.println("Unknown Input, Emergency server stop\n");
server.stop();
}
}
catch (IOException e)
{
System.out.println("Input Error\n");
}
}
}
//Class - ThreadAI.java
Code:
package server;
import java.io.*;
import java.net.*;
public class ThreadAI implements Runnable{
protected Socket clientSocket = null;
protected String serverText = null;
protected int ID;
String data;
BufferedReader inFromClient;
public ThreadAI(Socket clientSocket, int clientID) {
this.clientSocket = clientSocket;
this.ID = clientID;
}
public void run() {
try {
inFromClient = new BufferedReader(new InputStreamReader (clientSocket.getInputStream()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("New Thread Started with client ID: " + ID);
while (true)
{
try
{
data = inFromClient.readLine();
if (data != null)
{
if (data.equals("q"))
{
clientSocket.close();
break;
}
else
{
System.out.println("Client ID: " + this.ID + " says: "+ data);
}
}
}
catch (IOException e)
{
// report exception somewhere.
e.printStackTrace();
}
}
System.out.println("Thread with Client ID: " + ID +" is Canceled");
}
}
While i was writing this small server i had to implement
Client for test purpose
//Class - MyClient.java
Code:
package client;
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String argv[]) throws Exception {
String ToServer;
Socket clientSocket = new Socket("localhost", 9000);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter outToServer = new PrintWriter(
clientSocket.getOutputStream(), true);
while (true) {
System.out.println("SEND(Type Q or q to Quit):");
ToServer = in.readLine();
if (ToServer.equals("Q") || ToServer.equals("q")) {
outToServer.println(ToServer);
clientSocket.close();
break;
}
else {
outToServer.println(ToServer);
}
}
}
}
but the main point is to transfer this client to the iPhone
If someone could help me out with socket programming for iPhone and implement same client for iPhone it would be amazing, and good tutorial for all members of this forum and Google searchers
Best Regards
Vadims Briksins