?? serverudp.java.bak
字號(hào):
import java.io.*;
import java.net.*;
import java.util.*;
/**類ServerDatagram是數(shù)據(jù)報(bào)通信的服務(wù)器端程序,該程序用于實(shí)現(xiàn):
當(dāng)有一個(gè)客戶端連接到端口上時(shí),就新建一個(gè)線程,該線程讀取當(dāng)前
路徑下thirdFile.txt文件中的內(nèi)容,每次讀取一行,將讀取的內(nèi)容
通過(guò)數(shù)據(jù)報(bào)傳送到客戶端并在客戶端顯示出來(lái)*/
public class ServerUDP{
public static void main(String[] args){
try{
/*生成一個(gè)ServerDataThread類的對(duì)象,在ServerDataThread類中實(shí)現(xiàn)了數(shù)據(jù)報(bào)通信的服務(wù)器端處理*/
ServerDataThread threadObj = new ServerDataThread();
/*ServerDataThread類是Thread類的子類,因此threadObj也是一個(gè)線程對(duì)象*/
threadObj.start();
}catch(IOException e){}
}
}
/**ServerDataThread類是Thread類的子類,用于進(jìn)行數(shù)據(jù)報(bào)通信*/
class ServerDataThread extends Thread{
/*聲明一個(gè)DatagramSocket對(duì)象,初始值為null,用于數(shù)據(jù)報(bào)的接收和發(fā)送*/
protected DatagramSocket socketObj = null;
/*聲明一個(gè)BufferedReader類的對(duì)象,初值為null,用于創(chuàng)建一個(gè)輸入流*/
protected BufferedReader buffObj = null;
/*聲明一個(gè)布爾變量existData,用于判斷輸入流中是否還有數(shù)據(jù)*/
protected boolean existData = true;
/**構(gòu)造方法ServerDataThread()實(shí)例化DatagramSocket對(duì)象,并創(chuàng)建輸入流*/
public ServerDataThread() throws IOException{
/*進(jìn)行文件例外處理,因?yàn)橛锌赡芪募淮嬖?/
try{
/*聲明一個(gè)FileReader對(duì)象,這樣服務(wù)器端就可以對(duì)當(dāng)前路徑下的thirdFile.txt文件進(jìn)行讀取操作*/
FileReader fileObj = new FileReader("thirdFile.txt");
/*實(shí)例化BufferedReader對(duì)象,以提供文件讀取的效率*/
buffObj = new BufferedReader(fileObj);
}catch(FileNotFoundException e){
System.err.println("文件對(duì)象不存在");
}
}
/**方法run()實(shí)現(xiàn)數(shù)據(jù)報(bào)通信*/
public void run(){
/*while循環(huán)用于判斷當(dāng)前文件流中是否還存在數(shù)據(jù)*/
while(existData)
{
try{
/*聲明一個(gè)byte類型的數(shù)組buf,用于存放數(shù)據(jù)報(bào)中的數(shù)據(jù)*/
byte[] buf=new byte[256];
/*聲明一個(gè)字符串變量tempStr*/
String tempStr = "";
/*聲明一個(gè)字符串變量totalStr,用于存儲(chǔ)文件內(nèi)容*/
String totalStr = "";
socketObj = new DatagramSocket();
/*根據(jù)文件流對(duì)象是否存在進(jìn)行不同的處理*/
if(buffObj == null)//文件對(duì)象不存在
tempStr = "This file Object doesn't exist!";
else//文件對(duì)象存在
{
try{
/*while循環(huán)語(yǔ)句獲得文件中的內(nèi)容*/
while((tempStr = buffObj.readLine())!=null)
{
totalStr += tempStr+"\n";
}
/*沒(méi)有數(shù)據(jù)時(shí),就將文件流關(guān)閉*/
buffObj.close();
/*將布爾變量賦值為false*/
existData = false;
/*給字符串變量賦值,表示文件流到達(dá)結(jié)尾處*/
tempStr = "No more data in file.This is the end";
}catch(IOException e){
tempStr = "IOException occurred in server.";
}
}
/*將字符串轉(zhuǎn)換成byte型*/
buf = totalStr.getBytes();
/*設(shè)置數(shù)據(jù)報(bào)對(duì)應(yīng)的IP地址,這里是本機(jī)地址*/
InetAddress ipAddress=InetAddress.getByName("127.0.0.1");
/*獲得數(shù)據(jù)報(bào)的端口號(hào)*/
int portObj=3325;
/*重新實(shí)例化DatagramPacket對(duì)象,用于發(fā)送數(shù)據(jù)報(bào)*/
DatagramPacket packetObj=new DatagramPacket(buf,buf.length,ipAddress,portObj);
/*發(fā)送數(shù)據(jù)報(bào)*/
socketObj.send(packetObj);
}catch(IOException e){
/*如果出現(xiàn)例外,對(duì)例外進(jìn)行跟蹤,并輸出跟蹤信息*/
e.printStackTrace();
/*出現(xiàn)例外,將布爾變量existData置為false,以控制while循環(huán)*/
existData = false;
}
}
/*將數(shù)據(jù)報(bào)關(guān)閉*/
socketObj.close();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -