InetAddress:代表ip地址,一個InetAddress的對象就代表一個IP地址
TCP編程例一:客戶端給服務端發送信息。服務端輸出此信息到控制檯上
客戶端:
package bank;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client {
public static void main (String[] args){
Socket socket =null;
OutputStream out =null;
try {
//1.創建一個Socket的對象,通過構造器指明服務端的ip地址,以及接受程序的端口號
socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
//2.使用getOutputStream()發送數據,方法返回OutputStream的對象
out = socket.getOutputStream();
//3.具體的輸出過程
out.write("我是客戶端,請多關照".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out !=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服務端
package bank;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
try {
//1.創建一個ServerSocket的對象,通過構造器指明自身的端口
ss = new ServerSocket(9090);
//2.調用其accept()方法,返回一個Socket對象
s = ss.accept();
//3.調用Socket對象的getInputStream()獲取一個從客戶端發送過來的輸入流
is = s.getInputStream();
//4.對獲取的輸入流進行操作
byte[] b = new byte[20];
int len;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.println(str);
}
System.out.println("收到來自於" + s.getInetAddress().getHostAddress() + "的連接");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
TCP編程例二:客戶端給服務端發送信息,服務端將信息打印到控制檯上,同時發送“已收到信息”給客戶端
服務端
package bank;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
OutputStream os = null;
try {
//1.創建一個ServerSocket的對象,通過構造器指明自身的端口
ss = new ServerSocket(9090);
//2.調用其accept()方法,返回一個Socket對象
s = ss.accept();
//3.調用Socket對象的getInputStream()獲取一個從客戶端發送過來的輸入流
is = s.getInputStream();
//4.對獲取的輸入流進行操作
byte[] b = new byte[20];
int len;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.println(str);
}
System.out.println("收到來自於" + s.getInetAddress().getHostAddress() + "的連接");
os = s.getOutputStream();
os.write("我收到client".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
客戶端
package bank;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client {
public static void main (String[] args){
Socket socket = null;
OutputStream os = null;
InputStream is = null;
try {
//1.創建一個Socket對象,通過構造器指明服務端的ip地址以及接受程序的端口號
socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
//2.getOutputStream:發送數據,方法返回OutputStream的對象
os = socket.getOutputStream();
//3.具體的輸出過程
os.write("我是客戶端".getBytes());
//shutdownOutput():執行此方法,顯式告訴服務器發送完畢,防止服務器持續等待
socket.shutdownOutput();
is = socket.getInputStream();
byte[] b = new byte[20];
int len;
while ((len = is.read(b))!=-1){
String str = new String(b,0,len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is !=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
代碼對比
客戶端,使用輸出流,輸入到服務端
socket.shutdownOutput();
is = socket.getInputStream();
byte[] b = new byte[20];
int len;
while((len = is.read(b)) != -1){
String str = new String(b,0,len);
System.out.print(str);
}
服務端:
os = s.getOutputStream();
os.write("我已收到你的情意".getBytes());
對於使用inputstream與outputstream的疑惑
1.對於socket而言
client向server發信息,使用outputStream,得到server的信息,使用inputStream
2.對於serverSocket
server向client發信息用outputStream(輸出),從客戶端得到信息就用inputstream(輸入)
基本原理:應用程序數據流向來說,服務端從客戶端,本地文件系統等其他地方得到inputStream都是讀入內存供應用程序使用
應用程序往科幻,我呢間,數據庫等寫出數據的話使用outputStream,也就是從內存中寫出,
假設讀取本地文件a寫入到b,就是講inputStream把a的內容讀到內存,然後outputStream寫入b
TCP編程例三:客戶端發送文件給服務端,服務端保存在本地,並返回"發送成功"給客戶端,並關閉相應的連接
客戶端
package bank;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
//處理異常,一般使用try-catch 只是爲了書寫方便
public class Client {
public static void main (String[] args) throws IOException {
//1.創建Socket對象
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9898);
//2.從本地獲取一個文件發送給服務端
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("C:/Users/Administrator/Desktop/1.jpg"));
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b))!=-1){
os.write(b,0,len);
}
socket.shutdownOutput();
//3.接受來自服務端的信息
InputStream is = socket.getInputStream();
byte[] b1 = new byte[1024];
int len1;
while ((len1=is.read(b1))!=-1){
String str = new String(b1,0,len1);
System.out.println(str);
}
//4.關閉相應的流和Socket對象
is.close();
os.close();
fis.close();
socket.close();
}
}
服務端
package bank;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
//1.創建一個ServerSocket的對象
ServerSocket ss = new ServerSocket(9898);
//2.調用其accept()方法,返回一個Socket的對象
Socket s = ss.accept();
//3.將從客戶端發送來的信息保存在本地
InputStream is = s.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("C:/Users/Administrator/Desktop/1.jpg"));
byte[] b = new byte[1024];
int len;
while ((len = is.read(b))!=-1){
fos.write(b,0,len);
}
System.out.println("收到來自於"+s.getInetAddress().getHostAddress()+"的文件");
//4.發送"接受成功"的信息反饋給客戶端
OutputStream os = s.getOutputStream();
os.write("你發送的圖片接受成功".getBytes());
//5.關閉相應的流和Socket以及ServerSocket的對象
os.close();
fos.close();
is.close();
s.close();
ss.close();
}
}