网络通信的第一个元素:IP地址。通过IP地址,唯一的定位互联网上一台
* 主机
*InetAddress:位于java.net包下
*1.InetAddress用来代表IP地址。一个InetAddress的对象就代表着一个IP
*地址
*2.如何创建InetAddress的对象:getName(String host)
*3.getHostName()获取IP地址对应的域名。getHostAddress()获取IP地址
*
*
*
*
*/
/**
* A类网络的IP地址范围为1.0.0.1-127.255.255.254; B类网络的IP地址范围为:128.1.0.1-191.255.255.254;
* C类网络的IP地址范围为:192.0.1.1-223.255.255.254。
*
*
*/
public class TestInetAddress {
public static void main(String[] args) throws UnknownHostException {
// 创建一个InetAddress对象
InetAddress inet = InetAddress.getByName("www.beicai.com");
// inet = InetAddress.getByName("119.90.141.3");
System.out.println(inet);
System.out.println(inet.getHostName());
System.out.println(inet.getHostAddress());
System.out.println();
// 获取本机的IP
InetAddress inet1 = InetAddress.getLocalHost();
System.out.println(inet1);
System.out.println(inet1.getHostName());
System.out.println(inet1.getHostAddress());
}
}
public class TestInetAddress {
public static void main(String[] args) throws UnknownHostException {
// 创建一个InetAddress对象
InetAddress inet = InetAddress.getByName("www.beicai.com");
// inet = InetAddress.getByName("119.90.141.3");
System.out.println(inet);
System.out.println(inet.getHostName());
System.out.println(inet.getHostAddress());
System.out.println();
// 获取本机的IP
InetAddress inet1 = InetAddress.getLocalHost();
System.out.println(inet1);
System.out.println(inet1.getHostName());
System.out.println(inet1.getHostAddress());
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import org.junit.Test;
//TCP编程例一:客户端给服务端发送信息。服务端输出信息到控制台上
public class TestTCP {
// 客户端
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
try {
// 1.创建socket对象,通过构造器指明服务端的IP地址,及其
// 接收的端口号
socket = new Socket("127.0.0.1", 9090);
// 2.getOutputStream()发送数据,方法返回OutputStream
// 的对象
os = socket.getOutputStream();
// 3.具体的输出过程
os.write("你瞅啥?".getBytes());
} 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();
}
}
}
}
// 服务端
@Test
public void server() {
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);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// TODO: handle finally clause
// 5.关闭相应的流,Socket,ServerSocket的对象
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//客户端给服务端发送信息,服务端将信息打印到控制台上,同时发送回执
//给客户端
public class TestTCP2 {
// 客户端
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
InputStream is = null;
try {
socket = new Socket("10.114.19.237", 8989);
os = socket.getOutputStream();
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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();
}
}
}
}
// 服务端
@Test
public void server() {
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
OutputStream os = null;
try {
ss = new ServerSocket(8989);
s = ss.accept();
is = s.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);
}
os = s.getOutputStream();
os.write("瞅你咋地!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
// TODO: handle finally clause
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import org.junit.Test; import java.io.InputStream; import java.io.OutputStream; //TCP编程例三:从客户端发送文件给服务端,服务端保存到本地,并返回“发送成功” //给客户端 public class TestTCP3 { // 客户端 @Test public void client() { // 1.创建Socket的对象 Socket socket = null; // 2.从本地获取一个文件发送给服务端 OutputStream os = null; FileInputStream fis = null; // 3.接收来自于服务端的消息 InputStream is = null; try { socket = new Socket(InetAddress.getByName("127.0.0.1"), 9898); os = socket.getOutputStream(); fis = new FileInputStream(new File("1.jpg")); byte[] b = new byte[1024]; int len; while ((len = fis.read(b)) != -1) { os.write(b, 0, len); } socket.shutdownOutput(); 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); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 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(); } // 4.关闭相应的流和服务端的消息 if (is != null) { try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } if (os != null) { try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (fis != null) { try { fis.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(); } } } // 服务端 @Test public void server() { // 1.创建一个ServerSocket的对象 ServerSocket ss = null; // 2.调用accept()方法,返回一个Socket的对象 Socket s = null; // 3.将从客户端发送来的文件保存到本地 InputStream is = null; FileOutputStream fos = null; // 4.发送回执反馈给客户端 OutputStream os = null; try { ss = new ServerSocket(9898); s = ss.accept(); is = s.getInputStream(); fos = new FileOutputStream(new File("2.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() + "的文件"); os = s.getOutputStream(); os.write("头像上传成功".getBytes()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 5.关闭相应的流和Socket、ServerSocket的对象 if (os != null) { try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (s != null) { try { s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (ss != null) { try { ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }