Solved

Problem sending a value from a server to a client program (very basic)

Posted on 2004-04-13
4
235 Views
Last Modified: 2011-09-20
I'm trying to send a String from a server program to a client program using writeUTF().

Follow the code below.
It connects to the server but wont read the message for some reason!  It gets to the "here 1" stage and goes no further.

Anyone got any ideas?
Thanks.

Heres my code:

*************************SERVER******************************
import java.net.*;
import java.io.*;
import java.util.*;

public class TestServer
{
      public static void main (String args[]) throws IOException
      {            
            ServerSocket server = new ServerSocket (9835);
            DataOutputStream out;
            Socket client = null;
            
            while (true)
            {
                  client = server.accept ();

                  //send to Client
                  out = new DataOutputStream (new BufferedOutputStream (client.getOutputStream ()));
                  out.writeUTF ("hi");
            }
      }
}

*******************************************************************

*************************CLIENT*************************************
import java.net.*;
import java.io.*;
import java.util.*;

class Test
{
      public static void main(String[] args)
      {
            DataInputStream i;

            try
            {
                  Socket s = new Socket("localhost", 9835);      
                  i = new DataInputStream (new BufferedInputStream (s.getInputStream()));  //create DataInputStream
                  System.out.println("here 1");

                  String number = i.readUTF ();
                  System.out.println("here 2");
                  System.out.println(number);
            } catch (Exception e) {
                  System.out.println(e); }
      }
}
*******************************************************************
0
Comment
Question by:credit
[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
4 Comments
 
LVL 7

Accepted Solution

by:
searlas earned 125 total points
ID: 10815668
You have a BufferedOutputStream on your server.  It may not have written the string before you are trying to read it, and so hangs.
Try this:
while (true)
          {
               client = server.accept ();

               //send to Client
               out = new DataOutputStream (new BufferedOutputStream (client.getOutputStream ()));
               out.writeUTF ("hi");
               out.flush();
               out.close();
          }
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10815693
You always have to close() input and output streams when you're done with them, especially when it comes to network programming!
0
 
LVL 7

Expert Comment

by:searlas
ID: 10821552
Yes.  And you should always flush output streams when you have finished sending a chunk of data (i.e. if you send some data, do intensive calculation, send more data... etc.)
0
 

Author Comment

by:credit
ID: 10822135
Got it working.
Thanks alot!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Title # Comments Views Activity
jmss example java 2 48
ejb entity bean example issue 2 43
jmeter usage 4 67
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 34
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

751 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