Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: [Java] Client - server, example

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [Java] Client - server, example

    Hello,


    I have found some example of client-server applications, unfortunetely not enough comments in code :/ Here is what I;d like to have:

    1.
    I'd like to have separate applicaction as a server that will keep values of two/three variables (0 or 1).
    In client app I want to recieve values of these two variables from server.


    2.
    I'd like to have separate applicaction as a server that will keep values of two/three variables (0 or 1).
    In client app I want to recieve values of these two variables from server AND send information to server to change them.


    Could someone please help me? It will be really great to get code for point 1, so I will try to do on my own second one.


    Thanks
    G


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: [Java] Client - server, example

    Sounds like a plan.
    What do you have so far? Can you show the code and describe the problems you are having with it by commenting in the code where the problem is what how you'd like to change what it does or add to it?

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [Java] Client - server, example

    I found an example for "server"

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
     
    public class Server {
    	public static void main(String[] args) throws IOException {
     
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4567);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4567.");
            System.exit(1);
        }
     
        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }
     
    }
    }

    What next? What to put in the code so server will be "sharing" information about two variables?

    Client:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
     
    public class Client { 
      public static void main(String[] args) throws IOException {
    	Socket cSocket = null;
    	PrintWriter out = null;
    	BufferedReader in = null;
     
    try {
        cSocket = new Socket("localhost", 4567);
        out = new PrintWriter(cSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
    } catch (UnknownHostException e1) {
        System.err.println("Don't know about host.");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: taranis.");
        System.exit(1);
    }

    Ok, now in client's part I'd like to get information about these two variables...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: [Java] Client - server, example

    Here's one way it could work
    The server waits for the client to connect with the ServerSocket.
    When the client connects to the server, the server gets an output stream from the socket and
    sends/writes the value of the two variables. What kind of data is in the variables? int or String or ?
    The client gets an input stream from the socket and reads the response sent by the server.

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [Java] Client - server, example

    It will be int. Any code?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: [Java] Client - server, example

    Look at the using the outputStream for the side that sends the data and the inputStream for the side that wants to read the data.

    Do a Search on this forum for Socket, getInputStream( or getOutputStream( for code examples.
    Last edited by Norm; June 27th, 2010 at 07:17 AM.

  7. #7
    Junior Member
    Join Date
    Nov 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [Java] Client - server, example

    /*may be u can try this
    the server programe belllow*/

    import java.net.*;
    import java.io.*;
    import java.util.*;
    import java.lang.Object;
    public class Server12
    {
    public static void main(String args[])
    {
    try
    {
    Server12 world=new Server12();


    }catch(IOException e){}



    }

    static final public int PortNumber =8000;
    public Server12()throws IOException
    {
    ServerSocket SS=new ServerSocket(PortNumber);
    Socket S=SS.accept();
    while(true)
    {
    System.out.println("\nWaiting....Run the Client Program");
    // Socket S=SS.accept();
    System.out.println("\nClient found and Message Shown");
    //OutputStreamWriter OSW=new OutputStreamWriter(S.getOutputStream());
    ObjectInputStream fromclient;
    fromclient =new ObjectInputStream(S.getInputStream());
    DataOutputStream toclient=new DataOutputStream(S.getOutputStream());

    int i=fromclient.readInt();
    System.out.println("\nClient send :"+i);
    int x=i*i*3;
    toclient.writeInt(x);





    String Msg="Current Date And Time on Server is:"+new Date()+"\n";
    //OSW.write(Msg);
    // OSW.close();

    }

    }}


    //the client programe is


    import java.net.*;
    import java.io.*;
    import java.util.*;
    public class Client12
    {
    public static void main(String args[])
    {
    try
    {
    Client12 world=new Client12();


    }catch(IOException e){}



    }

    static final public int PortNumber =8000;
    public Client12()throws IOException
    {
    Scanner as=new Scanner(System.in);
    Socket S=new Socket("127.0.0.1",PortNumber);
    DataInputStream fromserver=new DataInputStream(S.getInputStream());
    //BufferedReader BR=new BufferedReader(R);
    DataOutputStream toserver=new DataOutputStream(S.getOutputStream());
    //int rad=0;
    for(
    {
    System.out.print("enter=");
    int rad=as.nextInt();
    System.out.print("you have entered=="+rad);
    toserver.writeInt(rad);
    int r=fromserver.readInt();

    System.out.print("you have entered=="+r);
    // fromserver.flash();

    System.out.println("\nreply From Server:\n"+r);
    }



    }}


    /*
    this is to sending a int value from client and the server inresponse gives a calculation that is int x.then the client reads the int receved from server .the receved integer is int r in the client programe and then print it.u then change it as ur needs.*/

Similar Threads

  1. Client/Server
    By Dillz in forum Paid Java Projects
    Replies: 2
    Last Post: June 2nd, 2010, 05:19 AM
  2. Client-Server program
    By Reztem in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 12th, 2010, 05:36 PM
  3. TCP Client Server: Object Oriented
    By nffc luke in forum Object Oriented Programming
    Replies: 2
    Last Post: April 28th, 2010, 06:39 PM
  4. [SOLVED] The concept of Server and Client
    By rendy.dev in forum Java Theory & Questions
    Replies: 3
    Last Post: January 18th, 2010, 03:13 AM
  5. Download a file to client from server
    By mvittalreddy in forum Java Networking
    Replies: 4
    Last Post: October 5th, 2009, 04:23 AM