Solved

data exchange betweem client server using sockets

Posted on 2004-10-04
3
247 Views
Last Modified: 2010-03-31
How To integrate random number generater in the server code and print those when requested by client.
Here are my client and server codes.When client requests getdata..the server should send 8 random numbers and client should be able to see that and display them.can anybody integrate the random number generator in my server code and make it work.

Server code::
---------------------------------------
import java.net.*;
import java.io.*;

public class Server
{
      public Server()
      {
            try
            {
                  //creating server socket binding at port # 3000
                  ServerSocket server=new ServerSocket(3000);
                  System.out.println("Server binded at "+((server.getInetAddress()).getLocalHost()).getHostAddress()+":3000");
                  System.out.println("Run the Client");
                  //ready to accept client request
                  Socket socket=server.accept();
                  //opening the input stream to read data from client connection
                  BufferedReader in= new BufferedReader(new InputStreamReader(socket.getInputStream()));
                  System.out.println(in.readLine());
                  //using output stream responsing data
                  PrintStream out=new PrintStream(socket.getOutputStream());
                  out.print("Welcome by server\n");
                  out.flush();
                  //closing the in & out streams
                  out.close();
                  in.close();
            }
            catch(Exception err)
            {
                  System.err.println("* err"+err);
            }
                  
      }
      
      public static void main(String a[])
      {
            new Server();
      }
      
}

client code
------------------------------------
import java.net.*;
import java.io.*;

public class Client
{
      
      public Client()
      {
            try
            {
                  //ceating the socket to connect to server running on same machine binded on port no 3000
                  Socket client=new Socket("localhost",3000);
                  System.out.println("Client connected ");
                  //getting the o/p stream of that connection
                  PrintStream out=new PrintStream(client.getOutputStream());
                  //sending the message to server
                  out.print("Hello from client\n");
                  out.flush();
                  //reading the response using input stream
                  BufferedReader in= new BufferedReader(new InputStreamReader(client.getInputStream()));
                  System.out.println(in.readLine());
                  //closing the streams
                  in.close();
                  out.close();
 
            }
            catch(Exception err)
            {
                  System.err.println("* err"+err);
            }
                  
      }
      
      public static void main(String a[])
      {
            new Client();
      }
      
}
0
Comment
Question by:prashanth_gurijala
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12222828
Use a DataOutputStrea/SDataInputStream to pass the random numbers.
something like:

import java.net.*;
import java.io.*;

public class Server
{
     public Server()
     {
          try
          {
               //creating server socket binding at port # 3000
               ServerSocket server=new ServerSocket(3000);
               System.out.println("Server binded at "+((server.getInetAddress()).getLocalHost()).getHostAddress()+":3000");
               System.out.println("Run the Client");
               //ready to accept client request
               Socket socket=server.accept();
               //opening the input stream to read data from client connection
               BufferedReader in= new BufferedReader(new InputStreamReader(socket.getInputStream()));
               System.out.println(in.readLine());
               //using output stream responsing data
               DataOutputStream out=new DataOutputStream(socket.getOutputStream());
               for (int i=0; i<8; i++)
               {
                       out.writeInt(Math.random()*10);
               }
               out.flush();
               //closing the in & out streams
               out.close();
               in.close();
          }
          catch(Exception err)
          {
               System.err.println("* err"+err);
          }
               
     }
     
     public static void main(String a[])
     {
          new Server();
     }
     
}

client code
------------------------------------
import java.net.*;
import java.io.*;

public class Client
{
     
     public Client()
     {
          try
          {
               //ceating the socket to connect to server running on same machine binded on port no 3000
               Socket client=new Socket("localhost",3000);
               System.out.println("Client connected ");
               //getting the o/p stream of that connection
               PrintStream out=new PrintStream(client.getOutputStream());
               //sending the message to server
               out.print("Hello from client\n");
               out.flush();
               //reading the response using input stream
               DataInputStream in= new DataInputStream(client.getInputStream());
               for (int i=0; i<8; i++)
               {
                  System.out.println(in.readInt());
               }
               //closing the streams
               in.close();
               out.close();
 
          }
          catch(Exception err)
          {
               System.err.println("* err"+err);
          }
               
     }
     
     public static void main(String a[])
     {
          new Client();
     }
     
}
0
 

Author Comment

by:prashanth_gurijala
ID: 12223121
Here when generating the random number its of double type   [out.writeInt(Math.random()*10);] it say that out can't accept double type values
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12223128
sorry, yes you'll need to cast it to an int:

out.writeInt((int)(Math.random()*10));
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now