Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Requests

Reply
 
LinkBack Thread Tools Display Modes
Old 07-14-2011, 06:58 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 3
Briksins is on a distinguished road
Question Network Programming

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
Briksins is offline   Reply With Quote
Old 07-14-2011, 03:09 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 3
Briksins is on a distinguished road
Default

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
Briksins is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone network, iphone to pc, java server iphone, server client

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 446
17 members and 429 guests
ahmedozcan, baja_yu, Droverson, iAppDeveloper, ipodphone, jasper_muc, JoeRCruso, karatebasker, laureix68, LunarMoon, PapaSmurf, peterwilli, pipposanta, Rudy, SLIC, SuperDietGenius, vogueestylee
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,962
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:12 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0