• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Echo Server

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am creating an Echo Server program that accepts the server name &port number from the command line, but am unsure of how to get the info from the command line into my program. I use a BufferedReader to collect the data from the command line, but how do I use the info stored in the BufferedReader in my program. See ****** below:

Here is my code:
import java.io.*;
import java.net.*;

public class EchoServer
{
public static void main(String args[])
{
//Declare Server Socket
ServerSocket aServerSocket = null;

//Declare Client Socket
Socket aClientSocket = null;

//Declare string used to send messages back & forth
String sline;

System.out.println("Enter server address & port number:");
//READ FROM COMMAND LINE;
***********************************************
BufferedReader br = new BufferedReader(new InputStreamReader System.in));
//FileReader fr = new FileReader(socketname);
//br = new BufferedReader(fr);

try
{
**************
//Here is where I need to use the command line info
aServerSocket = new ServerSocket(info From command line);
}//TRY

catch (IOException e)
{
//System.out.println(e);
System.out.println("Connection Failed");
}//CATCH

try
{
aClientSocket = aServerSocket.accept();
}//TRY

catch(IOException e)
{
System.out.println("Connection Failed");
}//CATCH

try
{
PrintWriter pw = new PrintWriter(aClientSocket.getOutputStream());
BufferedReader br2 = new BufferedReader (new InputStreamReader(aClientSocket.getInputStream()));
//dis = new DataInputStream(aClientSocket.getInputStream());
//ps = new PrintStream(aClientSocket.getOutputStream());

while(true)
{
sline = br.readLine();
pw.println(sline);
}//WHILE
}//TRY

catch (IOException e)
{
System.out.println(e);
}//CATCH

}//PUBLIC STATIC VOID MAIN
}//ECHO SERVER

[ January 18, 2005: Message edited by: Alex Green ]
[ January 18, 2005: Message edited by: Alex Green ]
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is not a JDBC question ...


try something like:


pascal
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to IO's and Streams
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic