credit
asked on
Problem sending a value from a server to a client program (very basic)
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:
*************************S ERVER***** ********** ********** *****
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");
}
}
}
************************** ********** ********** ********** ********** *
*************************C LIENT***** ********** ********** ********** **
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); }
}
}
************************** ********** ********** ********** ********** *
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:
*************************S
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");
}
}
}
**************************
*************************C
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); }
}
}
**************************
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You always have to close() input and output streams when you're done with them, especially when it comes to network programming!
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.)
ASKER
Got it working.
Thanks alot!
Thanks alot!