IP
package interweb;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestInterAddress {
public static void main(String[] args) throws UnknownHostException {
InetAddress inetAddress1= InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress2= InetAddress.getByName("localhost");
System.out.println(inetAddress2);
InetAddress inetAddress3= InetAddress.getLocalHost();
System.out.println(inetAddress3);
InetAddress inetAddress4= InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress4);
System.out.println(inetAddress4.getAddress());
System.out.println(inetAddress4.getCanonicalHostName());
System.out.println(inetAddress4.getHostAddress());
System.out.println(inetAddress4.getHostName());
}
}
TCP
一定要先开启服务器,再开启客户端
TCP实现聊天
package interweb.demo1;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServer {
public static void main(String[] args) {
ServerSocket serverSocket=null;
Socket accept=null;
InputStream is=null;
ByteArrayOutputStream byteArrayOutputStream=null;
try {
serverSocket= new ServerSocket(9999);
while (true){
accept=serverSocket.accept();
is= accept.getInputStream();
byteArrayOutputStream=new ByteArrayOutputStream();
byte[] buffer= new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
byteArrayOutputStream.write(buffer,0,len);
}
System.out.println(byteArrayOutputStream.toString());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (byteArrayOutputStream!=null){
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (accept!=null){
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package interweb.demo1;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TestClient {
public static void main(String[] args) {
Socket socket=null;
OutputStream os=null;
try {
InetAddress inetAddress=InetAddress.getByName("127.0.0.1");
int port=9999;
socket= new Socket(inetAddress,port);
os= socket.getOutputStream();
os.write("你好,欢迎学习狂神说Java".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
开启客户端 再查看服务器端
TCP实现文件上传
先将文件放入文件夹
package interweb.demo2;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TestService {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(9000);
Socket socket= serverSocket.accept();
InputStream inputStream= socket.getInputStream();
OutputStream os= new FileOutputStream( new File("receive.JPG"));
byte[] buffer= new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
os.write(buffer,0,len);
}
os.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
package interweb.demo2;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TestClient {
public static void main(String[] args) throws Exception {
Socket socket= new Socket(InetAddress.getByName("127.0.0.1"),9000);
OutputStream os= socket.getOutputStream();
FileInputStream fin= new FileInputStream(new File("2022.JPG"));
byte[] buffer=new byte[1024];
int len;
while ((len=fin.read(buffer))!=-1){
os.write(buffer,0,len);
}
os.close();
fin.close();
socket.close();
}
}
UDP
UDP不需要等待服务器的开启
UDP消息发送
package interweb.demo3;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UdpClient {
public static void main(String[] args) throws Exception {
DatagramSocket socket=new DatagramSocket();
String msg="你好啊,服务器!";
InetAddress localhost=InetAddress.getByName("127.0.0.1");
int port=9090;
DatagramPacket packet=new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
socket.send(packet);
socket.close();
}
}
package interweb.demo3;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpService {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer= new byte[1024];
DatagramPacket packet=new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
UDP聊天实现
package interweb.chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
public class UdpSender {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String data=bufferedReader.readLine();
byte[] datas=data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
socket.send(packet);
socket.close();
}
}
package interweb.chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpReceiver {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while (true){
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
byte[] data = packet.getData();
String msg=new String(data,0,data.length);
System.out.println(msg);
if (msg.equals("bye")){
break;
}
}
socket.close();
}
}
开始发送 接收端不断接收
UDP多线程在线咨询
package interweb.chat2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TalkSend implements Runnable {
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort, String toIP, int toPort) throws Exception {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
}
@Override
public void run() {
while (true) {
try {
String data = reader.readLine();
byte[] bytes = data.getBytes();
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress(this.toIP, this.toPort));
socket.send(packet);
if (data.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
package interweb.chat2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TalkReceive implements Runnable{
DatagramSocket socket=null;
private int port;
private String msgfrom;
public TalkReceive(int port,String msgfrom) throws Exception {
this.port = port;
this.msgfrom=msgfrom;
socket=new DatagramSocket(port);
}
@Override
public void run() {
while (true){
try {
byte[] container=new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);
byte[] data= packet.getData();
String msg=new String(data,0,data.length);
System.out.println(msgfrom+"说:"+msg);
if (msg.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
package interweb.chat2;
public class TalkStudent {
public static void main(String[] args) throws Exception {
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
package interweb.chat2;
public class TalkTeacher {
public static void main(String[] args) throws Exception{
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
UDP下载网络资源
package interweb.URL;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class UrlDown {
public static void main(String[] args) throws Exception{
URL url = new URL("https://dl.stream.qqmusic.qq.com/C4000012Ez0a1tFcOI.m4a?guid=4827791422&vkey=C04043FF630746C952BAF949B2771DFF169AFDAD21282D0130E3E7E5DB2E4C95D06F3F318BC19FE18ED3187713AB89ED3CB997569CDDEA8D&uin=1796511432&fromtag=66");
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream outputStream = new FileOutputStream("7.m4a");
byte[] buffer= new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
outputStream.close();
inputStream.close();
urlConnection.disconnect();
}
}
|