• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please Help, Server/Client Code

 
Roan Kaas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I've copied this script from a book but i can't seem to get it to work :'(
I've added debug printouts just to see where the program goes. The server will connect to the client and the server SEEMS to send data but my client either doesn't get it or i'm coding it wronge. Please help

[Server Code]
import java.io.*;
import java.net.*;

public class Main_Server
{
String advice = "Welcome";

public void go()
{
System.out.println("Server Started");
try{

ServerSocket serverSocket = new ServerSocket(5000);

while(true)
{
Socket sock = serverSocket.accept();

PrintWriter writer = new PrintWriter(sock.getOutputStream());
writer.println("Hello");
System.out.println("Data Sent");
}
}
catch(IOException ex)
{
ex.printStackTrace();
}
}


public static void main(String[] args)
{
System.out.println("Starting Server");
Main_Server server = new Main_Server();
server.go();
System.out.println("Server Terminated");
}
}


[Client Code]
import java.io.*;
import java.net.*;

public class Client {

/** Creates a new instance of Main */
public void go() {
System.out.println("Waitng for data...");
try{
Socket s = new Socket("127.0.0.1",5000);
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
System.out.println("Got Data:");
String advice = reader.readLine();
System.out.println(":" + advice + ":");
reader.close();

}
catch(IOException ex)
{
System.out.println("An Error Has Occured! Please report the following to diablodemorte@hotmail.com:");
ex.printStackTrace();


}
//}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Running...");
System.out.println("Attempting to connect the Master Server of holy awsomeness");
Client client = new Client();
client.go();
System.out.println("Terminated");
}

}





Regards,
Roan


P.S> Thanx in advance
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you construct a PrintWriter from an OutputStream, the PrintWriter uses a BufferedWriter to make the connection. Therefore the data you print will be saved up in a buffer until some amount has accumulated before it will actually be sent. You're only sending one line to a particular client, and that's not enough to fill the buffer, so the client won't see the data, ever.

You can either 1) use the PrintWriter constructor that lets you turn on automatic buffer-flushing:

new PrintWriter(sock.getOutputStream(), true);

or you can call

writer.flush();

right after your call to writer.println().

Finally, note that your server is not closing the Sockets it accepts -- that's a resource leak, and the server will eventually run out of file descriptors and crash.

I'm going to move this to our Sockets and Internet Protocols forum for any follow-up.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!