Java聊天室之使用Socket實現(xiàn)傳遞對象_第1頁
Java聊天室之使用Socket實現(xiàn)傳遞對象_第2頁
Java聊天室之使用Socket實現(xiàn)傳遞對象_第3頁
Java聊天室之使用Socket實現(xiàn)傳遞對象_第4頁
Java聊天室之使用Socket實現(xiàn)傳遞對象_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論