- IP
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Hello {
public static void main(String[] args) {
try {
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress);
InetAddress inetAddress2 = InetAddress.getByName("localhost");
System.out.println(inetAddress);
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println(inetAddress);
InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress1);
System.out.println(inetAddress1.getCanonicalHostName());
System.out.println(inetAddress1.getHostAddress());
System.out.println(inetAddress2.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
- 一些命令
import java.net.InetSocketAddress;
public class Hello {
public static void main(String[] args) {
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1",8080);
InetSocketAddress inetSocketAddress1 = new InetSocketAddress("localhost",8080);
System.out.println(inetSocketAddress);
System.out.println(inetSocketAddress1);
System.out.println(inetSocketAddress.getAddress());
System.out.println(inetSocketAddress.getHostName());
System.out.println(inetSocketAddress.getPort());
}
}
package kssManyThread;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class day1 {
public static void main(String[] args) throws IOException {
InetAddress ServerIp = InetAddress.getByName("127.0.0.1");
int port = 9999;
Socket socket = new Socket(ServerIp, port);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("你好,欢迎狂神说Java".getBytes(StandardCharsets.UTF_8));
if(outputStream!=null){
outputStream.close();
}
if (socket!=null){
socket.close();
}
}
}
package kssManyThread;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class day2 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
if (baos!=null){
baos.close();
}
if (is!=null){
is.close();
}
if (socket!=null) {
socket.close();
}
if (serverSocket!=null) {
serverSocket.close();
}
}
}
package kssManyThread;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class day1 {
public static void main(String[] args) throws IOException {
InetAddress ServerIp = InetAddress.getByName("127.0.0.1");
int port = 9999;
Socket socket = new Socket(ServerIp, port);
OutputStream outputStream = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("E:\\idea\\WorkerSystem\\src\\Worker\\t01369c7b27429f28cb.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len=fis.read())!=-1){
outputStream.write(buffer,0,len);
}
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte [] buffer2 = new byte[2014];
int len2;
while((len2=inputStream.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
if(outputStream!=null){
outputStream.close();
}
if (socket!=null){
socket.close();
}
}
}
package kssManyThread;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class day2 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("re.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
OutputStream os = socket.getOutputStream();
os.write("我接受完毕了".getBytes(StandardCharsets.UTF_8));
if (fos!=null){
fos.close();
}
if (is!=null){
is.close();
}
if (socket!=null) {
socket.close();
}
if (serverSocket!=null) {
serverSocket.close();
}
}
}
kage kssManyThread;
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
public class day1 {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket();
String message = "你好啊,服务器";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(message.getBytes(StandardCharsets.UTF_8), 0, message.getBytes(StandardCharsets.UTF_8).length,localhost,port );
socket.send(packet);
socket.close();
}
}
package kssManyThread;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class day2 {
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(new String(packet.getData()));
socket.close();
}
}
package kssManyThread;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class day2 implements Runnable{
DatagramSocket socket = null;
private int port;
private String msgFrom;
public day2(int port,String msgFrom) throws SocketException {
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 receiveData = new String(data, 0, data.length);
System.out.println(msgFrom+":"+receiveData);
if (receiveData.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
package kssManyThread;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.nio.charset.StandardCharsets;
public class day1 implements Runnable {
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public day1(int fromPort, String toIP, int toPort) throws SocketException {
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[] datas = data.getBytes(StandardCharsets.UTF_8);
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet);
if(data.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
package kssManyThread;
import java.net.SocketException;
public class Talk {
public static void main(String[] args) throws SocketException {
new Thread(new day1(7777,"localhost",9999)).start();
new Thread(new day2(8888,"老师")).start();
}
}
package kssManyThread;
import java.net.SocketException;
public class Talk2 {
public static void main(String[] args) throws SocketException {
new Thread(new day1(5555,"localhost",8888)).start();
new Thread(new day2(9999,"学生")).start();
}
}
package kssManyThread;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class last {
public static void main(String[] args) throws IOException {
URL url = new URL("");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("图标.png");
byte[] buffer = new byte[1024];
int len;
while((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}