Socket 是一個通信鏈路的端點,它提供給應(yīng)用程序互相訪問的接口,它處在java.net包下.
InetAddress 類:IP地基于TCP的Socket編程用到的類
測試InetAdress的上述函數(shù)
package com.qf.demo;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* 測試本機 的地址
* localhost
* 127.0.0.1
*
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
try {
// 得到本機的信息
InetAddress inetAddress = InetAddress.getLocalHost();
// 得到主機名
String name = inetAddress.getHostName();
System.out.println(name);
// 得到主機的ip地址
String address = inetAddress.getHostAddress();
System.out.println(address);
InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2.getHostName());
System.out.println(inetAddress2.getHostAddress());
InetAddress inetAddress3 =InetAddress.getByName("localhost");
// 如果獲取不到主機名 主機名是展示的是ip地址
System.out.println(inetAddress3.getHostName());
System.out.println(inetAddress3.getHostAddress());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Socket應(yīng)用
- 客戶端發(fā)送數(shù)據(jù)給服務(wù)端
- 1 創(chuàng)建Socket
- 2 準備發(fā)送的數(shù)據(jù)
- 3 將數(shù)據(jù)放到socket中
- 4 (調(diào)配物流車, 物流車送貨)
- 5 關(guān)閉socket
*注意: 先運行 服務(wù)端 在運行客戶端
客戶端代碼
public class Client {
public static void main(String[] args) {
//1 創(chuàng)建Socket
Socket socket = null;
OutputStream os =null;
try {
System.out.println("客戶端起來了");
socket = new Socket("127.0.0.1", 6666);
// 2 準備發(fā)送的數(shù)據(jù)
String string = "hello world";
// 3 將數(shù)據(jù)放到socket中
os = socket.getOutputStream();
// 將數(shù)據(jù)交給快遞員寫給快點點
os.write(string.getBytes());
os.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(os!=null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
- 服務(wù)端接收數(shù)據(jù)
- 1 創(chuàng)建服務(wù)端的快遞點
- 2 等待接收數(shù)據(jù)
- 3 接收到數(shù)據(jù) , 就可以從快遞點拿出數(shù)據(jù)
- 4 關(guān)socket
服務(wù)器端代碼
public class Server {
public static void main(String[] args) {
// 1 創(chuàng)建服務(wù)端的快遞點
ServerSocket serverSocket =null;
Socket socket = null;
InputStream is =null;
try {
System.out.println("服務(wù)端起來了");
serverSocket = new ServerSocket(6666);
// 2 等待接收數(shù)據(jù)
socket = serverSocket.accept();// 如果沒有接收到數(shù)據(jù), 阻塞程序執(zhí)行, 直到接收到數(shù)據(jù)
// 3 接收到數(shù)據(jù) , 就可以從快遞點拿出數(shù)據(jù)
is = socket.getInputStream();
byte[] bs= new byte[1024];
int num = is.read(bs);
String string = new String(bs, 0, num);
System.out.println("服務(wù)端接收到的數(shù)據(jù)是: "+string);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
當(dāng)我們運行服務(wù)器的時候服務(wù)器會等著接受Socket,如果沒有則進入阻塞狀態(tài)
當(dāng)我們運行客戶端的時候?qū)?shù)據(jù)給Socket那么服務(wù)端就會由阻塞進入就緒狀態(tài),然后進入執(zhí)行狀態(tài)執(zhí)行下面的代碼
那么服務(wù)端可不可以和客戶端進行互相通信呢
下面的例子就是互相通信的
客戶端
package com.qf.demo4;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import com.qf.demo3.Util;
/**
* 控制臺循環(huán)輸入數(shù)據(jù) 發(fā)送給 服務(wù)端
*
* @author Administrator
*
*/
public class Client {
public static void main(String[] args) {
// 1 創(chuàng)建快遞點
Socket socket = null;
OutputStream os = null;
InputStream is = null;
try {
socket = new Socket("10.0.143.51", 8888);
// 2 準備要發(fā)送的數(shù)據(jù)
Scanner scanner = new Scanner(System.in);
// 3 獲得快遞員
os = socket.getOutputStream();
is = socket.getInputStream();
while (true) {
String data = scanner.next();
os.write(data.getBytes());
os.flush();
if ("over".equals(data)) {
break;
}
// 收到回信
byte[] bs = new byte[1024];
int num = is.read(bs);
String reault = new String(bs, 0, num);
System.out.println("服務(wù)器回復(fù)的數(shù)據(jù)是 : " + reault);
if ("over".equals(reault)) {
break;
}
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
Util.closed(null, socket, is, os);
}
}
}
服務(wù)端
package com.qf.demo4;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import com.qf.demo3.Util;
public class Server {
public static void main(String[] args) {
// 1 創(chuàng)建 服務(wù)端的socket
ServerSocket serverSocket = null;
Socket socket =null;
InputStream is =null;
OutputStream os = null;
try {
serverSocket = new ServerSocket(8888);
// 2 等待接收客戶端的數(shù)據(jù)
socket = serverSocket.accept();
is = socket.getInputStream();
os = socket.getOutputStream();
Scanner scanner = new Scanner(System.in);
// 3 讀取信息
while(true){
byte[] bs = new byte[1024];
int num = is.read(bs);
String string = new String(bs, 0, num);
System.out.println("客戶端發(fā)送了: "+string);
if("over".equals(string)){
break;
}
// 以下是回復(fù)數(shù)據(jù)
String result = scanner.next();
os.write(result.getBytes());
os.flush();
if("over".equals(result)){
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
Util.closed(serverSocket, socket, is, os);
}
}
}