Solved

data exchange betweem client server using sockets

Posted on 2004-10-04
3
246 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

920 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

15 Experts available now in Live!

Get 1:1 Help Now