Java socket 多线程编程 示例
2024-10-15 13:39:54
参照网上代码:
1.工程:
2.代码:
Client.java
package com.my.socket.test; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket; public class Client { public static final String CHARCODE = "utf-8"; public static void main(String[] args) { Socket socket = null;
int port = 8821; OutputStream socketOut = null;
BufferedReader br = null;
try {
socket = new Socket("localhost", port);
// 发送消息
String msg = "wwwwwwwwwwww哈哈w1241243123";
// base64 编码,防止中文乱码
msg = Util.encode(msg.getBytes(CHARCODE));
System.out.println("c1:" + msg);
System.out.println("c2:" + Util.decode(msg,CHARCODE)); msg = msg + "\r\n";
socketOut = socket.getOutputStream();
socketOut.write(msg.getBytes(CHARCODE));
socketOut.flush(); // 接收服务器的反馈
br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String res = br.readLine();
if (res != null) {
res = Util.decode(res,CHARCODE);
System.out.println("c3:" + res);
} } catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (socketOut != null) {
try {
socketOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
}
MultiThreadServer.java
package com.my.socket.test; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class MultiThreadServer {
private int port = 8821;
private ServerSocket serverSocket;
private ExecutorService executorService;
private final int POOL_SIZE = 10; public MultiThreadServer() throws IOException {
serverSocket = new ServerSocket(port);
executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors() * POOL_SIZE);
System.out.println("服务已启动");
} public void service() {
while (true) {
Socket socket = null;
try {
socket = serverSocket.accept();
executorService.execute(new Handler(socket)); } catch (Exception e) {
e.printStackTrace();
}
}
} public static void main(String[] args) throws IOException {
new MultiThreadServer().service();
} } class Handler implements Runnable { public static final String CHARCODE = "utf-8"; private Socket socket; public Handler(Socket socket) {
this.socket = socket;
} private PrintWriter getWriter(Socket socket) throws IOException {
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut, true);
} private BufferedReader getReader(Socket socket) throws IOException {
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
} public void run() {
BufferedReader br = null;
PrintWriter out = null;
try {
br = getReader(socket); out = getWriter(socket);
String msg = null;
while ((msg = br.readLine()) != null) {
System.out.println("s1:" + msg);
msg = Util.decode(msg,CHARCODE);
System.out.println("s2:" + msg); String res = "wwwwwwwwwwww哈哈w1241243123";
res = Util.encode(res.getBytes(CHARCODE));
System.out.println("s1:" + res);
System.out.println("s2:" + Util.decode(res,CHARCODE)); out.println(res);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (out != null) {
out.close();
}
}
} }
Util.java
package com.my.socket.test; import java.io.IOException; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; public class Util { public static String decode(String str, String charCode) {
try {
return new String(new BASE64Decoder().decodeBuffer(str), charCode);
} catch (IOException e) {
e.printStackTrace();
}
return null;
} public static String encode(byte[] bstr) {
return new BASE64Encoder().encode(bstr);
}
}
最新文章
- 【皇甫】☀Struts_第一节课
- 修改navigationbar右侧按钮离屏幕边缘位置
- (转)Android系统自带Activity样式(@android:style/)
- php数据库操作常用相关函数
- nagios 完全配置手册
- UVALIVE 5893 计算几何+搜索
- 安装器---Inno Setup
- 3_初学sql注入
- CocosCraetor中图像资源Texture和SpriteFrame的区别
- Mysql基础教程之mysql 设置参数常用方法
- Scrapy 原理
- sql查询count 单独字段不同值
- Android开发——Android多进程以及使用场景介绍
- Ice框架简介及Vs2013安装Ice 3.7.0步骤及实例
- Visual Studio 调试时无法命中断点
- 微软职位内部推荐-SW Engineer II for Windows System
- Centos6.x搭建lnmp环境
- ssm分页
- Problem E: 深入浅出学算法019-求n的阶乘
- 【 Linux 】为lnmp架构添加memcached支持
热门文章
- 隐藏 input 标签的边框
- EditText的hint不显示
- WinForm动态添加控件及其事件(转)
- VB.NET读取Excel工作表信息
- eclipse里面构建maven项目详解(转载)
- java实验一 20135104刘帅
- Hyper-V初涉_早期Windows安装虚拟硬件驱动
- 配置ubuntu 14.04.3 LTS odoo 9.0开发环境
- mysql(或者mariadb)连接工具HeidiSQL
- Asp.Net Web API 2第十三课——ASP.NET Web API中的JSON和XML序列化