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
Solved

data exchange betweem client server using sockets

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

840 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