Day15:JAVA网络编程下
客户端:
package test;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class WebClientTest {
public static void main(String[] args) {
InetAddress inetAddress = null;
Socket socket = null;
FileInputStream fis = null;
OutputStream os = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
inetAddress = InetAddress.getByName("127.0.0.1");
socket = new Socket(inetAddress, 6426);
fis = new FileInputStream("dnfgirl1.jfif");
os = socket.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
//告诉服务器文件已传输完毕
socket.shutdownOutput();
//知晓服务器有没有接收到文件
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
while ((len=is.read(buffer2))!=-1){
baos.write(buffer2,0,len);
}
System.out.println(baos.toString());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(baos!=null){
try {
baos.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(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务端:
package test;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class WebServerTest {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(6426);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("dnfgirl1clone.jfif"));
OutputStream os = socket.getOutputStream();) {
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//告诉客户端服务器已接收到文件
os.write("我已接收到文件".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Tomcat
服务端:
-
自定义 S
-
tomcat服务器 S :JAVA后台开发
客户端:
-
自定义 C
-
浏览器 B
注
-
.bat文件是windows下执行的
-
.sh文件是linux下执行的
-
new String(packet.getData(),0,packet.getLength())
//出错的原因是导入了com.sun.org.apache.xpath.internal.operations.String
UDP
DatagramPacket、DatagramSocket
客户端:
-
建立一个Socket
-
建个包
-
发送包
package test;
import java.io.IOException;
import java.net.*;
public class TestUDPClient {
public static void main(String[] args) {
try (DatagramSocket datagramSocket = new DatagramSocket()) {
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
int port = 6426;
String express = "快递";
DatagramPacket packet = new DatagramPacket(express.getBytes(), 0, express.getBytes().length, inetAddress, port);
datagramSocket.send(packet);
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务端(接收端):
-
开放端口
-
接收数据
package test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TestUDPServer {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(6426)) {
byte[] express = new byte[1024];
DatagramPacket packet = new DatagramPacket(express, 0, express.length);
socket.receive(packet); //阻塞接收
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
聊天实现
单方面聊天
A端
package test.chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class TestSender {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(4423);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));) {
while (true){
String str = bufferedReader.readLine();
byte[] data = str.getBytes();
DatagramPacket packet = new DatagramPacket(data,0,data.length,new InetSocketAddress("127.0.0.1",6426));
socket.send(packet);
if(str.trim().equals("bye")){
break;
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
B端
package test.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TestReceiver {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(6426)) {
while (true){
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);
String str = new String(packet.getData());
System.out.println(str);
if(str.trim().equals("bye")){
break;
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
同时聊天
发送端
package test.chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class TestSenderPort implements Runnable{
DatagramSocket socket = null;
DatagramPacket packet = null;
BufferedReader bufferedReader = null;
private int fromIp;
private String toIp;
private int toPort;
public TestSenderPort(int fromIp, String toIp, int toPort) {
this.fromIp = fromIp;
this.toIp = toIp;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromIp);
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true){
String str = bufferedReader.readLine();
byte[] data = str.getBytes();
packet = new DatagramPacket(data,0,data.length,new InetSocketAddress(toIp,toPort));
socket.send(packet);
if(str.trim().equals("bye")){
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
socket.close();
}
}
接收端
package test.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TestReceiverPort implements Runnable{
DatagramSocket socket = null;
DatagramPacket packet = null;
private int fromPort;
private String name;
public TestReceiverPort(int fromPort, String name) {
this.fromPort = fromPort;
this.name = name;
try {
socket = new DatagramSocket(fromPort);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
byte[] data = new byte[1024];
packet = new DatagramPacket(data,0,data.length);
socket.receive(packet);
String str = new String(packet.getData());
System.out.println(name+":"+str);
if(str.trim().equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
聊天端
package test.chat;
public class TestStudent {
public static void main(String[] args) {
new Thread(new TestSenderPort(4567,"127.0.0.1",6426)).start();
new Thread(new TestReceiverPort(4423,"宝宝")).start();
}
}
package test.chat;
public class TestTeacher {
public static void main(String[] args) {
new Thread(new TestSenderPort(5678,"localhost",4423)).start();
new Thread(new TestReceiverPort(6426,"宝贝")).start();
}
}
URL
统一资源定位符:定位资源的,定位互联网上的某一个资源
DNS域名解析:例如把www.baidu.com解析为一个IP
协议://IP地址:端口号/项目名/资源
获取资源
package test.chat;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class TestURLConnection {
public static void main(String[] args) {
HttpURLConnection huc = null;
InputStream is = null;
FileOutputStream fos = null;
try {
//获取链接
URL url = new URL("http://localhost:8080/qianjun/qianjun.txt");
huc = (HttpURLConnection)url.openConnection();
is = huc.getInputStream();
fos = new FileOutputStream("qianjun.txt");
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//断开链接
if(huc!=null){
huc.disconnect();
}
}
}
}

浙公网安备 33010602011771号