MrGone06095
asked on
java read vs readline
First let me admit that I barely know java enough to be dangerous.
I'd like to modify the code below (in the function run) so that when it receives a message, it prints it to the console, but I don't want it to print a CR LF.
I get some compiler warnings about depreciated functions, so fee free to update the code where it makes sense to do so:
An added bonus would be to make the program exit gracefully if the user hits ctrl-c
I'd like to modify the code below (in the function run) so that when it receives a message, it prints it to the console, but I don't want it to print a CR LF.
I get some compiler warnings about depreciated functions, so fee free to update the code where it makes sense to do so:
An added bonus would be to make the program exit gracefully if the user hits ctrl-c
import java.io.*;
import java.net.*;
import java.security.*;
/**
* Title: Sample Server
* Description: This utility will accept input from a socket, posting back to the socket before closing the link.
* It is intended as a template for coders to base servers on. Please report bugs to brad at kieser.net
* Copyright: Copyright (c) 2002
* Company: Kieser.net
* @author B. Kieser
* @version 1.0
*/
public class sample_server {
private static int port=12310, maxConnections=0;
// Listen for incoming connections and handle them
public static void main(String[] args) {
int i=0;
try{
ServerSocket listener = new ServerSocket(port);
Socket server;
while((i++ < maxConnections) || (maxConnections == 0)){
doComms connection;
server = listener.accept();
doComms conn_c= new doComms(server);
Thread t = new Thread(conn_c);
t.start();
}
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
}
class doComms implements Runnable {
private Socket server;
private String line,input;
doComms(Socket server) {
this.server=server;
}
public void run () {
input="";
try {
// Get input from the client
DataInputStream in = new DataInputStream (server.getInputStream());
PrintStream out = new PrintStream(server.getOutputStream());
while((line = in.readLine()) != null) {
input=input + line;
}
// Now write to the client
System.out.print("> " + input);
server.close();
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.