问题:Java面试知识点:网络编程
答案:
1.InetAddress
代码如下:
package com.xy;import java.net.InetAddress;
import java.net.UnknownHostException;/*** @ProjectName: day01* @Package: com.xy* @ClassName: test01* @Author: 杨路恒* @Description:* @Date: 2021/9/1 0001 11:31* @Version: 1.0*/
public class test01 {public static void main(String[] args) throws UnknownHostException {InetAddress name = InetAddress.getByName("WIN-S5E6V36PP3P");String hostAddress = name.getHostAddress();String hostName = name.getHostName();System.out.println(name);System.out.println(hostName);System.out.println(hostAddress);}
}
2.UDP
代码如下:
package com.xy;import java.io.IOException;
import java.net.*;/*** @ProjectName: day01* @Package: com.xy* @ClassName: test02* @Author: 杨路恒* @Description:* @Date: 2021/9/1 0001 11:39* @Version: 1.0*/
public class test02 {public static void main(String[] args) throws IOException {DatagramSocket ds=new DatagramSocket();byte[] bytes="杨大大".getBytes();int length=bytes.length;InetAddress address = InetAddress.getByName("192.168.65.1");
// InetAddress address = InetAddress.getByName("127.0.0.1");int port=10086;DatagramPacket dp=new DatagramPacket(bytes,length,address,port);ds.send(dp);ds.close();}
}public static void main(String[] args) throws IOException {DatagramSocket ds=new DatagramSocket(10086);byte[] bytes=new byte[1024];DatagramPacket dp=new DatagramPacket(bytes,bytes.length);ds.receive(dp);byte[] data = dp.getData();int length=dp.getLength();String s=new String(data,0,length);System.out.println(s);ds.close();}public static void main(String[] args) throws IOException {DatagramSocket ds=new DatagramSocket(666);byte[] bytes=null;DatagramPacket dp=null;while (true) {bytes=new byte[1024];dp=new DatagramPacket(bytes,bytes.length);ds.receive(dp);byte[] data = dp.getData();String s=new String(data,0,data.length);if (s.equals("886")){break;}System.out.println(s);}ds.close();}
3.TCP
代码如下:
package com.xy;import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;/*** @ProjectName: day01* @Package: com.xy* @ClassName: test06* @Author: 杨路恒* @Description:* @Date: 2021/9/1 0001 21:33* @Version: 1.0*/客户端public class test06 {public static void main(String[] args) throws IOException {
// Socket s=new Socket(InetAddress.getByName("192.168.65.1"),66);Socket s=new Socket("192.168.65.1",66);OutputStream os = s.getOutputStream();os.write("杨大大".getBytes());s.close();}
}服务器端public static void main(String[] args) throws IOException {ServerSocket ss=new ServerSocket(66);Socket s = ss.accept();InputStream is = s.getInputStream();byte[] bytes=new byte[1024];int length = is.read(bytes);String s1=new String(bytes,0,length);System.out.println(s1);ss.close();s.close();}
文件上传代码如下:
package com.xy;import java.io.*;
import java.net.Socket;/*** @ProjectName: day01* @Package: com.xy* @ClassName: test11Socket* @Author: 杨路恒* @Description:* @Date: 2021/9/2 0002 20:03* @Version: 1.0*/
public class test11Socket {public static void main(String[] args) throws IOException {Socket s=new Socket("192.168.65.1",66);BufferedReader br=new BufferedReader(new FileReader("day09\\aa.txt"));OutputStream os = s.getOutputStream();BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));String length=null;while ((length=br.readLine())!=null){bw.write(length);bw.newLine();bw.flush();}// bw.write("886");
// bw.newLine();
// bw.flush();s.shutdownOutput();BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream()));String s1 = br1.readLine();System.out.println(s1);br.close();s.close();}
}package com.xy;import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;/*** @ProjectName: day01* @Package: com.xy* @ClassName: test12Socket* @Author: 杨路恒* @Description:* @Date: 2021/9/2 0002 20:08* @Version: 1.0*/
public class test12Socket {public static void main(String[] args) throws IOException {ServerSocket ss=new ServerSocket(66);Socket s = ss.accept();BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));BufferedWriter bw=new BufferedWriter(new FileWriter("day09\\aaa.txt"));String length=null;while ((length=br.readLine())!=null){
// if (length.equals("886")){
// break;
// }bw.write(length);bw.newLine();bw.flush();}BufferedWriter bw1=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));bw1.write("上传成功");bw1.newLine();bw1.flush();ss.close();}
}public static void main(String[] args) throws IOException {ServerSocket ss=new ServerSocket(66);UUID uuid=UUID.randomUUID();System.out.println(uuid);while (true){Socket s = ss.accept();new Thread(new ServerThread(s)).start();}}public class ServerThread implements Runnable{private Socket s;public ServerThread(Socket s) {this.s = s;}@Overridepublic void run() {try {BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));int count=0;File file=new File("day09\\aaa[+"+count+"].txt");while (file.exists()){count++;file=new File("day09\\aaa[+"+count+"].txt");}BufferedWriter bw=new BufferedWriter(new FileWriter(file));String length=null;while ((length=br.readLine())!=null){// if (length.equals("886")){// break;// }bw.write(length);bw.newLine();bw.flush();}BufferedWriter bw1=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));bw1.write("上传成功");bw1.newLine();bw1.flush();s.close();} catch (IOException e) {e.printStackTrace();}}
}