Cyh的博客
Email:kissyan4916@163.com
posts - 26, comments - 19, trackbacks - 0, articles - 220
導航
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
公告
一直努力努力努力,像奴隸奴隸奴隸!~~
<
2025年7月
>
日
一
二
三
四
五
六
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
8
9
常用鏈接
我的隨筆
我的文章
我的評論
我的參與
最新評論
隨筆檔案
(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標簽的使用(18242)
2.?struts2異常攔截器(5869)
3.?struts2迭代標簽(3852)
4.?用freemind 秒殺Spring Security(1925)
5.?加載順序會影響對spring bean 的調用。(1494)
網絡編程>>基本的Socket編程
Posted on 2009-12-12 16:04
啥都寫點
閱讀(232)
評論(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
{
//
從客戶端的輸入流中讀取一行數(shù)據
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
{
//
往客戶端輸出流中寫數(shù)據
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的輸出流寫數(shù)據
out.println(request);
System.out.println(
"
Client 發(fā)送請求:
"
+
request);
}
public
String getResponse()
{
String str
=
new
String();
try
{
//
從socket的輸入流中讀取數(shù)據
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;
//
在構造函數(shù)中完成圖形界面的初始化
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 © 啥都寫點
主站蜘蛛池模板:
国产免费观看黄AV片
|
亚洲精品乱码久久久久久蜜桃不卡
|
亚洲精品岛国片在线观看
|
亚洲黄黄黄网站在线观看
|
亚洲欧洲日产国码无码久久99
|
亚洲天堂在线播放
|
亚洲情A成黄在线观看动漫软件
|
亚洲精品无码专区在线播放
|
四虎精品成人免费视频
|
99在线热播精品免费99热
|
99热免费在线观看
|
最近免费中文字幕视频高清在线看
|
全部免费a级毛片
|
亚洲AV无码久久精品成人
|
亚洲人xxx日本人18
|
日韩在线视频免费
|
免费国产黄网站在线观看
|
亚洲国产第一站精品蜜芽
|
国产成人亚洲综合无码精品
|
亚洲一区在线观看视频
|
人妻巨大乳hd免费看
|
亚洲一区二区三区免费视频
|
精品国产免费一区二区
|
亚洲精品无码久久久久sm
|
www.亚洲成在线
|
国产精品免费视频观看拍拍
|
在线视频精品免费
|
亚洲成a人片在线观看日本麻豆
|
吃奶摸下高潮60分钟免费视频
|
亚洲精品中文字幕无码蜜桃
|
亚洲中文字幕无码一去台湾
|
久久九九免费高清视频
|
成视频年人黄网站免费视频
|
亚洲一区二区三区无码影院
|
亚洲午夜电影在线观看
|
久久久受www免费人成
|
最近2019中文字幕mv免费看
|
亚洲人成人无码网www电影首页
|
亚洲人成色在线观看
|
baoyu777永久免费视频
|
日本高清免费网站
|