java 记录一下socket的server与client
package com.test.server; import java.io.IOException;
import java.net.ServerSocket; import org.apache.log4j.Logger; public class Server{ private static final Logger logger = Logger.getLogger(Server.class); public static ServerSocket server = null; static{
try {
server = new ServerSocket(5678);
} catch (IOException e) {
logger.error("Server set up error: ", e);
}
} public static void stop(){
if(server != null){
try {
server.close();
} catch (IOException e) {
logger.error("Server close error: ", e);
}
}
} public static void main(String[] args) {
Server.stop();
} }
package com.test.server; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket; import org.apache.log4j.Logger; public class ServerThread implements Runnable { private static final Logger logger = Logger.getLogger(ServerThread.class); private Socket socket; public ServerThread(Socket socket) {
this.socket = socket;
} @Override
public void run() {
// String line;
BufferedReader is = null;
Writer writer = null;
if (socket == null) {
logger.error("Server down!");
return;
}
StringBuilder sb = new StringBuilder();
try {
is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String temp;
int index;
temp = is.readLine();
System.out.println("line is: "+ temp);
while (temp != null) {
System.out.println(temp);
if ((index = temp.indexOf("eof")) != -1) {// 遇到eof时就结束接收
sb.append(temp.substring(0, index));
break;
}
sb.append(temp);
temp = is.readLine();
}
// logger.info(sb.toString());
System.out.println(sb.toString());
writer = new OutputStreamWriter(socket.getOutputStream());
writer.write("Hello Client.");
writer.write("eof\n");
writer.flush();
} catch (IOException e) {
logger.error("Read information error: ", e);
} finally {
try {
if (is != null) {
is.close();
}
if (writer != null) {
writer.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
logger.error("Close the IO session error: ", e);
} }
} }
package com.test.server; import java.io.IOException;
import java.net.Socket; import org.apache.log4j.Logger; public class SocketServer { private static final Logger logger = Logger.getLogger(SocketServer.class); public void connect() {
try {
while (true) {
Socket socket = Server.server.accept();
// socket.getRemoteSocketAddress();
new Thread(new ServerThread(socket)).start();
}
} catch (IOException e) {
logger.error("Server set up error: ", e);
} } public static void main(String[] args) {
SocketServer ss = new SocketServer();
ss.connect();
} }
package com.test.client; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
import java.net.UnknownHostException; import org.apache.log4j.Logger; public class SocketClient { private static final Logger logger = Logger.getLogger(SocketClient.class); public void client() {
Socket client = null;
BufferedReader is = null;
Writer writer = null;
try {
client = new Socket("172.16.13.41", 5678); writer = new OutputStreamWriter(client.getOutputStream());
writer.write("Hello Server.");
writer.write("eof\n");
writer.flush(); is = new BufferedReader(new InputStreamReader(client.getInputStream()));
StringBuffer sb = new StringBuffer();
String temp;
int index;
while ((temp = is.readLine()) != null) {
if ((index = temp.indexOf("eof")) != -1) {
sb.append(temp.substring(0, index));
break;
}
sb.append(temp);
}
// logger.info(sb.toString());
System.out.println(sb.toString());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (writer != null) {
writer.close();
}
if (client != null) {
client.close();
}
} catch (IOException e) {
logger.error("Close the IO session error: ", e);
}
} } public static void main(String[] args) {
SocketClient sc = new SocketClient();
sc.client();
} }
java 记录一下socket的server与client的更多相关文章
- [001]socket通信--server和client实现迭代的简单例子
server端: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unist ...
- [000]socket通信--server和client实现的简单例子
在C语言中就学过scanf和printf,其实理解socket就跟这两个输入输出差不多,只不过是信息的传输而已. 1.TCP服务器端(server)的默认函数调用顺序: 按照上述的调用顺序,我们可以来 ...
- Python中Socket编程server与client简单的合法性认证
导入python自带的hmac模块对随机生成的位数32字节和自定义token进行加密处理 import osmsg = os.urandom(32) #随机生成msgOut[4]: b"F ...
- Java与C++Socket通讯注意事项
c++与java进行socket通信时注意事项 原文链接: http://my.oschina.net/ypimgt/blog/106439 因为java发送的都是网络字节序(big-endium), ...
- Apache Ignite 学习笔记(三): Ignite Server和Client节点介绍
在前两篇文章中,我们把Ignite集群当做一个黑盒子,用二进制包自带的脚本启动Ignite节点后,我们用不同的客户端连接上Ignite进行操作,展示了Ignite作为一个分布式内存缓存,内存数据库的基 ...
- 上机题目(0基础)- Java网络操作-Socket实现client和server端通信二(Java)
上一节实现了client像server端发送请求.本节将实现server端向client回传信息.实现原理非常easy,在原来的基础上.在server端实现输出流,在client实现输入流就可以,详细 ...
- Java设置Client Socket链接Server超时时间
Java设置Client Socket链接Server超时时间 学习了:http://blog.csdn.net/tterminator/article/details/52494141 http:/ ...
- C Socket Programming for Linux with a Server and Client Example Code
Typically two processes communicate with each other on a single system through one of the following ...
- Android简单实现Socket通信,client连接server后,server向client发送文字数据
案例实现的是简单的Socket通信,当client(Androidclient)连接到指定server以后,server向client发送一句话文字信息(你能够拓展其他的了) 先看一下服务端程序的实现 ...
随机推荐
- TensorFlow练习7: 基于RNN生成古诗词
http://blog.topspeedsnail.com/archives/10542 主题 TensorFlow RNN不像传统的神经网络-它们的输出输出是固定的,而RNN允许我们输入输出向量 ...
- 推荐系统resys小组线下活动见闻2009-08-22
http://www.tuicool.com/articles/vUvQVn 时间2009-08-30 15:13:22 不周山原文 http://www.wentrue.net/blog/?p= ...
- Flask调试
1.AttributeError: 'NoneType' object has no attribute 'app' 原因:直接在py中调用视图函数,但没有上下文,导致出错 2.不转换html代码 { ...
- ASP入门(三)-VBScript变量、运算符
ASP内置了两种语法引擎,分别是VBScript和JScript. VBScript是VB的一个子集.JScript和JavaScript有些类似. 如果你熟悉VB,建议用VBScript,否则推荐使 ...
- 【mybatis】多次查询缓存的问题
转自:http://cheng-xinwei.iteye.com/blog/2021700?utm_source=tuicool&utm_medium=referral 最近在使用mybati ...
- CentOS7安装 Apache HTTP 服务器
CentOS7安装 Apache HTTP 服务器 时间:2015-05-02 00:45来源:linux.cn 作者:linux.cn 举报 点击:11457次 不管你因为什么原因使用服务器,大部分 ...
- css背景全屏-视差
<!DOCTYPE html> <html> <head> <title></title> <style> *{margin:0 ...
- Shiro整合springboot,freemaker,redis(含权限系统完整源码)
区块链技术联盟 2018-02-08 17:06:40 目录 一.导语 二.shiro功能介绍 三.shiro详解 四.shiro实战案例分享 五.系统配置 六.其他 一.导语 今天推荐给大家一个非常 ...
- css 进度条
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- 关于0xFFFFFFFF和alpha,温故而知新
做图像处理都好多年了,今天随手做个小画板的时候,发现一个挺有趣的小坑.而其实这个小坑,以前也坑过自己,不过太久没处理了,又踩到坑里了. 先来看看:0xFFFFFFFF>>24 这个结果是什 ...