Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6766815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:52:35+00:00 2026-05-26T14:52:35+00:00

I’ve just begun socket programming, and I’m working on an Echo server in Java.

  • 0

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();
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:52:35+00:00Added an answer on May 26, 2026 at 2:52 pm

    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?");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
I have thousands of HTML files to process using Groovy/Java and I need to
I am writing an app with both english and french support. The app requests
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.