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 © 啥都寫點
主站蜘蛛池模板:
亚洲中文久久精品无码1
|
久久香蕉国产线看免费
|
好吊妞998视频免费观看在线
|
亚洲中文字幕无码不卡电影
|
a一级毛片免费高清在线
|
免费大学生国产在线观看p
|
国产精品亚洲色婷婷99久久精品
|
免费观看a级毛片
|
亚洲av永久无码天堂网
|
全部免费国产潢色一级
|
青青免费在线视频
|
亚洲自偷自偷在线制服
|
久久国产福利免费
|
亚洲精品国产品国语在线
|
精品国产麻豆免费人成网站
|
91亚洲国产成人久久精品网站
|
最近2019免费中文字幕6
|
亚洲福利一区二区精品秒拍
|
成年在线网站免费观看无广告
|
亚洲AV无码AV日韩AV网站
|
亚洲精品国产高清嫩草影院
|
a级毛片无码免费真人久久
|
在线电影你懂的亚洲
|
免费观看男人免费桶女人视频
|
爱情岛亚洲论坛在线观看
|
国产亚洲老熟女视频
|
蜜桃视频在线观看免费视频网站WWW
|
亚洲同性男gay网站在线观看
|
日韩免费三级电影
|
国产成人免费AV在线播放
|
亚洲中文字幕无码av在线
|
日韩免费在线观看
|
天堂在线免费观看
|
tom影院亚洲国产一区二区
|
国产a级特黄的片子视频免费
|
成人免费777777被爆出
|
亚洲国产成人超福利久久精品
|
国产一级淫片免费播放
|
日本xxxx色视频在线观看免费
|
亚洲人成人网站18禁
|
国产AV无码专区亚洲精品
|