Results 1 to 1 of 1
Thread: Socket programming using applet
- 08-20-2009, 11:35 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 1
- Rep Power
- 0
Socket programming using applet
Hi, every one
I am new to java programming:
I need an expert advice. It's urgent. Please help.
I need to create a Server socket from an applet. This socket should be able to listen to client request and send back the response. I also need a client applet which will communicate with the above server.
I tried to do this in the init method of an applet class. But it doesn't work.The code is as follows. Please help:
Am i doing the right thing. Or is there any other way.Java Code:import java.awt.Graphics; import java.awt.Color; import java.applet.Applet; import java.io.*; import java.net.*; public class HelloWorld extends Applet { StringBuffer buffer; ServerSocket server; Socket client; BufferedReader in; PrintWriter out; String line; @Override public void init() { this.setBackground(Color.WHITE); buffer = new StringBuffer(); buffer.append("Hello World!"); //Create a server and show the messge that server has been started try { server = new ServerSocket(4321); buffer.append("\n"); buffer.append("Server is ready to listen."); } catch (IOException e) { buffer.append("Could not listen on port 4321"); //System.exit(-1); } try { client = server.accept(); } catch (IOException e) { buffer.append("Accept failed: 4321"); //System.exit(-1); } try { in = new BufferedReader(new InputStreamReader( client.getInputStream())); out = new PrintWriter(client.getOutputStream(), true); } catch (IOException e) { //System.out.println("Read failed"); buffer.append("Read failed"); //System.exit(-1); } while(true) { try { line = in.readLine(); //Send data back to client out.println(line); } catch (IOException e) { //System.out.println("Read failed"); buffer.append("Read failed"); //System.exit(-1); } } } @Override public void start() { } @Override public void stop() { } @Override public void destroy() { try { server.close(); } catch (IOException e) { } } @Override public void paint(Graphics g) { g.drawRect(0, 0, getSize().width - 1, getSize().height - 1); g.drawString(buffer.toString(), 5, 15); } @Override public void update(Graphics g) { } }
Please help
Similar Threads
-
Socket applet programming error?
By Master Zero in forum Java AppletsReplies: 6Last Post: 10-05-2011, 09:15 PM -
socket programming
By Omarero in forum New To JavaReplies: 5Last Post: 12-18-2008, 04:58 PM -
Socket programming
By ikoko in forum New To JavaReplies: 1Last Post: 10-16-2008, 02:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks