Java - Socket example: EchoClient and EchoServer
来源:互联网 发布:磁贴数据库已损坏 编辑:程序博客网 时间:2024/05/18 00:39
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package chimomo.java.learning;import java.io.*;import java.net.*;/** * * @author Chimomo */public class EchoClient { public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println("Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket echoSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) { String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine()); } } catch (UnknownHostException e) { System.err.println("Don't know about host " + hostName); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to " + hostName); System.exit(1); } }}
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package chimomo.java.learning;import java.net.*;import java.io.*;/** * * @author Chimomo */public class EchoServer { public static void main(String[] args) throws IOException { if (args.length != 1) { System.err.println("Usage: java EchoServer <port number>"); System.exit(1); } int portNumber = Integer.parseInt(args[0]); try ( ServerSocket serverSocket = new ServerSocket(Integer.parseInt(args[0])); Socket clientSocket = serverSocket.accept(); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));) { String inputLine; while ((inputLine = in.readLine()) != null) { out.println(inputLine); } } catch (IOException e) { System.out.println("Exception caught when trying to listen on port " + portNumber + " or listening for a connection"); System.out.println(e.getMessage()); } }}
1 0
- Java - Socket example: EchoClient and EchoServer
- Java NIO 实现 EchoServer EchoClient
- JAVA 实现 EchoServer EchoClient 服务器、客户机本地通信程序
- python socket and SSL example
- Java Example Codes and Tutorials
- Spring and Java Thread example
- socket example
- Java object sorting example (Comparable and Comparator)
- Java Thread wait, notify and notifyAll Example
- Java: 实例 Example for Java Date and Calendar
- Raw socket Example
- Perl Socket example
- socket select example
- inet address example(socket)
- Android Socket Example
- [linux]raw socket example
- java ServerSocket and Socket programming
- Socket between Java and Perl
- Android5.1应用统计源码分析
- adb基本命令和Logcat基本命令
- Android 学习资料收集
- 关于centos7中使用rpm方式安装mysql5.7版本后无法使用root登录的问题
- Android深入浅出之Binder机制: Reference 2#
- Java - Socket example: EchoClient and EchoServer
- codevs2178 表达式运算Cuties
- 关于Android中textView的背景设置
- SELECT查询结果排序
- Android的IPC机制Binder的各个部分 : Reference 3#
- C++ STL <map> 用法简介
- Coursera公开课笔记: 斯坦福大学机器学习第七课“正则化(Regularization)”
- 【POJ2886】Who Gets the Most Candies?-线段树+反素数
- Oracle关于高速缓存区应用原理