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();
}
}