I've just begun socket programming, and I'm working on an Echo server in Java. One of the things I'd like to do is implement the server in both TCP and UDP and allow the client to choose which protocol to use at runtime.
This is a noob question, but how do I allow a user this option to choose TCP or UDP protocols? I tried putting in an if-else at the beginning which branched on the protocol choice from scanner input, but that just skips the both blocks irrespective of the choice?
Thanks.
I've implemented the TCP echo server:
public class EchoServer
{
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
try{
serverSocket = new ServerSocket(10007);
}
catch (IOException e)
{
System.err.println("Could not listen on port: 10007.");
System.exit(1);
}
Socket clientSocket = null;
System.out.println ("Waiting for connection.....");
try {
clientSocket = serverSocket.accept();
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}
System.out.println ("Connection successful");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader( clientSocket.getInputStream()));
String cAddress = "";
String inputLine;
cAddress = clientSocket.getInetAddress().toString();
while ((inputLine = in.readLine()) != null)
{
System.out.println ("Server: " + inputLine + " " + cAddress + " ");
out.println(inputLine + " " + cAddress);
if (inputLine.equals("bye"))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
And the client side:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class EchoClient {
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(System.in);
String serverHostname;
System.out.println("Enter an IP value: ");
serverHostname = s.next();
//String serverHostname = new String ("127.0.0.1");
if (args.length > 0)
serverHostname = args[0];
System.out.println ("Attemping to connect to host " +
serverHostname + " on port 10007.");
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket(serverHostname, 10007);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverHostname);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: " + serverHostname);
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
System.out.print ("input: ");
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
System.out.print ("input: ");
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
There is no way (that I know of) to do conditionals in Java without using the If statement. If your else-if wasn't working, then you were checking the condition wrong. To compare strings in java you use String.equals( String ), you can't use ==.
Try using it again on standard input, but just do something simple. Like:
Scanner scan = new Scanner( System.in );
System.out.println( "use tcp?" );
String in = scan.nextLine();
if( in.indexOf( "y" ) >= 0 || in.indexOf( "Y" ) >= 0 ){
System.out.println("using tcp");
}else{
System.out.println("not using tcp, use udp instead?");
}