要想實(shí)現(xiàn)網(wǎng)絡(luò)傳輸,需要考慮的問題有哪些计贰?
1.1 如何才能準(zhǔn)確的定位網(wǎng)絡(luò)上的一臺(tái)主機(jī)漠酿?
1.2 如何才能進(jìn)行可靠的赫粥、高效的數(shù)據(jù)傳輸?
簡單的說胃夏,你要知道數(shù)據(jù)的目的地(IP)轴或,你要用什么渠道傳遞數(shù)據(jù)(TCP/UDP);
java如何實(shí)現(xiàn)的網(wǎng)絡(luò)通信
使用IP地址---定位一臺(tái)主機(jī) 使用端口號(hào)---定位一個(gè)應(yīng)用
1).如何創(chuàng)建一個(gè)InetAddress的對(duì)象仰禀?getByName(""); 比如:InetAddress inet = InetAddress.getByName("192.168.10.165");
2).如何獲取本機(jī)的一個(gè)InetAddress的對(duì)象照雁?getLocalHost()
域名:getHostName() ip:getHostAddress()
對(duì)應(yīng)有協(xié)議
對(duì)于傳輸層而言:分為TCP UDP (了解)
TCP的編程: Socket ServerSocket
例子:
1.客戶端發(fā)送內(nèi)容給服務(wù)端,服務(wù)端將內(nèi)容打印到控制臺(tái)上答恶。
2.客戶端發(fā)送內(nèi)容給服務(wù)端饺蚊,服務(wù)端給予反饋。
3.從客戶端發(fā)送文件給服務(wù)端悬嗓,服務(wù)端保存到本地污呼。并返回“發(fā)送成功”給客戶端。并關(guān)閉相應(yīng)的連接包竹。
代碼實(shí)現(xiàn):客戶端
public class TestTCP{
@Test
public void client () {
Socket socket = null;
OutputStream outputSteam = null;
FileINputStream fileInputStream = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"),9999);
fileInputStream = new FileInputStream(new File("1.jpg"));
byte[] b =new byte[1024];
int len;
outputSream = socket.getOutputStream();
while ((len = fileInputStream.read(b)) != -1){
outputStream.write(b,0,len);
}
socket.shutdownOutput();//停止輸出
inputStream = socket.getInputStream();
byte[] b1 = new byte[1024];
int len1;
while ((len1 =inputStream.read(b1) ) != -1){
String str = new String(b,0,len1);
System.out.println(str);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
代碼實(shí)現(xiàn):服務(wù)端
@Test
public void server(){
ServerSocket socket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
FileOutputStream fileOutputStream = null;
Socket s = null;
try {
socket = new ServerSocket(9991);
s = socket.accept();
fileOutputStream = new FileOutputStream(new File("2.jpg"));
byte[] b = new byte[1024];
int len;
inputStream = s.getInputStream();
while ((len = inputStream.read(b)) != -1){
fileOutputStream.write(b,0,len);
}
outputStream = s.getOutputStream();
outputStream.write("我已經(jīng)收到你發(fā)的文件了".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (s != null){
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}