/*******************************************************************************
* @(#)ServerIo.java 2012-8-7
*
* Copyright 2012 Neusoft Group Ltd. All rights reserved.
* Neusoft PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*******************************************************************************/
package com.neusoft.demo.io;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.neusoft.demo.utils.Constants;
import com.neusoft.transfer.Converser;
/**
* <pre>
* 采用JAVA IO的简单通信服务器
*
* 服务器端采用线程池,为每个客户端分配线程处理
* </pre>
* @author <a href="mailto:hongchq@neusoft.com">ChengQian.Hong </a>
* @version $Revision 1.1 $ 2012-8-7 上午9:05:40
*/
public class ServerIo {
private ServerSocket serverSocket;
private ExecutorService executorService;
/**
* 启动服务器
* @throws IOException
*/
private void startServer() throws IOException {
serverSocket = new ServerSocket(Constants.IO_SERVER_PORT);
executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()
* Constants.THEAD_POOL_SIZE);
System.out.println("start io_server success!");
}
/**
* 接收数据,返回响应
*/
private void service() {
while (true) {
Socket socket = null;
try {
socket = serverSocket.accept();
executorService.execute(new Handler(socket));
} catch (Throwable t) {
t.printStackTrace();
}
}
}
class Handler implements Runnable {
Socket socket;
public Handler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
System.out.println("new connection accepeted:" + socket.getInetAddress() + ":"
+ socket.getPort());
System.out.println("current thread:" + Thread.currentThread().getName());
InputStream br;
try {
br = getReader(socket);
OutputStream pw = getWriter(socket);
byte[] msg = new byte[10];
while (br.read(msg) != -1) {
byte[] response = getResponse(msg);
int commandId = getCommandId(msg);
if (commandId != 4) {
pw.write(response);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private int getCommandId(byte[] msg) {
byte[] commandId = new byte[4];
System.arraycopy(msg, 4, commandId, 0, 4);
int commandIdValue = Converser.bytes2Int(commandId);
return commandIdValue;
}
/**
* 获得响应
*/
private byte[] getResponse(byte[] msg) {
byte[] commandId = new byte[4];
System.arraycopy(msg, 4, commandId, 0, 4);
int commandIdValue = Converser.bytes2Int(commandId);
byte[] result = new byte[10];
switch (commandIdValue) {
case 0:
System.out.println("login msg");
System.arraycopy(Converser.int2Byte(10), 0, result, 0, 4);
System.arraycopy(Converser.int2Byte(1), 0, result, 4, 4);
System.arraycopy("ok".getBytes(), 0, result, 8, 2);
break;
case 2:
System.out.println("service msg");
System.arraycopy(Converser.int2Byte(10), 0, result, 0, 4);
System.arraycopy(Converser.int2Byte(3), 0, result, 4, 4);
System.arraycopy("ok".getBytes(), 0, result, 8, 2);
break;
case 4:
System.out.println("bye msg");
break;
default:
break;
}
return result;
}
/**
* 获取socket输出流
* @param socket
* @return
* @throws IOException
*/
private OutputStream getWriter(Socket socket) throws IOException {
OutputStream outputStream = socket.getOutputStream();
return outputStream;
}
/**
* 获取socket输入流
* @param socket
* @return
* @throws IOException
*/
private InputStream getReader(Socket socket) throws IOException {
InputStream inputStream = socket.getInputStream();
return inputStream;
}
/**
* @param args
*/
public static void main(String[] args) {
ServerIo serverIo = new ServerIo();
try {
serverIo.startServer();
serverIo.service();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用IO方式的简单TCP通信
需积分: 13 70 浏览量
2013-06-09
16:21:58
上传
评论
收藏 3KB RAR 举报
战歌IT
- 粉丝: 122
- 资源: 2409
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈