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 © 啥都寫點
主站蜘蛛池模板:
免费精品视频在线
|
亚洲va在线va天堂va四虎
|
亚洲人成网站免费播放
|
国产l精品国产亚洲区在线观看
|
国产精品免费福利久久
|
国产亚洲av人片在线观看
|
免费看一级一级人妻片
|
免费观看亚洲人成网站
|
男男黄GAY片免费网站WWW
|
国产免费怕怕免费视频观看
|
豆国产96在线|亚洲
|
免费v片在线观看品善网
|
又长又大又粗又硬3p免费视频
|
亚洲成?v人片天堂网无码
|
午夜成人无码福利免费视频
|
伊人久久综在合线亚洲91
|
三年片在线观看免费西瓜视频
|
亚洲狠狠综合久久
|
国内精自视频品线六区免费
|
成人亚洲国产va天堂
|
免费v片视频在线观看视频
|
一区二区三区免费高清视频
|
国产亚洲3p无码一区二区
|
免费A级毛片无码A∨
|
亚洲成_人网站图片
|
亚洲精品岛国片在线观看
|
黄色网址在线免费
|
亚洲狠狠ady亚洲精品大秀
|
性感美女视频免费网站午夜
|
九九免费观看全部免费视频
|
久久精品国产精品亚洲艾草网
|
7723日本高清完整版免费
|
国产亚洲精品欧洲在线观看
|
亚洲精品制服丝袜四区
|
在线看片无码永久免费视频
|
欧洲乱码伦视频免费国产
|
亚洲视屏在线观看
|
一本色道久久88亚洲综合
|
99视频在线免费看
|
猫咪免费观看人成网站在线
|
久久久久亚洲av无码专区
|