Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Network Programming

BriksinsBriksins Posts: 3New Users
edited July 2011 in Tutorial Requests
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
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
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
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
Post edited by Briksins on

Replies

  • BriksinsBriksins Posts: 3New Users
    edited July 2011
    Hmm looks like im sorted,

    This source help very much: Socket communication on the iPhone - Mobile Development Wiki

    now im able to connect to the server, well at list server detecting my connection, im not able to chat yet, but im working on it.
    but im still confuse with global picture! Guys could you please explain to me how it works? As far as i understand i can do some event triggering instead of constant loop trough some delegates
    but i'm not fully understand that concept
    could you explain to me how it works or refer me to some good source or good code example or at leas name of the open source app (for example as part of some tutorial) i should look in

    i'm very new to iphone dev, however within 1 day i did some progress, so im not hopeless :)
    Please help me with advise
  • rajayrajay Posts: 4New Users Noob
    This link not working...
    Socket communication on the iPhone - Mobile Development Wiki
Sign In or Register to comment.