Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Sockets and Internet Protocols
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Paul Clapham
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Saloon Keepers:
Tim Holloway
Carey Brown
Roland Mueller
Piet Souris
Bartenders:
Forum:
Sockets and Internet Protocols
MY server just hanged
feda alshahwan
Ranch Hand
Posts: 170
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I created a small code for implementing mobile web server but when I try to run it it just doesnot proceed I guess it is a threading problem could you please Check my code and help me to solve it.
import java.io.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; /** * @author fa00064 */ public class MobileWebServerMidlet extends MIDlet implements CommandListener,Runnable{ private Form form=new Form("Creating Mobile Web Server"); private Command startservercommand = new Command("Start Server",Command.OK,1); private StringItem stringitem=new StringItem("Server"," If You Want to Start Server Press Start Server Button"); private Display display; private ServerSocketConnection scn; private byte DELAY,LINGER,KEEPALIVE,RCVBUF,SNDBUF; public void startApp() { form.append(stringitem); form.addCommand(startservercommand); Display.getDisplay(this).setCurrent(form); form.setCommandListener(this); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c,Displayable d){ if (c==startservercommand) { try { scn = (ServerSocketConnection) Connector.open("socket://:30000"); Thread t=new Thread(this); t.start(); } catch (IOException ex) { ex.printStackTrace(); } //display.setCurrent(form); //run(); destroyApp(false); } } public void run(){ // Create the server listening socket for port 30000 // ServerSocketConnection scn = null; try { form.append("Waiting for Client Request....." + scn.getLocalAddress()); while (true){ SocketConnection sc; try { sc = (SocketConnection) scn.acceptAndOpen(); RequestHandler RH= new RequestHandler (sc); } catch (IOException ex) { ex.printStackTrace(); } } } catch (IOException ex) { ex.printStackTrace(); } } // Wait for a connection. public class RequestHandler extends Thread{ SocketConnection sc; RequestHandler (SocketConnection sc){ this.sc=sc; start(); // Set thread priority down to allow server to accept new requests setPriority(NORM_PRIORITY-1); } public void run(){ try{ form.append("Request Accepted on port number:"+scn.getLocalPort()); // Set application specific hints on the socket. sc.setSocketOption(DELAY, 0); sc.setSocketOption(LINGER, 5); sc.setSocketOption(KEEPALIVE, 0); sc.setSocketOption(RCVBUF, 128); sc.setSocketOption(SNDBUF, 128); // Get the input stream of the connection. InputStream is = sc.openInputStream(); // Get the output stream of the connection. OutputStream os = sc.openOutputStream(); // Read the input data. System.out.println("Going to read data..."); int ch = 0; String result= ""; //while(ch != 23 && ch != 13) while (ch!=-1){ ch = is.read(); result=result+(char)ch; } //System.out.println("Data Read ..."); form.append(result); is.close(); os.write("WellCome Client".getBytes()); System.out.println("data sent to buffer..."); os.flush(); os.close(); sc.close(); scn.close(); } catch (IOException e){ System.err.println(e) ; e.printStackTrace(); } } } }
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Sockets in J2ME
HTTPConnection in mobiles
NullPointerException
How to read text file in memory card from MIDlet?
Can not connect server socket over internet
More...