Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Please Help, Server/Client Code

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic