Solved

data exchange betweem client server using sockets

Posted on 2004-10-04
3
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

730 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