夢幻之旅
DEBUG - 天道酬勤
::
首頁
::
新隨筆
::
聯(lián)系
::
聚合
::
管理
::
671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
<
2011年10月
>
日
一
二
三
四
五
六
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
公告
本博客中未注原創(chuàng)的文章均為轉(zhuǎn)載,對轉(zhuǎn)載內(nèi)容可能做了些修改和增加圖片注釋,如果侵犯了您的版權,或沒有注明原作者,請諒解
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(21)
給我留言
查看公開留言
查看私人留言
隨筆分類
(644)
Android(10)
(rss)
ANT(4)
(rss)
C#(10)
(rss)
C/C++(16)
(rss)
CSS(3)
(rss)
DataBase(119)
(rss)
DB-DailyMmaintenance(16)
(rss)
Design Patterns(27)
(rss)
english
(rss)
Exceptions(7)
(rss)
EXT(39)
(rss)
FLASH(9)
(rss)
Hardware(20)
(rss)
Hibernate(13)
(rss)
Html(23)
(rss)
Java(143)
(rss)
java Net(10)
(rss)
JavaScript(39)
(rss)
Linux(26)
(rss)
php(5)
(rss)
Regular Exp(3)
(rss)
Spring(17)
(rss)
Struts(12)
(rss)
TOOL(43)
(rss)
VB/VBA/VBS(5)
(rss)
webservice(9)
(rss)
XML(2)
(rss)
我的夢幻旅途(14)
(rss)
隨筆檔案
(669)
2017年9月 (4)
2016年10月 (1)
2015年6月 (1)
2015年4月 (2)
2015年1月 (1)
2014年8月 (2)
2014年7月 (9)
2014年6月 (1)
2014年5月 (2)
2014年4月 (3)
2014年3月 (3)
2013年10月 (4)
2013年9月 (8)
2013年8月 (4)
2013年6月 (3)
2013年5月 (4)
2013年4月 (7)
2013年3月 (1)
2013年1月 (3)
2012年12月 (4)
2012年11月 (1)
2012年10月 (1)
2012年9月 (4)
2012年8月 (1)
2012年7月 (2)
2012年6月 (1)
2012年5月 (4)
2012年4月 (2)
2012年3月 (1)
2012年2月 (4)
2012年1月 (6)
2011年12月 (10)
2011年11月 (7)
2011年10月 (6)
2011年9月 (37)
2011年8月 (34)
2011年7月 (44)
2011年6月 (10)
2011年5月 (5)
2011年4月 (3)
2011年3月 (1)
2011年2月 (1)
2011年1月 (18)
2010年12月 (9)
2010年11月 (13)
2010年10月 (17)
2010年9月 (2)
2010年8月 (10)
2010年7月 (10)
2010年6月 (5)
2010年5月 (8)
2010年4月 (9)
2010年3月 (11)
2010年2月 (3)
2010年1月 (8)
2009年12月 (6)
2009年11月 (10)
2009年10月 (5)
2009年9月 (1)
2009年8月 (18)
2009年7月 (6)
2009年6月 (2)
2009年5月 (1)
2009年4月 (4)
2009年3月 (6)
2009年2月 (5)
2009年1月 (3)
2008年12月 (13)
2008年11月 (13)
2008年10月 (30)
2008年9月 (9)
2008年8月 (24)
2008年7月 (17)
2008年6月 (15)
2008年5月 (16)
2008年4月 (15)
2008年3月 (19)
2008年2月 (3)
2008年1月 (20)
2007年12月 (24)
2007年11月 (9)
文章檔案
(6)
2008年4月 (1)
2008年3月 (1)
2008年1月 (2)
2007年11月 (2)
最新隨筆
1.?PP代碼生成器(四) 使用解決方案生成代碼
2.? PP代碼生成器(三) 設計freemarker模板, 創(chuàng)建解決方案
3.? PP代碼生成器(二) 解決方案, 生成任務, 輔助設計面板
4.? PP代碼生成器(一) 簡介, 下載, 運行
5.?PP持久層代碼生成器
6.?比較好的博客
7.?系統(tǒng)集成項目管理工程師
8.?軟件公司項目經(jīng)理崗位職責
9.?聯(lián)想筆記本 顯示屏 鍵盤失靈 釋放靜電
10.?eclipse maven
積分與排名
積分 - 959953
排名 - 37
最新評論
1.?re: Myeclipse 快捷鍵大全(絕對全)
crl+向右箭頭(輸入法有問題打不出來)移到下一個參數(shù)的位置,然后crl+shift+向右箭頭 選中該位置的參數(shù)即可
--紅領巾
2.?re: Log4j基本使用方法
555
--555
3.?re: Myeclipse 快捷鍵大全(絕對全)[未登錄]
很不錯,謝謝
--銀狐
4.?re: Flex 表單
是誰這么無聊~嗚~~~
--HUIKK
5.?re: Spring AfterReturning 異常
具體是什么意思啊
--dingli
閱讀排行榜
1.?Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream(70030)
2.?log4j.properties 使用說明(42083)
3.?Myeclipse 快捷鍵大全(絕對全)(32733)
4.?TNSNAMES.ORA 配置(24396)
5.?oracle 樹狀查詢(21364)
評論排行榜
1.?Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream(53)
2.?JMail 異常: java.lang.NoClassDefFoundError: javax/activation/DataSource(21)
3.?javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first(14)
4.?java 讀取 excel 2003 或 excel 2007(14)
5.?java.lang.UnsupportedClassVersionError: Bad version number in .class file(8)
TCP一點東東
package org.upeng.mail.net.server;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import org.upeng.mail.net.util.AESUtil;
import org.upeng.mail.net.util.NetConfit;
import org.upeng.mail.net.util.NetLoger;
import org.upeng.mail.process.InstructionProcessor;
/**/
/*
*
* <ul>
* <li>Title:[服務器處理類]</li>
* <li>Description: [type of functional description]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-9-5]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public
class ServerHandler implements Runnable
{
/**/
/*
* SOCKET
*/
private Socket socket;
/**/
/*
* 緩沖寫入
*/
private BufferedWriter bw;
/**/
/*
* 緩沖讀入
*/
private BufferedReader br;
/**/
/*
* 指令處理緩存
*/
private InstructionProcessor processor;
/**/
/*
*
* <ul>
* <li>Description:[構造方法]</li>
* <ul>
*
* @param socket
*/
public
ServerHandler(Socket socket)
{
this.socket
=
socket;
processor
=
new InstructionProcessor();
}
public
void run()
{
try
{
//
創(chuàng)建緩沖讀寫
br
=
new BufferedReader(new InputStreamReader(socket
.getInputStream()));
bw
=
new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream()));
//
客戶端發(fā)來的消息
String msg
=
null
;
while
(true)
{
msg
=
readLine();
//
這里是遠程商品,發(fā)向遠程
processor.process(getRemoteIp(), msg);
write(NetConfit.tcp_oparator_success);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
freeSource();
}
}
/**/
/*
*
* <ul>
* <li>Description:[寫數(shù)據(jù)]</li>
* <li>Created by [Huyvanpull] [2011-9-5]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @param msg
* @throws IOException
*/
private void write(String msg) throws IOException
{
//
判斷寫入的消息是否合法
if
(msg
==
null
||
"".equals(msg))
{
return
;
}
//
處理消息,加上部首, 且加密數(shù)據(jù),防止非法者抓包
String encodeMsg
=
NetConfit.tcp_msg_head
+
msg;
encodeMsg
=
AESUtil.encrypt(encodeMsg, NetConfit.password);
//
寫數(shù)據(jù)
bw.write(encodeMsg);
bw.newLine();
bw.flush();
//
記錄日志
NetLoger.logSnd(getRemoteIp(), socket.getPort(), msg);
}
/**/
/*
*
* <ul>
* <li>Description:[讀數(shù)據(jù)]</li>
* <li>Created by [Huyvanpull] [2011-9-5]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @return
* @throws IOException
*/
private String readLine() throws IOException
{
String decodeMsg
=
"";
//
讀取一行數(shù)據(jù),并判斷數(shù)據(jù)是否非法
String endcodeMsg
=
br.readLine();
if
(endcodeMsg
==
null
||
"".equals(endcodeMsg))
{
return
decodeMsg;
}
//
解密數(shù)據(jù),并判斷數(shù)據(jù)是否是合法的
decodeMsg
=
AESUtil.decrypt(endcodeMsg, NetConfit.password);
if
(decodeMsg.length()
<
5
||
!NetConfit.tcp_msg_head.equals(decodeMsg.
substring
(
0
,
4
)))
{
//
如果消息不合法
return
"";
}
else
{
decodeMsg
=
decodeMsg.
substring
(
4
);
}
//
寫入日志
NetLoger.logRsv(getRemoteIp(), socket.getPort(), decodeMsg);
//
返回結果
return
decodeMsg;
}
/**/
/*
*
* <ul>
* <li>Description:[得到遠程IP]</li>
* <li>Created by [Huyvanpull] [2011-9-7]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @return
*/
private String getRemoteIp()
{
return
socket.getInetAddress().getHostAddress().toString();
}
/**/
/*
*
* <ul>
* <li>Description:[釋放資源]</li>
* <li>Created by [Huyvanpull] [2011-9-6]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
private void freeSource()
{
try
{
//
釋放BufferWriter
if
(bw
!=
null
)
{
bw.
close
();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
try
{
//
釋放BufferReader
if
(br
!=
null
)
{
br.
close
();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
try
{
//
釋放Socket
if
(socket
!=
null
)
{
socket.
close
();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
2.
package
org.upeng.mail.net.server;
import
java.io.IOException;
import
java.net.ServerSocket;
import
java.net.Socket;
import
java.net.SocketTimeoutException;
import
java.util.concurrent.ExecutorService;
import
java.util.concurrent.Executors;
import
org.upeng.mail.net.util.NetConfit;
import
org.upeng.mail.net.util.NetLoger;
/** */
/**
* <ul>
* <li>Title:[TCPServer]</li>
* <li>Description: [TCP網(wǎng)絡服務]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-9-6]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
*
@version
1.0
*/
public
class
TCPServer
extends
Thread
{
/** */
/**
TCP服務器實例
*/
private
static
TCPServer tcpServer
=
new
TCPServer();
/** */
/**
server socket
*/
private
ServerSocket serverSocket;
private
ExecutorService executorService;
/** */
/**
端口號
*/
private
int
port;
/** */
/**
是否啟動成功(如端口已經(jīng)被占用,就不能啟動成功)
*/
private
boolean
isStoped
=
false
;
/** */
/**
* <ul>
* <li>Description:[構造方法]</li>
* <ul>
*/
private
TCPServer()
{
startServer();
}
/** */
/**
* <ul>
* <li>Description:[得到單態(tài)的實例]</li>
* <li>Created by [Huyvanpull] [2011-9-6]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
*
@return
*/
public
static
TCPServer getInstance()
{
return
tcpServer;
}
/**/
/*
* (non-Javadoc)
*
* @see java.lang.Thread#run()
*/
public
void
run()
{
this
.service();
}
/** */
/**
* <ul>
* <li>Description:[服務]</li>
* <li>Created by [Huyvanpull] [2011-9-6]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
private
void
service()
{
//
測試信息
StringBuffer debugInfo
=
new
StringBuffer();
while
(
!
isStoped)
{
Socket socket
=
null
;
try
{
//
等等連接
socket
=
serverSocket.accept();
//
測試信息
debugInfo.delete(
0
, debugInfo.length());
debugInfo.append(
"
服務器與
"
);
debugInfo.append(socket.getRemoteSocketAddress().toString());
debugInfo.append(
"
建立連接
"
);
NetLoger.log(debugInfo.toString());
//
服務器處理線程
executorService.execute(
new
ServerHandler(socket));
}
catch
(SocketTimeoutException stex)
{}
catch
(Exception e)
{
e.printStackTrace();
}
}
}
/** */
/**
* <ul>
* <li>Description:[服務器是否啟動成功]</li>
* <li>Created by [Huyvanpull] [2011-9-6]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
*
@return
*/
public
boolean
isStopeded()
{
return
isStoped;
}
/** */
/**
* <ul>
* <li>Description:[啟動服務器]</li>
* <li>Created by [Huyvanpull] [2011-9-6]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
private
void
startServer()
{
//
設置端口,是測試模式和正式模式
this
.port
=
NetConfit.localPort;
try
{
serverSocket
=
new
ServerSocket(port);
serverSocket.setSoTimeout(
30000
);
executorService
=
Executors.newCachedThreadPool();
NetLoger.log(
"
TCP服務器啟動成功.
"
);
this
.start();
}
catch
(Exception ex)
{
isStoped
=
true
;
NetLoger.log(
"
TCP服務器啟動失敗.
"
);
ex.printStackTrace();
}
}
/** */
/**
* <ul>
* <li>Description:[關閉服務器]</li>
* <li>Created by [Huyvanpull] [2011-9-6]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
@SuppressWarnings(
"
deprecation
"
)
public
void
shutDown()
{
try
{
this
.executorService.shutdown();
this
.executorService.shutdownNow();
this
.isStoped
=
true
;
serverSocket.close();
}
catch
(Exception ex)
{
ex.printStackTrace();
}
NetLoger.log(
"
關閉socket服務器
"
);
}
public
static
void
main(String[] args)
throws
IOException
{
TCPServer.getInstance();
System.out.println(TCPServer.getInstance().isStoped);
}
}
posted on 2011-10-22 00:51
HUIKK
閱讀(401)
評論(0)
編輯
收藏
所屬分類:
java Net
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
TCP一點東東
(轉(zhuǎn))MINA2 之IoBuffer
(轉(zhuǎn))Mina 框架總結
socketPool
apache mina框架小試
jboss netty 框架小試
java 簡單socket 通信
[轉(zhuǎn)]JAVA-用HttpClient來模擬瀏覽器GET,POST
UDP
java 得到本機所有ip
Powered by:
BlogJava
Copyright © HUIKK
主站蜘蛛池模板:
安徽省
|
凤山市
|
玛曲县
|
阿拉善左旗
|
吴桥县
|
泗洪县
|
肥城市
|
南京市
|
伊宁县
|
团风县
|
监利县
|
迁安市
|
巨野县
|
湘阴县
|
临城县
|
丰城市
|
陆良县
|
望谟县
|
北流市
|
同心县
|
尼玛县
|
兴海县
|
沽源县
|
曲水县
|
北海市
|
木里
|
伊宁县
|
什邡市
|
钟山县
|
临海市
|
西吉县
|
赣州市
|
海淀区
|
大姚县
|
榆树市
|
漯河市
|
泽库县
|
潞城市
|
金湖县
|
湖南省
|
兴海县
|