




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第Java聊天室之使用Socket實現(xiàn)傳遞對象目錄一、題目描述二、解題思路三、代碼詳解
一、題目描述
題目實現(xiàn):使用網(wǎng)絡(luò)編程時,需要通過Socket傳遞對象。
二、解題思路
創(chuàng)建一個類:Student,實現(xiàn)序列化
?Student類包含兩個屬性及對應(yīng)的get()和set()方法
創(chuàng)建一個服務(wù)器類:ServerSocketFrame,繼承JFrame類
寫一個getserver()方法,實例化Socket對象,啟用9527當(dāng)服務(wù)的端口。
創(chuàng)建輸入流對象,用來接收客戶端信息。
再定義一個getClientInfo()方法,用于接收客戶端發(fā)送的信息。
對文本框添加一個事件:實現(xiàn)向客戶端發(fā)磅信息。
創(chuàng)建一個客戶端類:ClientSocketFrame,繼承JFrame類。
寫一個connect()方法,實例化Socket對象,連接本地服務(wù)的9527端口服務(wù)。
再定義一個getClientInfo()方法,用于接收服務(wù)端發(fā)送的信息。
技術(shù)重點(diǎn):
在Java中使用Socket傳遞對象時,該對象必須是序列化的,在Java中實現(xiàn)Serializable接口的類,創(chuàng)建的對象就是序列化對象,可以通過Socket進(jìn)行傳遞,從而實現(xiàn)了使用Socket傳遞對象的功能。Serializable接口在javaio包中,該接口沒有方法,只是用于標(biāo)識對象是可序列化的。該接口的定義如下:publicinterfaceSerializable使用Serializable接口,可以創(chuàng)建序列化類。
三、代碼詳解
Student
packagecom.xiaoxuzhu;
importjava.io.Serializable;
*Description:
*@authorxiaoxuzhu
*@version1.0
*pre
*修改記錄:
*修改后版本修改人修改日期修改內(nèi)容
*2025/6/4.1xiaoxuzhu2025/6/4Create
*/pre
*@date2025/6/4
publicclassStudentimplementsSerializable{//序列化對象類
privateStringid;//類的成員變量
privateStringname;//類的成員變量
publicStringgetId(){//成員變量的getter方法
returnid;
publicvoidsetId(Stringid){//成員變量的setter方法
this.id=id;
publicStringgetName(){//成員變量的getter方法
returnname;
publicvoidsetName(Stringname){//成員變量的setter方法
=name;
}
ServerSocketFrame
packagecom.xiaoxuzhu;
importjava.awt.BorderLayout;
importjava.awt.Dimension;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.*;
import.*;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
*Description:
*@authorxiaoxuzhu
*@version1.0
*pre
*修改記錄:
*修改后版本修改人修改日期修改內(nèi)容
*2025/6/4.1xiaoxuzhu2025/6/4Create
*/pre
*@date2025/6/4
publicclassServerSocketFrameextendsJFrame{
privateJTextFieldtf_name;
privateJTextFieldtf_id;
privateJTextAreata_info;
privateObjectOutputStreamout=null;//創(chuàng)建流對象
privateObjectInputStreamin=null;//創(chuàng)建流對象
privateServerSocketserver;//聲明ServerSocket對象
privateSocketsocket;//聲明Socket對象socket
publicvoidgetserver(){
try{
server=newServerSocket(9527);//實例化Socket對象
ta_info.append("服務(wù)器套接字已經(jīng)創(chuàng)建成功\n");//輸出信息
while(true){//如果套接字是連接狀態(tài)
ta_info.append("等待客戶機(jī)的連接......\n");//輸出信息
socket=server.accept();//實例化Socket對象
ta_info.append("客戶機(jī)連接成功\n");//輸出信息
out=newObjectOutputStream(socket.getOutputStream());
in=newObjectInputStream(socket.getInputStream());
getClientInfo();//調(diào)用getClientInfo()方法
}catch(Exceptione){
e.printStackTrace();//輸出異常信息
privatevoidgetClientInfo(){
try{
while(true){//如果套接字是連接狀態(tài)
Studentstud=(Student)in.readObject();
if(stud!=null)
ta_info.append("接收到客戶機(jī)發(fā)送的編號為:"+stud.getId()+"名稱為:"+stud.getName()+"\n");//獲得客戶端信息
}catch(Exceptione){
ta_info.append("客戶端已退出。\n");//輸出異常信息
}finally{
try{
if(in!=null){
in.close();//關(guān)閉流
if(socket!=null){
socket.close();//關(guān)閉套接字
}catch(IOExceptione){
e.printStackTrace();
publicstaticvoidmain(String[]args){//主方法
ServerSocketFrameframe=newServerSocketFrame();//創(chuàng)建本類對象
frame.setVisible(true);
frame.getserver();//調(diào)用方法
publicServerSocketFrame(){
super();
setTitle("服務(wù)器端程序");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,379,260);
finalJScrollPanescrollPane=newJScrollPane();
getContentPane().add(scrollPane,BorderLayout.CENTER);
ta_info=newJTextArea();
scrollPane.setViewportView(ta_info);
finalJPanelpanel=newJPanel();
getContentPane().add(panel,BorderLayout.NORTH);
finalJLabellabel=newJLabel();
label.setText("編號:");
panel.add(label);
tf_id=newJTextField();
tf_id.setPreferredSize(newDimension(70,25));
panel.add(tf_id);
finalJLabellabel_1=newJLabel();
label_1.setText("名稱:");
panel.add(label_1);
tf_name=newJTextField();
tf_name.setPreferredSize(newDimension(100,25));
panel.add(tf_name);
finalJButtonbutton=newJButton();
button.addActionListener(newActionListener(){
publicvoidactionPerformed(finalActionEvente){
Studentstud=newStudent();
stud.setId(tf_id.getText());
stud.setName(tf_name.getText());
try{
out.writeObject(stud);
}catch(IOExceptione1){
e1.printStackTrace();
ta_info.append("服務(wù)器發(fā)送的編號是:"+tf_id.getText()+"名稱是:"+tf_name.getText()+"\n");//將文本框中信息顯示在文本域中
tf_id.setText(null);//將文本框清空
tf_name.setText(null);
button.setText("發(fā)送");
panel.add(button);
}
ClientSocketFrame
packagecom.xiaoxuzhu;
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
import.*;
importjavax.swing.*;
*Description:
*@authorxiaoxuzhu
*@version1.0
*pre
*修改記錄:
*修改后版本修改人修改日期修改內(nèi)容
*2025/6/4.1xiaoxuzhu2025/6/4Create
*/pre
*@date2025/6/4
publicclassClientSocketFrameextendsJFrame{//創(chuàng)建類繼承JFrame類
privateJButtonbutton;
privateJTextFieldtf_name;
privateJLabellabel_1;
privateJLabellabel;
privateJPanelpanel;
privateObjectInputStreamin=null;//創(chuàng)建流對象
privateObjectOutputStreamout=null;//創(chuàng)建流對象
privateSocketsocket;//聲明Socket對象
privateJTextAreata_info=newJTextArea();//創(chuàng)建JtextArea對象
privateJTextFieldtf_id=newJTextField();//創(chuàng)建JtextField對象
privateContainercc;//聲明Container對象
publicClientSocketFrame(){//構(gòu)造方法
super();//調(diào)用父類的構(gòu)造方法
setTitle("客戶端程序");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,373,257);
cc=this.getContentPane();//實例化對象
finalJScrollPanescrollPane=newJScrollPane();
getContentPane().add(scrollPane,BorderLayout.CENTER);
scrollPane.setViewportView(ta_info);
getContentPane().add(getPanel(),BorderLayout.NORTH);
privatevoidconnect(){//連接套接字方法
ta_info.append("嘗試連接......\n");//文本域中信息信息
try{//捕捉異常
socket=newSocket("",9527);//實例化Socket對象
while(true){
out=newObjectOutputStream(socket.getOutputStream());
in=newObjectInputStream(socket.getInputStream());
ta_info.append("完成連接。\n");//文本域中提示信息
getClientInfo();
}catch(Exceptione){
e.printStackTrace();//輸出異常信息
publicstaticvoidmain(String[]args){//主方法
ClientSocketFrameclien=newClientSocketFrame();//創(chuàng)建本例對象
clien.setVisible(true);//將窗體顯示
clien.connect();//調(diào)用連接方法
privatevoidgetClientInfo(){
try{
while(true){//如果套接字是連接狀態(tài)
Studentstud=(Student)in.readObject();
if(stud!=null)
ta_info.append("接收到服務(wù)器發(fā)送的編號為:"+stud.getId()+"名稱為:"+stud.getName()+"\n");//獲得服務(wù)器信息
}catch(Exceptione){
e.printStackTrace();
finally{
try{
if(in!=null){
in.close();//關(guān)閉流
if(socket!=null){
socket.close();//關(guān)閉套接字
}catch(IOExceptione){
e.printStackTrace();
*@return
protectedJPanelgetPanel(){
if(panel==null){
panel=newJPanel();
panel.add(getLabel());
tf_id.setPreferredSize(newDimension(70,25));
panel.add(tf_id);
panel.add(getLabel_1());
panel.add(getTf_name());
panel.add(getButton());
returnpanel;
*@return
protectedJLabelgetLabel(){
if(label==null){
label=newJLabel();
label.setText("編號:");
returnlabel;
*@return
protectedJLabelgetLabel_1(){
if(label_1==null){
label_1=newJLabel();
label_1.setText("名稱:");
returnlabel_1;
*@return
protectedJTextFie
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年沉浸式戲劇新媒體營銷策略與推廣效果分析報告
- 2025年港口自動化裝卸設(shè)備在自動化港口堆場管理中的應(yīng)用與效益分析報告
- 2025年工程經(jīng)濟(jì)案例分析題試題及答案
- 2025年大數(shù)據(jù)精準(zhǔn)營銷在文化產(chǎn)業(yè)的創(chuàng)新應(yīng)用報告
- 焊工火災(zāi)應(yīng)急預(yù)案(3篇)
- 充電樁建設(shè)與運(yùn)營的效益分析
- 城區(qū)熱力管網(wǎng)項目實施方案(參考模板)
- 殘疾人就業(yè)保障實施方案
- 安徽xx工廠建設(shè)可行性研究報告
- 家居行業(yè)2025年線上線下融合模式創(chuàng)新與消費(fèi)者行為研究新視角報告
- 內(nèi)鏡下ESD護(hù)理配合
- 直腸癌課件完整版本
- 2024至2030年中國動畫產(chǎn)業(yè)投資分析及前景預(yù)測報告
- 2025年中考?xì)v史復(fù)習(xí)專項訓(xùn)練:世界現(xiàn)代史選擇題100題(原卷版)
- 四年級下冊語文課外閱讀題三(5篇含答案)
- 五年級小數(shù)乘法練習(xí)題300道及答案
- 萬達(dá)商家入駐商場合同(2024版)
- 【課件】初心與使命-時代的美術(shù)擔(dān)當(dāng)+課件-高中美術(shù)人美版(2019)美術(shù)鑒賞
- DLT 722-2014 變壓器油中溶解氣體分析和判斷導(dǎo)則
- 北師大版五年級下冊英語教案
- 鋁合金薄板的熱處理工藝與性能的研究
評論
0/150
提交評論