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 © 啥都寫點
主站蜘蛛池模板:
亚洲一区二区三区免费
|
免费看国产精品麻豆
|
黄网站色视频免费看无下截
|
亚洲综合激情六月婷婷在线观看
|
成人免费视频试看120秒
|
久久永久免费人妻精品
|
四虎精品免费永久免费视频
|
亚洲欧美成人一区二区三区
|
亚洲免费在线播放
|
亚洲三区在线观看无套内射
|
精品国产日韩亚洲一区
|
中文字幕亚洲乱码熟女一区二区
|
又爽又黄无遮挡高清免费视频
|
亚洲免费综合色在线视频
|
67pao强力打造国产免费
|
91香焦国产线观看看免费
|
四虎国产精品永久免费网址
|
91制片厂制作传媒免费版樱花
|
99精品视频在线视频免费观看
|
国产午夜免费高清久久影院
|
久久久久成人精品免费播放动漫
|
91国内免费在线视频
|
曰批视频免费40分钟试看天天
|
1000部啪啪毛片免费看
|
成人av免费电影
|
一区国严二区亚洲三区
|
国产精品亚洲а∨无码播放
|
久久精品国产亚洲AV麻豆王友容
|
图图资源网亚洲综合网站
|
亚洲免费在线视频观看
|
猫咪免费观看人成网站在线
|
99视频免费在线观看
|
无码中文字幕av免费放
|
亚洲第一页日韩专区
|
亚洲欧洲自拍拍偷午夜色
|
国产99久久亚洲综合精品
|
久视频精品免费观看99
|
亚洲欧洲久久av
|
中文字幕乱码亚洲无线三区
|
香蕉免费看一区二区三区
|
久久这里只有精品国产免费10
|