First... remember that you are using a
BufferedWriter object, which maintains an internal buffer for which you need to use it's
flush() method to send anything to the other side. Essentially, the BufferedWriter allows you to construct a significant message, with many lines that may be constructed in all sorts of fantastic programmatical ways, it literally could be data for anything, so the control is mostly given to the coder rather than the library. As such, you need to actually tell it to send it, not just expect to do so whenever it chooses. What you're getting trapped on is the readLine() method of a BufferedReader, which is having the client waiting for some kind of response from the server, and the server isn't furnishing one. These are the changes I made:
//SERVER
import java.io.*;
import java.net.*;
class ServerTest
{
public static void main(String args[]) throws IOException
{
String ostr = "Hi this is the server!";
String istr;
BufferedReader br;
BufferedWriter bw;
ServerSocket ss = new ServerSocket(9999);
System.out.println("Waiting for connection...");
Socket s = ss.accept();
System.out.println("Connection accepted!");
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write(ostr);
bw.newLine();
bw.flush();
System.out.println(br.readLine());
br.close();
bw.close();
s.close();
}
}
//CLIENT
import java.io.*;
import java.net.*;
class ClientTest
{
public static void main(String args[]) throws IOException
{
String ostr = "Hi this is the client!";
String istr;
BufferedReader br;
BufferedWriter bw;
Socket s = new Socket("127.0.0.1", 9999);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
System.out.println(br.readLine());
bw.write(ostr);
bw.newLine();
bw.flush();
br.close();
bw.close();
s.close();
}
}
I eliminated the loop on both of them (it wasn't necessary for the demonstration, but don't do it in a real program, networking can be concurrency hell), and for both of them I added the snippet
bw.newLine();
bw.flush();
The newLine is because of the readLine() method, which requires that a line end with either \n or \r, so a newLine character is necessary in the writing stream, and the newLine method happens to choose the appropriate one for the platform. The bw.flush() method is used to send the data to the other side of the connection, which is then read by readLine and interpreted properly. If you want to maintain a loop in the client that detects if br.readLine() is null, you'll have to close the connection manually from one of the sides within the loop.
The second thing, yes, people are active on this forum. It happens to be, however, that Java isn't a terribly popular language here. There are some people who use it, and a couple who are actually pretty good at it, but you'll find a lot more people here use C or C++. The Java forums may take you several days to get a desirable answer, IE one that actually helps you solve your problem, but people who know how to use Java will eventually show up and give you a hand. I know it can be difficult to be patient, especially if it's an assignment for school, but most people have jobs, family, friends, and otherwise are tangled up in their own lives. Answering questions on here is something we do
after other stuff. :)