网络编程的主要解决两个问题
链接到哪一台主机 => 输出双方的地址
ip => 哪一台主机
端口号 => 哪一个应用
另:域名解析(DNS) => 域名 通过DSN 解析成ip地址
找到主机以后,如何进行高效安全的数据传输 => 一定的规则(TCP/IP,UCP)
测试 InetAddress 类
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestInetAddress {
public static void main(String[] args) {
InetAddress inet;
try {
inet = InetAddress.getByName("www.baidu.com");
System.out.println(inet);
System.out.println(inet.getHostAddress());
System.out.println(inet.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
比如 观看视频的资源,为了保证视频的不卡顿,用的就是UDP协议。
无论使用哪种协议进行传输,都不能离开socket。
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
public class tcpTest {
@Test
public void serverTest() {
ServerSocket ss = null;
Socket s = null;
InputStream in = null;
try {
// 服务器端服務器socket
// 指明自身的端口號
ss = new ServerSocket(5000);
// 调用服务器端socket的接受方法
// 获得一个socket 对象
s = ss.accept();
// 通过socket对象获得,从客户端过来的输入流
in = s.getInputStream();
byte[] b = new byte[80];
int len=0;
while((len = in.read(b)) != -1){
String str = new String(b,0,len);
System.out.println("服务端收到:" + str);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in != null){
try {
in.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();
}
}
}
}
@Test
public void clientTest() {
Socket s = null;
OutputStream os = null;
try {
// 客戶端的socket
// 指明服務器的ip地址 以及 端口
s = new Socket(InetAddress.getLocalHost(), 5000);
// 獲得流 發送數據
os = s.getOutputStream();
// 待發送數據的具體編輯
// byte[] b;
os.write("Hello,服務器,我是客戶端".getBytes());
} 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(s!=null){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
tcp版 服务器 客户端 实现
顺便 如果 服务器端已经 启动
也可以通过windows下的,telnet ip号 端口号 来访问。
两端可交互版
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
public class tcpTest2 {
@Test
public void serverTest() {
ServerSocket ss = null;
Socket s = null;
InputStream in = null;
OutputStream out = null;
try {
// 服务器端服務器socket
// 指明自身的端口號
ss = new ServerSocket(5000);
// 调用服务器端socket的接受方法
// 获得一个socket 对象
s = ss.accept();
// 通过socket对象获得,从客户端过来的输入流
in = s.getInputStream();
byte[] b = new byte[80];
int len=0;
while((len = in.read(b)) != -1){
String str = new String(b,0,len);
System.out.println("服务端收到:" + str);
}
System.out.println("收到来自客户端 " + s.getInetAddress().getHostName() + " " + s.getInetAddress().getHostAddress());
// 华丽丽的分割线
// 服务器端 收到信息以后 给予 客户端的反馈信息
out = s.getOutputStream();
out.write("Hello 客户端,我是服务器,我已经收到你的问候,吼吼~~".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(in != null){
try {
in.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();
}
}
}
}
@Test
public void clientTest() {
Socket s = null;
OutputStream os = null;
InputStream is = null;
try {
// 客戶端的socket
// 指明服務器的ip地址 以及 端口
s = new Socket(InetAddress.getLocalHost(), 5000);
// 獲得流 發送數據
os = s.getOutputStream();
// 待發送數據的具體編輯
// byte[] b;
os.write("Hello,服務器,我是客戶端".getBytes());
// 显示告诉服务端 我已输出完毕
s.shutdownOutput();
// 又是华丽丽的分割线
is = s.getInputStream();
byte[] b = new byte[80];
int len;
while((len=is.read(b))!=-1){
System.out.println(new String(b,0,len));
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 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(os!=null){
try {
os.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();
}
}
}
}
}
文件传输版
1.服务端读取图片并发送给客户端,客户端保存图片到本地。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
public class tcpTest3File {
@Test
public void serverTest() {
ServerSocket ss = null;
Socket s = null;
InputStream in = null;
OutputStream out = null;
FileOutputStream fos = null;
try {
// 服务器端服務器socket
// 指明自身的端口號
ss = new ServerSocket(5000);
// 调用服务器端socket的接受方法
// 获得一个socket 对象
s = ss.accept();
// 通过socket对象获得,从客户端过来的输入流
in = s.getInputStream();
fos = new FileOutputStream(new File("2.jpg"));
byte[] b = new byte[1024];
int len=0;
while((len = in.read(b)) != -1){
// String str = new String(b,0,len);
// System.out.println("服务端收到:" + str);
fos.write(b,0,len);;
}
System.out.println("收到来自客户端 " + s.getInetAddress().getHostName() + " " + s.getInetAddress().getHostAddress());
// 华丽丽的分割线
// 服务器端 收到信息以后 给予 客户端的反馈信息
out = s.getOutputStream();
out.write("Hello 客户端,我是服务器,我已经收到你的问候,吼吼~~".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in != null){
try {
in.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();
}
}
}
}
@Test
public void clientTest() {
Socket s = null;
OutputStream os = null;
FileInputStream fis = null;
InputStream is = null;
try {
// 客戶端的socket
// 指明服務器的ip地址 以及 端口
s = new Socket(InetAddress.getLocalHost(), 5000);
// 獲得流 發送數據
os = s.getOutputStream();
// 待發送數據的具體編輯
// byte[] b;
// os.write("Hello,服務器,我是客戶端".getBytes());
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);
}
// 显示告诉服务端 我已输出完毕
s.shutdownOutput();
// 又是华丽丽的分割线
is = s.getInputStream();
byte[] b1 = new byte[80];
int len1;
while((len1=is.read(b1))!=-1){
System.out.println(new String(b1,0,len1));
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(s!=null){
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.客户端给服务端发送文本,服务端会将文本转成大写在返回给客户端。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
public class tcpTest3File {
@Test
public void serverTest() {
ServerSocket ss = null;
Socket s = null;
InputStream in = null;
OutputStream out = null;
FileOutputStream fos = null;
try {
// 服务器端服務器socket
// 指明自身的端口號
ss = new ServerSocket(5000);
// 调用服务器端socket的接受方法
// 获得一个socket 对象
s = ss.accept();
// 通过socket对象获得,从客户端过来的输入流
in = s.getInputStream();
fos = new FileOutputStream(new File("aA.txt"));
byte[] b = new byte[1024];
int len=0;
String newS = "";
while((len = in.read(b)) != -1){
// String str = new String(b,0,len);
// System.out.println("服务端收到:" + str);
String str = new String(b,0,len);
newS = str.toUpperCase();
fos.write(newS.getBytes(),0,len);;
}
System.out.println("收到来自客户端 " + s.getInetAddress().getHostName() + " " + s.getInetAddress().getHostAddress());
// 华丽丽的分割线
// 服务器端 收到信息以后 给予 客户端的反馈信息
out = s.getOutputStream();
out.write("Hello 客户端,我是服务器,我已经收到你的问候,吼吼~~".getBytes());
out.write(newS.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in != null){
try {
in.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();
}
}
}
}
@Test
public void clientTest() {
Socket s = null;
OutputStream os = null;
FileInputStream fis = null;
InputStream is = null;
try {
// 客戶端的socket
// 指明服務器的ip地址 以及 端口
s = new Socket(InetAddress.getLocalHost(), 5000);
// 獲得流 發送數據
os = s.getOutputStream();
// 待發送數據的具體編輯
// byte[] b;
// os.write("Hello,服務器,我是客戶端".getBytes());
fis = new FileInputStream(new File("a.txt"));
byte[] b = new byte[1024];
int len;
while((len=fis.read(b))!=-1){
os.write(b,0,len);
}
// 显示告诉服务端 我已输出完毕
s.shutdownOutput();
// 又是华丽丽的分割线
is = s.getInputStream();
byte[] b1 = new byte[80];
int len1;
while((len1=is.read(b1))!=-1){
System.out.println(new String(b1,0,len1));
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(s!=null){
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}