Solved

data exchange betweem client server using sockets

Posted on 2004-10-04
3
245 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
Comment Utility
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
Comment Utility
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
Comment Utility
sorry, yes you'll need to cast it to an int:

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

7 Experts available now in Live!

Get 1:1 Help Now