BaseCode之socket工具类:SocketHelper.java
来源:互联网 发布:时时彩后三计划软件 编辑:程序博客网 时间:2024/05/28 11:48
import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.net.Socket;public class SocketHelper { private String ip = null; private int port = 0; private Socket socket = null; private DataOutputStream outputStream = null; private DataInputStream inputStream = null; public SocketHelper(String ip, int port) { this.ip = ip; this.port = port; } public void createConnection() throws Exception { try { socket = new Socket(ip, port); socket.setKeepAlive(false); } finally { if (socket != null) { socket.close(); } } } public synchronized void sendMessage(String sendMessage) throws Exception { try { if (outputStream == null) { outputStream = new DataOutputStream(socket.getOutputStream()); } byte b[] = sendMessage.getBytes("GBK"); outputStream.write(b); outputStream.flush(); } finally { if (outputStream != null) { outputStream.close(); } } } public synchronized String getMessage() throws Exception { return getMessage(-1); } public synchronized String getMessage(int readSize) throws Exception { try { if (inputStream == null) { inputStream = new DataInputStream( new BufferedInputStream( socket.getInputStream())); } ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); if (inputStream != null) { if (readSize != -1 && readSize > 0) { for (int i = 0; i < readSize; i++) { int read = inputStream.read(); if (read == -1) { break; } else { byteArrayOutputStream.write(read); } } } else { while (true) { int read = inputStream.read(); if (read <= 0) { break; } else { byteArrayOutputStream.write(read); } } } return new String(byteArrayOutputStream.toByteArray(), "GBK"); } } finally { if (inputStream != null) { inputStream.close(); } } return null; } public void shutDownConnection() { try { if (outputStream != null) { outputStream.close(); } if (inputStream != null) { inputStream.close(); } if (socket != null) { socket.shutdownInput(); socket.shutdownOutput(); socket.close(); } } catch (Exception e) { } } public static void main(String[] args) throws Exception { String ip = "127.0.0.1"; int port = 8080; String send = "待发送的信息"; SocketHelper socketHelper = null; String recvMsg = null; try{ socketHelper = new SocketHelper(ip, port); socketHelper.createConnection(); socketHelper.sendMessage(send); recvMsg = socketHelper.getMessage(); } finally { socketHelper.shutDownConnection(); } System.out.println("服务器返回信息:" + recvMsg); }}
阅读全文
0 0
- BaseCode之socket工具类:SocketHelper.java
- BaseCode之日期工具类:DateUtil.java
- BaseCode之文件工具类:FileUtil.java
- BaseCode之XML工具类:XMLUtil.java
- BaseCode之对象工具类:BeanUtil.java
- BaseCode之基于jdk实现的Base64加解密:Base64.java
- java.net 包 Socket通信 工具类
- android socket工具类
- socket通讯工具类
- java同步工具类之--
- Java工具类之StringBuffer
- Java工具类之SimpleDateFormat
- Java工具类之DataUtils
- java工具类之BigDecimal
- Java之ExceptionHelper工具类
- JAVA之StringUtils工具类
- java工具类之NumberUtils
- JAVA工具类之---------StringUtils
- centos7 ssh无密登录
- Apache Kafka 入门
- a+b
- 2012年蓝桥杯之奇怪的比赛
- 第三节、配置文件:入口和出口
- BaseCode之socket工具类:SocketHelper.java
- ngrok起步篇
- Struts2.5使用感叹号实现动态方法调用无效的解决办法
- codeforces 868B Race Against Time
- 泛型类中的通配符以及反射与 泛型
- Angular4-在线竞拍应用-数据绑定
- 使用LinkedHashSet对ArrayList去重并保持原顺序
- 首页
- 给定一个字符串str,返回str的统计字符串