《西安郵電學(xué)院 通信軟件設(shè)計》由會員分享,可在線閱讀,更多相關(guān)《西安郵電學(xué)院 通信軟件設(shè)計(8頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、最新 精品 Word 歡迎下載 可修改
西 安 郵 電 學(xué) 院
實 驗 報 告
課 程 通信軟件設(shè)計
開課時間 至學(xué) 年第 學(xué)期
這個軟件主要功能是以UDP為基礎(chǔ)實現(xiàn)兩臺電腦之間收發(fā)簡單文本消息。軟件全部使用java編寫,并以JavaBuilder8為工具進(jìn)行編寫與調(diào)試。布局方式采用NULL方式,主要使用AWT的組件和部分SWING的組件。
使用方法如下:
1 首先在輸入IP地址的輸入欄中輸入想與之進(jìn)行通信的
2、主機(jī)的IP地址,即消息接受方的IP地址。
2 在消息編輯框中輸入想發(fā)送的消息內(nèi)容。
3 編輯完欲發(fā)送的消息后,點擊“發(fā)送消息”按鈕進(jìn)行發(fā)送;
4 雙方發(fā)送的消息和發(fā)送方與接受方的IP地址都會在下面的空白區(qū)域----“頻道”內(nèi)顯示。
6 如果沒有輸入IP地址便發(fā)送編輯完的文本消息,空白區(qū)域內(nèi)會顯示出提示信息提示用戶應(yīng)輸入接受方的IP地址。
7 當(dāng)頻道內(nèi)顯示的消息太多時,可以按”刷新頻道”來清除頻道內(nèi)全部信息!
軟件運行時候的抓圖:
本機(jī)測試結(jié)果的抓圖效果
程序代碼與簡要說明
1 chatFrame類的代碼:
package chat;
3、
import java.io.*;
import java.awt.*;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2021
*
Company:
* @author not attributable
* @version 1.0
*/
public class chatFrame extends JFrame {
JPanel contentPane;
Label label1 = new Lab
4、el();
TextField textField1 = new TextField();
Label label2 = new Label();
TextField textField2 = new TextField();
Button button1 = new Button();
TextArea textArea1 = new TextArea();
DatagramPacket sendpacket,receivepacket; //定義發(fā)送和接受數(shù)據(jù)包
DatagramSocket sendsocket,receivesocket;
5、
//定義發(fā)送和接受DatagramSocket
//Construct the frame
public chatFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
6、 //定義個組件和他們的布局
contentPane = (JPanel) this.getContentPane();
label2.setAlignment(Label.CENTER);
label2.setForeground(Color.black);
label2.setText("消息編輯框");
label1.setBounds(new Rectangle(14, 17, 133, 32));
contentPane.setLayout(null);
this.setSize(new Dimensi
7、on(500, 400));
this.setTitle("用UDP實現(xiàn)聊天");
textField1.setBackground(SystemColor.text);
textField1.setColumns(0);
textField1.setText("");
textField1.setBounds(new Rectangle(129, 72, 200, 61));
label1.setAlignment(Label.CENTER);
label1.setText("輸入對方IP地址");
la
8、bel2.setBounds(new Rectangle(23, 86, 108, 28));
try{
sendsocket=new DatagramSocket(5000); //初始化
receivesocket=new DatagramSocket(5001);
}
catch(SocketException se){ //可能產(chǎn)生Socketexception類的異常
se.printStackTrace();
System.exit(0);
}
butto
9、n1.setBackground(Color.magenta);
button1.setLabel("發(fā)送消息");
button1.setBounds(new Rectangle(374, 23, 88, 42));
public void actionPerformed(ActionEvent e){
button1_actionPerformed(e);
}
});
textField2.setBackground(SystemColor.activeCaptionBorder);
10、
textField2.setText("");
textField2.setBounds(new Rectangle(166, 21, 169, 29));
textArea1.setText("");
textArea1.setBounds(new Rectangle(46, 157, 382, 185));
contentPane.setBackground(new Color(113, 111, 159));
jButton1.setBackground(Color.pink);
jButton1.setBou
11、nds(new Rectangle(374, 86, 87, 42));
jButton1.setText("刷新頻道");
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane.add(label1, null);
contentPane.add(textField2, null);
contentPane.add(textField1, null);
12、 contentPane.add(label2, null);
contentPane.add(textArea1, null);
contentPane.add(jButton1, null);
contentPane.add(button1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if
13、(e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public void waitforpackets(){ //這個方法完成接受數(shù)據(jù)包的功能
while(true){
try{
byte[]array=new byte[100];
receivepacket=new DatagramPacket(array,array.length);
//創(chuàng)建接受數(shù)據(jù)包的緩沖
14、 receivesocket.receive(receivepacket);
//接受數(shù)據(jù)包
textArea1.append("\nfrom "+receivepacket.getAddress() +" : ");
byte data[]=receivepacket.getData();
String received=new String(data,0);
//將字節(jié)數(shù)組轉(zhuǎn)化為字符串
textAre
15、a1.append(received);
}
catch(IOException se){
//可能產(chǎn)生的IOException類異常
textArea1.append(se.toString() +"/n");
se.printStackTrace();
}
}
}
void button1_actionPerformed(ActionEvent e){
//這個方法完成發(fā)送數(shù)據(jù)包
String str =
16、 textField2.getText();
if(pareTo("")!=0){
try{
textArea1.append("\nto "+textField2.getText() +" : "+textField1.getText());
String s=textField1.getText();
byte data[]=new byte[100];
s.getBytes(0,s.length(),data,0);
sendpacket=new
17、DatagramPacket(data,s.length(),
InetAddress.getByName(textField2.getText()),5001);
//創(chuàng)建發(fā)送的數(shù)據(jù)包
sendsocket.send(sendpacket);
//發(fā)送數(shù)據(jù)包
}
catch(IOException exc){
textArea1.append(exc.toString() +
18、"\n");
exc.printStackTrace();
}
}
else
textArea1.setText("please input your friends IP first!");
}
JButton jButton1 = new JButton();
void jButton1_actionPerformed(ActionEvent e) {
textArea1.setText("");
}
}
2.chatapp類的代碼:
package
19、 chat;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2021
*
Company:
* @author not attributable
* @version 1.0
*/
public class chatApp {
boolean packFrame = false;
//Construct the application
public chatApp() {
chatF
20、rame frame = new chatFrame();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize
21、 = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame
22、.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
frame.waitforpackets();
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new chatApp();
}
}