package sunday;
import java.io.; import java.net.; import java.util.Scanner;
/* UDP TCP 留言板(UDP) 服务器反馈 客户端数据来自键盘录入 多线程上传文件 */ public class
Demo
{ public static void main(String[] args) throws IOException { //web(); //send(); //client(); //client1(); client2(); }//main public static void
idea()
{ /*
*/ } //网络编程 public static void
udp()
throws IOException { DatagramSocket datagramSocket = new DatagramSocket(); DatagramSocket datagramSocket1 = new DatagramSocket(1234); //内容,内容长度,IP地址,端口号 byte[] bytes = “字符串”.getBytes(); DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(“192.168.0.101”), 1234); byte[] bytes1 = new byte[1024]; DatagramPacket datagramPacket1 = new DatagramPacket(bytes1,bytes1.length); //发送,接收,解析,关闭 datagramSocket.send(datagramPacket); datagramSocket1.receive(datagramPacket1);
byte[] data = datagramPacket1.getData(); String s = new String(data); System.out.println(s); datagramSocket.close();
}
public static void
tcp()
throws IOException { Socket socket = new Socket(“192.168.0.101”, 12345); ServerSocket serverSocket = new ServerSocket(12345); //获取输出流,写数据 OutputStream outputStream = socket.getOutputStream(); outputStream.write(“shuju”.getBytes());
//监听客户端连接,获取输入流,读数据,禁用此套接字的输出流 Socket socket1 = serverSocket.accept(); InputStream inputStream = socket1.getInputStream(); byte[] bytes = new byte[1024]; int read = inputStream.read(); String data = new String(bytes, 0, read); System.out.println(“数据是”+data);
socket.shutdownOutput(); //解放 socket.close(); serverSocket.close(); socket1.close();
} //留言板(UDP) public static void
send()
throws IOException { //创建对象 DatagramSocket datagramSocket = new DatagramSocket(); //录入数据 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line=bufferedReader.readLine())!=null) { if (“886”.equals(line)) { break; } //打包 byte[] bytes = line.getBytes(); DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(“192.168.0.101”),12345); //发送 datagramSocket.send(datagramPacket); } //关闭 datagramSocket.close(); }
//服务器反馈 public static void
client()
throws IOException { //客户端 //找对象 Socket socket = new Socket(“192.168.0.100”, 12345); //获取输出流,写数据 OutputStream outputStream = socket.getOutputStream(); outputStream.write(“鸨,想你的液”.getBytes());
//接受服务器反馈 InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024]; int read = inputStream.read(bytes); String data = new String(bytes, 0, read); System.out.println(“服务器端发来:”+data);
//解放 socket.close(); } //客户端数据来自键盘录入 public static void
client1()
throws IOException { Socket socket = new Socket(“192.168.0.100”, 1234); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String line; while ((line=bufferedReader.readLine())!=null){ if(“886”.equals(line)){ break; } bufferedWriter.write(line); bufferedWriter.newLine(); bufferedWriter.flush(); } socket.close(); } //多线程上传文件 public static void
client2()
throws IOException { //找对象 Socket socket = new Socket(“192.168.0.100”, 1234);
//先读后写 BufferedReader bufferedReader = new BufferedReader(new FileReader(“E:\idea\July\12.txt”)); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String line; while ((line=bufferedReader.readLine())!=null){ bufferedWriter.write(line); bufferedWriter.newLine(); bufferedWriter.flush(); } socket.shutdownOutput(); //接收反馈 BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(socket.getInputStream())); String s = bufferedReader1.readLine();//等待读取数据 System.out.println(“服务器反馈:”+s); //解放 bufferedReader.close(); socket.close();
} }//end package sunday;
import java.io.; import java.net.;
public class
Demo1
{ public static void main(String[] args) throws IOException { //receive(); //server(); //server1(); server2(); }//main public static void
receive()
throws IOException { //建对象 DatagramSocket datagramSocket = new DatagramSocket(1234); while (true) { //数据包 byte[] bytes = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length); datagramSocket.receive(datagramPacket); //解析 System.out.println(“有网友留言:”+new String(datagramPacket.getData(),0,datagramPacket.getLength())); } }
//服务器反馈 public static void
server()
throws IOException { //服务器端 //找对象 ServerSocket serverSocket = new ServerSocket(12345);
//监听客户端连接,返回一个socket对象 Socket socket = serverSocket.accept(); //获取输入流,读数据,并把数据显示在控制台 InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024]; int read = inputStream.read(bytes); String data = new String(bytes, 0, read); System.out.println(“客户端发来:”+data); //给出反馈 OutputStream outputStream = socket.getOutputStream(); outputStream.write(“呦呦呦,gui”.getBytes());
//解放 serverSocket.close(); } public static void
server1()
throws IOException { ServerSocket serverSocket = new ServerSocket(1234);
//监听客户端连接,返回一个socket对象 Socket socket = serverSocket.accept(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; while ((line=bufferedReader.readLine())!=null){ System.out.println(line); } serverSocket.close(); } //多线程上传文件 public static void
server2()
throws IOException { //找对象 ServerSocket serverSocket = new ServerSocket(1234); //开线程 while (true){ Socket socket = serverSocket.accept(); new Thread(new ServerThread(socket)).start(); }
} }//end package sunday;
import java.io.*; import java.net.Socket;
public class
ServerThread
implements Runnable { private Socket socket; public ServerThread(Socket socket) { this.socket=socket; }
@Override public void run() { try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); //BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(“E:\idea\July\mark.txt”)); int count=0; File file = new File(“E:\idea\July\mark(” + count + “).txt”); while (file.exists()){ count++; file = new File(“E:\idea\July\mark(” + count + “).txt”); } BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
String line; while ((line=bufferedReader.readLine())!=null){ bufferedWriter.write(line); bufferedWriter.newLine(); bufferedWriter.flush();
//反馈 BufferedWriter bufferedWriter1 = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); bufferedWriter1.write(“文件上传成功”); bufferedWriter1.newLine(); bufferedWriter1.flush(); } }catch (IOException e){ e.printStackTrace(); }
} }
|