Forum Stats

  • 3,851,951 Users
  • 2,264,054 Discussions
  • 7,904,915 Comments

Discussions

Simple Server/Client Applet example code ???

843807
843807 Member Posts: 46,582
edited Oct 4, 2002 2:00PM in Java Applet Development
Hi to all.
I would like to ask if possible to someone to tell me some example code to do this simple thing:

I want an applet showing only a TextField and a button. When the user writes something on the textfield and press the button, it should send the text in the textfield to the "server", and then it should save it in a variable.

I think there should be 2 different classes at least (server and client), but no idea in how could I make this on Java.

So I am asking if possible for some sample code for make this.

Thank you for the information

John

Comments

  • 843807
    843807 Member Posts: 46,582
    hello!

    Here is a console based Server which will recieve the string and disply on the DOS screen.
    /////////////////////////////////// Server /////////////////
    import java.net.*;
    import java.io.*;
    import java.util.*;

    public class ChatServer extends Thread
    {
    ServerSocket ssSocket;
    Socket sSocket;

    public BufferedReader in;
    public PrintWriter out;

    public ChatServer()
    {
    try
    {
    ssSocket = new ServerSocket (4000);
    }
    catch(IOException e)
    {
    System.out.println (e);
    }
    }

    public void run()
    {
    try
    {
    sSocket = ssSocket.accept();
    if(sSocket != null)
    {
    in = new BufferedReader(new InputStreamReader(sSocket.getInputStream()));
    out = new PrintWriter(sSocket.getOutputStream(), true);
    String str = in.readLine();
    System.out.println (str);
    }
    }
    catch(IOException e)
    {
    System.out.println (e);
    }
    }

    public static void main (String []args)
    {
    Thread th = (Thread) new ChatServer();
    th.start();
    }
    }
    ///////////////////////// Client //////////////////////
    public class Client extends Applet implements ActionListener
    {
    public Socket sClient;
    public BufferedReader in;
    public PrintWriter out ;
    String str;

    private TextField txt = new TextField();
    private Button btn = new Button ("OK");

    public void init()
    {
    try
    {
    sClient = new Socket ("localhost" , 4000);
    in = new BufferedReader(new InputStreamReader(sClient.getInputStream()));
    out = new PrintWriter(sClient.getOutputStream(), true);
    }
    catch (UnknownHostException uhe)
    {
    }
    catch (IOException ioe)
    {
    System.out.println (" I/O Exception : " + ioe);
    }

    setLayout(new FlowLayout());
    txt.setColumns (20);
    add(txt);
    add(btn);
    btn.addActionListener(this);
    }

    public void actionPerformed(ActionEvent AE)
    {
    if(AE.getSource() == btn)
    {
    str = txt.getText();
    out.println(str);
    }
    }
    }


    now u should try it and improve it according to ur need...

    Ahmad Jamal.
  • 843807
    843807 Member Posts: 46,582
    John.

    This and another more complex tasks are "extremely" easy to do using SwingML.

    More info at

    http://swingml.sourceforge.net
This discussion has been closed.