您的位置 首页 趣闻

Java实现网络监听

,Java实现网络监听

// tcpServer.java by fpont 3/2000
.
// default port is 1500.
// connection to be closed by client.
// this server handles only 1 connection. import java.io.*; ServerSocket server_socket;
BufferedReader input;
try {
port = Integer.parseInt(args[0]);
}
catch (Exception e) {
System.out.println("port = 1500 (default)");
port = 1500;
} 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);
}
try {
socket.close();
System.out.println("Connection closed by client");
}
catch (IOException e) {
System.out.println(e);
}
}
}
catch (IOException e) {
System.out.println(e);
}
}
}
// default port is 1500 import java.io.*; String server = "localhost";
Socket socket = null;
String lineToBeSent;
BufferedReader input;
PrintWriter output;
int ERROR = 1;
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;
}
} 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);
} input = new BufferedReader(new InputStreamReader(System.in));
output = new PrintWriter(socket.getOutputStream(),true);
while(true) {
lineToBeSent = input.readLine();
// stop if input line is "."
if(lineToBeSent.equals(".")) break;
output.println(lineToBeSent);
}
}
catch (IOException e) {
System.out.println(e);
} socket.close();
}
catch (IOException e) {
System.out.println(e);
}
}
}

免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。

作者: dawei

【声明】:第七手机网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

敢为天下后,OPPO Find N折叠屏的底气何在?

聊起OPPO,给我的第一印象就是本分,而在刚刚发布的OPPO Find N折叠屏手机上,我看到的是OPPO敢为天下后的底气。 当大家还沉浸在前一天发布的马里亚纳MariSilicon X影像NPU芯片时,OPPO又在12月15日推出了旗下首款折叠屏手机产品OPPO Find N。这是一款传闻已

OPPO Find N展开一刻,两大创新技术打破折痕路上意难平

荣耀发布了荣耀60系列的新品,特别是荣耀60 Pro的5000万像素超感知AI前置镜头和1亿像素超清后置主摄,是生活和游玩记录的好帮手。多镜录像功能在荣耀50系列上就已开始引入,在荣耀60系列上还新增了AI手势识别,Vlog隔空换镜这一创新功能,对拍摄Vlog来说相当

深扒天玑9000,发冲高关键年产品底气怎样?

联发科的天玑9000自公布以来,以过硬的技术实力迅速冲上了包括微博热搜在内的各类话题平台,吹响了冲击旗舰的号角。手机芯片进入4nm时代,arm推出面向未来十年的v9架构,5G技术R16即将商用,内存技术在升级面对全产业大跃进之势,联发科的天玑也迎来了上升的

想拍更远?汇总今年搭载潜望式长焦摄像头的手机

部分手机摄影发烧友已不再满足普通的2x长焦摄像头,他们需要拍的更远,画质也不能妥协,于是能拍更远的潜望式长焦摄像头就应运而生了。不过当镜头焦距上去了,后置镜头模组的厚度就难以控制,如果强行塞下更长焦段的镜头,模组厚度将达到1~2cm,这样的手机估

像素并非越高越好,为何5000万像素手机镜头倍受青欢迎

500万、800万、1200万像素,智能手机摄像头像素在早年发展的步伐相对还是比较缓慢,近来2000万、4800万、6400万、1.08亿像素接踵而来,手机摄像头的像素规格呈现出十分迅猛的迭代更新速度。然而在2019年首推量产1.08亿像素的智能手机后,至今手机上单颗传感

返回顶部