Cyh的博客
Email:kissyan4916@163.com
posts - 26, comments - 19, trackbacks - 0, articles - 220
導航
BlogJava
首頁
新隨筆
聯系
聚合
管理
公告
一直努力努力努力,像奴隸奴隸奴隸!~~
<
2025年5月
>
日
一
二
三
四
五
六
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
6
7
常用鏈接
我的隨筆
我的文章
我的評論
我的參與
最新評論
隨筆檔案
(25)
2011年5月 (1)
2010年4月 (12)
2010年1月 (1)
2009年12月 (2)
2009年6月 (1)
2009年4月 (4)
2009年2月 (4)
文章分類
(219)
Android(26)
DB(5)
J2EE(31)
J2SE(79)
JavaScript(15)
others(47)
SOA&Web Service(1)
中間件(1)
軟件工程(12)
軟件架構(2)
文章檔案
(220)
2011年8月 (1)
2010年12月 (23)
2010年11月 (2)
2010年8月 (5)
2010年7月 (2)
2010年6月 (2)
2010年5月 (1)
2010年4月 (12)
2010年3月 (28)
2010年2月 (5)
2010年1月 (23)
2009年12月 (39)
2009年6月 (14)
2009年5月 (31)
2009年3月 (2)
2009年2月 (29)
2009年1月 (1)
新聞檔案
(66)
2010年10月 (1)
2010年9月 (5)
2010年8月 (11)
2010年7月 (21)
2010年6月 (13)
2010年5月 (8)
2010年4月 (5)
2009年11月 (2)
相冊
Ryan
收藏夾
(7)
JAVA(7)
最新隨筆
1.?集成FCKeditor 3.5.3
2.?android自適應屏幕方向和大小
3.?Android游戲開發(fā)之旅(二十) 雙按事件捕獲
4.?Android游戲開發(fā)之旅(十八) SoundPool類
5.?Android游戲開發(fā)之旅(十九) 分辨率大全
6.?Android游戲開發(fā)之旅(十七) 圖像漸變特效
7.?Android游戲開發(fā)之旅(十六) 異步音樂播放
8.? Android游戲開發(fā)之旅(十四) 游戲開發(fā)實戰(zhàn)一
9.?Android游戲開發(fā)之旅(十五) 按鍵中斷處理
10.?Android游戲開發(fā)之旅(十二)Sensor重力感應(2)
搜索
最新評論
1.?re: struts2 checkboxlist標簽的使用
同居同意同意
--yuk
2.?re: struts2 checkboxlist標簽的使用
ss
--d
3.?re: JavaMail(4)--使用POP3接收郵件
郵件信息可以打印出來,可是下載郵件會出錯是什么原因?
--琳喵喵0721
4.?re: JavaMail(4)--使用POP3接收郵件
評論內容較長,點擊標題查看
--流風
5.?re: 操作PDF文件
評論內容較長,點擊標題查看
--ly.wolf
閱讀排行榜
1.?struts2 checkboxlist標簽的使用(18234)
2.?struts2異常攔截器(5862)
3.?struts2迭代標簽(3847)
4.?用freemind 秒殺Spring Security(1920)
5.?加載順序會影響對spring bean 的調用。(1491)
網絡編程>>基本的Socket編程
Posted on 2009-12-12 16:04
啥都寫點
閱讀(226)
評論(0)
編輯
收藏
所屬分類:
J2SE
Socket 服務器端需要在某個端口上開啟服務端類型的Socket,即java.net.ServerSocket.通過它的accept方法等待并接收客戶端的請求,返回的是一個java.net.Socket對象,如果一直沒有客戶端請求,那么accept方法將會一直等待。
Socket 客戶端根據服務器端的IP地址和端口號創(chuàng)建一個Socket對象,連接服務器。
服務器端和客戶端都持有一個Socket對象,服務器端的Socket從服務器端指向客戶端,而客戶端的Socket從客戶端指向服務器端,就像在服務器和客戶端建立了兩條單向的管道
Socket 類提供的getOutputStream方法獲得Socket的輸出流,getInputStream方法獲得Socket的輸入流。
/** */
/**
---------------------------SimpleServer.java-------------------------------
*/
/** */
/**
* 一個簡單的socket服務器,能接受客戶端請求,并將請求返回給客戶端
*/
public
class
SimpleServer
{
//
服務端偵聽的Socket
ServerSocket serverSkt
=
null
;
//
客戶端
Socket clientSkt
=
null
;
//
客戶端輸入流
BufferedReader in
=
null
;
//
客戶端輸出流
PrintStream out
=
null
;
//
構造方法
public
SimpleServer(
int
port)
{
System.out.println(
"
服務器代理正在監(jiān)聽,端口:
"
+
port);
try
{
//
創(chuàng)建監(jiān)聽socket
serverSkt
=
new
ServerSocket(port);
}
catch
(IOException e)
{
System.out.println(
"
監(jiān)聽端口
"
+
port
+
"
失敗
"
);
}
try
{
//
接收連接請求
clientSkt
=
serverSkt.accept();
}
catch
(IOException e)
{
System.out.println(
"
連接失敗
"
);
}
try
{
//
獲得輸出輸出流
in
=
new
BufferedReader(
new
InputStreamReader(clientSkt
.getInputStream()));
out
=
new
PrintStream(clientSkt.getOutputStream());
}
catch
(IOException e)
{
}
}
//
收到客戶端請求
public
String getRequest()
{
String frmClt
=
null
;
try
{
//
從客戶端的輸入流中讀取一行數據
frmClt
=
in.readLine();
System.out.println(
"
Server 收到請求:
"
+
frmClt);
}
catch
(Exception e)
{
System.out.println(
"
無法讀取端口
..
"
);
System.exit(
0
);
}
return
frmClt;
}
//
發(fā)送響應給客戶端
public
void
sendResponse(String response)
{
try
{
//
往客戶端輸出流中寫數據
out.println(response);
System.out.println(
"
Server 響應請求:
"
+
response);
}
catch
(Exception e)
{
System.out.println(
"
寫端口失敗
"
);
System.exit(
0
);
}
}
public
static
void
main(String[] args)
throws
IOException
{
//
啟動服務器
SimpleServer sa
=
new
SimpleServer(
8888
);
while
(
true
)
{
//
讀取客戶端的輸入并返回給客戶端。
sa.sendResponse(sa.getRequest());
}
}
}
/** */
/**
--------------------------SimpleClient.java--------------------------
*/
/** */
/**
* 一個簡單的socket客戶端,能夠往服務器發(fā)送socket請求。
*/
public
class
SimpleClient
{
//
客戶端輸入輸出流
PrintStream out;
BufferedReader in;
//
構造方法
public
SimpleClient(String serverName,
int
port)
{
try
{
//
根據服務器名和端口號,連接服務器
Socket clientSocket
=
new
Socket(serverName, port);
//
獲取socket的輸入輸出流
out
=
new
PrintStream(clientSocket.getOutputStream());
in
=
new
BufferedReader(
new
InputStreamReader(clientSocket
.getInputStream()));
}
catch
(Exception e)
{
System.out.println(
"
無法連接服務器!
"
);
}
}
//
發(fā)送請求
public
void
sendRequest(String request)
{
//
向socket的輸出流寫數據
out.println(request);
System.out.println(
"
Client 發(fā)送請求:
"
+
request);
}
public
String getResponse()
{
String str
=
new
String();
try
{
//
從socket的輸入流中讀取數據
str
=
in.readLine();
System.out.println(
"
Client收到Server返回:
"
+
str);
}
catch
(IOException e)
{
}
return
str;
}
}
/** */
/**
-----------------------------ClientFrame.java---------------------------------------
*/
/** */
/**
* 客戶端的圖形界面
*/
public
class
ClientFrame
extends
JFrame
implements
ActionListener
{
//
"發(fā)送"按鈕
JButton sendButton;
//
發(fā)送內容的輸入框
JTextField inputField;
//
服務器返回內容的文本域
JTextArea outputArea;
//
客戶端socket對象
SimpleClient client;
//
在構造函數中完成圖形界面的初始化
public
ClientFrame()
{
JLabel label1
=
new
JLabel(
"
輸入:
"
);
inputField
=
new
JTextField(
20
);
JPanel panel1
=
new
JPanel();
panel1.add(label1);
panel1.add(inputField);
JLabel label2
=
new
JLabel(
"
服務器返回:
"
);
outputArea
=
new
JTextArea(
6
,
20
);
JScrollPane crollPane
=
new
JScrollPane(outputArea);
JPanel panel2
=
new
JPanel();
panel2.setLayout(
new
BorderLayout());
panel2.add(label2, BorderLayout.NORTH);
panel2.add(crollPane, BorderLayout.CENTER);
sendButton
=
new
JButton(
"
發(fā) 送
"
);
sendButton.addActionListener(
this
);
JPanel panel
=
new
JPanel();
panel.setLayout(
new
BorderLayout());
panel.add(panel1, BorderLayout.NORTH);
panel.add(sendButton, BorderLayout.CENTER);
panel.add(panel2, BorderLayout.PAGE_END);
setTitle(
"
Socket 客戶端
"
);
this
.getContentPane().add(panel);
this
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public
void
actionPerformed(ActionEvent ae)
{
//
判斷事件源控件是否是"發(fā)送"按鈕
if
(ae.getSource()
==
sendButton)
{
try
{
//
發(fā)送文本框中的文本
client.sendRequest(inputField.getText());
}
catch
(Exception ex)
{
ex.printStackTrace();
}
//
接收服務器回應并寫入文本域
outputArea.append(client.getResponse()
+
"
\n
"
);
}
}
public
static
void
main(String[] args)
{
ClientFrame frame
=
new
ClientFrame();
frame.pack();
//
連接服務器
frame.client
=
new
SimpleClient(
"
127.0.0.1
"
,
8888
);
frame.setVisible(
true
);
}
}
--
學海無涯
Powered by:
BlogJava
Copyright © 啥都寫點
主站蜘蛛池模板:
亚洲免费日韩无码系列
|
91热久久免费精品99
|
亚洲女同成av人片在线观看
|
日韩精品无码免费专区网站
|
亚洲精品国产免费
|
国产精品久免费的黄网站
|
精品无码一级毛片免费视频观看
|
91嫩草私人成人亚洲影院
|
免费看AV毛片一区二区三区
|
三年在线观看免费观看完整版中文
|
亚洲美女一区二区三区
|
免费一级国产生活片
|
人妻无码一区二区三区免费
|
精品亚洲av无码一区二区柚蜜
|
亚洲国产精品一区二区第一页
|
免费国产黄线在线观看
|
你是我的城池营垒免费看
|
久久亚洲国产成人影院
|
伊人久久大香线蕉亚洲
|
99久久免费精品国产72精品九九
|
国产特黄一级一片免费
|
亚洲а∨天堂久久精品9966
|
久久久久久久91精品免费观看
|
免费亚洲视频在线观看
|
亚洲美女一区二区三区
|
狠狠色婷婷狠狠狠亚洲综合
|
久久精品a一国产成人免费网站
|
三年片在线观看免费西瓜视频
|
色偷偷尼玛图亚洲综合
|
亚洲综合激情九月婷婷
|
亚洲日本韩国在线
|
日韩高清免费在线观看
|
91热成人精品国产免费
|
a国产成人免费视频
|
一级毛片在线免费播放
|
中中文字幕亚洲无线码
|
久久久久亚洲AV片无码下载蜜桃
|
亚洲中文字幕成人在线
|
日本免费一二区在线电影
|
在线视频免费观看爽爽爽
|
久久免费精品视频
|