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发送一句话文字信息(你能够拓展其他的了) 先看一下服务端程序的实现 ...
随机推荐
- Java反编译代码分析(一)
浅析如何读懂这种反编译过来的文件,不喜勿喷. 赋值 Node node; Node node1 = _$3.getChildNodes().item(0); node1; node1; JVM INS ...
- 建模角度理解word embedding及tensorflow实现
http://www.jianshu.com/p/d44ce1e3ec2f 1. 前言 本篇主要介绍关键词的向量表示,也就是大家熟悉的word embedding.自Google 2013 年开源wo ...
- SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间。
出现的错误:SqlDateTime 溢出.必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间. 错误的原因:.NET Framework dat ...
- windows 环境变量
1.考虑下面的需求,进入cmd之后,我就想执行mysql客户端命令,而这需要转到mysql安装目录,找到mysql可执行文件,在这个目录下执行mysql命令.这样太麻烦,有没有好的解决办法? 2.使用 ...
- Intellij IDEA连接Spark集群
1. 首先安装Scala插件,File->Settings->Plugins,搜索出Scla插件,点击Install安装: 2. File->New Project->mave ...
- Python学习笔记_01:基本概念介绍
目录 1.Python语言简介 2.Python中常用数据结构 2.1什么是列表? 2.2什么是元组? 2.3什么是字典? 2.4索引及分片 3.其它基本概念 3.1数据类型和变量 3.2生成器 3. ...
- 算法笔记_226:填符号凑算式(Java)
目录 1 问题描述 2 解决方案 1 问题描述 匪警请拨110,即使手机欠费也可拨通! 为了保障社会秩序,保护人民群众生命财产安全,警察叔叔需要与罪犯斗智斗勇,因而需要经常性地进行体力训练和智力训 ...
- WireShark开启IP, TCP,UDP校验和的办法
首先点击编辑->首选项
- 排序基础之归并排序、快排、堆排序、希尔排序思路讲解与Java代码实现
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6594855.html 一:归并排序==通过中间值进行左右划分递归,然后调用合并函数对左右递归的结果进行合并( ...
- merge-intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example,Given[1,3],[2,6],[8,10 ...