首页 文章

Java Echo Server TCP *和* UDP实现?

提问于
浏览
0

我've just begun socket programming, and I'正在使用Java的Echo服务器 . 我想做的其中一件事是在TCP和UDP中实现服务器,并允许 client 选择在运行时使用哪个协议 .

这是一个noob问题,但如何让用户选择此选项来选择TCP或UDP协议呢?我尝试在开头添加一个if-else,它从扫描仪输入中选择协议选项,但是不管选择什么,它都会跳过这两个块?

谢谢 .

我已经实现了TCP echo服务器:

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(); 
  } 
}

而客户方:

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();
}
}

3 回答

  • 0

    我没有办法(我知道)在不使用If语句的情况下在Java中执行条件 . 如果你的其他人 - 如果没有工作,那么你检查错误的条件 . 要比较java中的字符串,你使用String.equals(String),你不能使用== .

    尝试在标准输入上再次使用它,但只是做一些简单的事情 . 喜欢:

    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

    如果您想知道套接字级别的可能性,您应该能够将TCP服务器套接字和UDP服务器套接字绑定到同一端口 . 您必须有单独的线程处理每个套接字 . 有关如何编写UDP服务器套接字的说明(称为DatagramSocket检查this tutorial .

  • 0

    您需要在应用程序中打开两个具有不同端口的独立套接字 . 除此之外,您需要在Java中使用单独的线程,这些用法在很多方法和教程中都有描述 .

    对于套接字创建,使用DatagramSocket for UDP和ServerSocket for TCP . 我不知道,你想做什么,但你必须知道你必须自己处理UDP中的dataloss . UDP常用于流式音频或视频,其中丢失一些数据并不重要 .

    验证您的应用程序需求 . 有关详细信息,请参阅http://en.wikipedia.org/wiki/User_Datagram_Protocol#Comparison_of_UDP_and_TCP .

    别客气 .

相关问题