Java实现网络监听


import java.net.*;
import java.io.*; public class tcpServer { public static void main(String args[]) { int port;
ServerSocket server_socket;
BufferedReader input;
try {
port = Integer.parseInt(args[0]);
}
catch (Exception e) {
System.out.println("port = 1500 (default)");
port = 1500;
} try {
server_socket = new ServerSocket(port);
System.out.println("Server waiting for client on port " +
server_socket.getLocalPort());
// server infinite loop
while(true) {
Socket socket = server_socket.accept();
System.out.println("New connection accepted " +
socket.getInetAddress() +
":" + socket.getPort());
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// print received data
try {
while(true) {
String message = input.readLine();
if (message==null) break;
System.out.println(message);
}
}
catch (IOException e) {
System.out.println(e);
} // connection closed by client
try {
socket.close();
System.out.println("Connection closed by client");
}
catch (IOException e) {
System.out.println(e);
}
}
}
catch (IOException e) {
System.out.println(e);
}
}
} import java.net.*;
import java.io.*; public class tcpClient { public static void main(String[] args) { int port = 1500;
String server = "localhost";
Socket socket = null;
String lineToBeSent;
BufferedReader input;
PrintWriter output;
int ERROR = 1; // read arguments
if(args.length == 2) {
server = args[0];
try {
port = Integer.parseInt(args[1]);
}
catch (Exception e) {
System.out.println("server port = 1000 (default)");
port = 1500;
}
} // connect to server
try {
socket = new Socket(server, port);
System.out.println("Connected with server " +
socket.getInetAddress() +
":" + socket.getPort());
}
catch (UnknownHostException e) {
System.out.println(e);
System.exit(ERROR);
}
catch (IOException e) {
System.out.println(e);
System.exit(ERROR);
} try {
input = new BufferedReader(new InputStreamReader(System.in));
output = new PrintWriter(socket.getOutputStream(),true); // get user input and transmit it to server
while(true) {
lineToBeSent = input.readLine();
// stop if input line is "."
if(lineToBeSent.equals(".")) break;
output.println(lineToBeSent);
}
}
catch (IOException e) {
System.out.println(e);
} try {
socket.close();
}
catch (IOException e) {
System.out.println(e);
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. Lamda表达式多个字段排序问题 ThenBy、ThenByDescending
  2. 信贷业务(Ali)
  3. shell 问题解决
  4. win10 进入安全模式的方法
  5. Python学习笔记(1):列表元组结构
  6. C语言第3天标准的输入输出函数
  7. 《Java数据结构与算法》笔记-CH5-链表-4用链表实现堆栈
  8. Creating Your Own Server: The Socket API, Part 2
  9. bzoj 1432 [ZJOI2009]Function(找规律)
  10. Oracle Database does not provide any supplemental logging, which means that by default LogMiner is not usable
  11. 初识Angular2
  12. TinyXml 快速入门(三)
  13. MAC 命令行工具(Command Line Tools)安装
  14. 如何选择面向移动设备的html5开发框架
  15. iwms后台出现从客户端(ctl00$cphMain$logo="<img src="pic/logo.g...")中检测到有潜在危险的 Request.Form 值。错误解决方法
  16. R语言grid包just参数如何just图形位置
  17. iview组件select无法手动设置值
  18. windows模拟linux部分功能
  19. ros主从关系
  20. maven学习笔记--maven项目创建

热门文章

  1. WinDbg调试高内存的.Net进程Dump
  2. HUST - 1010 The Minimum Length(最小循环节)
  3. 书写优雅的shell脚本(一)- if语句
  4. UVA-10600(次小生成树)
  5. python-day-10-python mysql and ORM
  6. linux以行为单位进行读写操作
  7. E20170415-ms
  8. 洛谷 - P2181 - 对角线 - 打表 - 组合数学
  9. python string类型 bytes类型 bytearray类型
  10. bzoj 2131: 免费的馅饼【dp+树状数组】