网络编程自学笔记 (2)

服务器端

package com.lessonweb.tcp; import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class TcpServerDemo02 { public static void main(String[] args) throws Exception { //1、创建服务 ServerSocket serverSocket = new ServerSocket(9000); //2、监听客户端的连接 Socket socket = serverSocket.accept(); //3、获取输入流 InputStream is = socket.getInputStream(); //4、文件输出 FileOutputStream fos = new FileOutputStream(new File("receive")); 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()); //关闭资源 os.close(); fos.close(); is.close(); socket.close(); serverSocket.close(); } }

客户端

package com.lessonweb.tcp; import java.io.*; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class TcpClientDemo02 { public static void main(String[] args) throws Exception { Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000); //2、创建一个输出流 OutputStream os = socket.getOutputStream(); //3、读取一个文件 FileInputStream files = new FileInputStream(new File("TcpClientDemo01.java")); //4、写出文件 byte[] buffer = new byte[1024]; int len; while((len = files.read())!=1){ os.write(buffer,0,len); } //通知服务器。我已经结束了 socket.shutdownOutput();//我已经传输完了 //确定服务器接收完毕 才能够断开连接 InputStream inputStream = socket.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer2 = new byte[1024]; int len2; while ((len2=inputStream.read())!=-1){ baos.write(buffer2,0,len2); } System.out.println(baos.toString()); //5、关闭 baos.close(); inputStream.close(); files.close(); os.close(); socket.close(); } } 1.7、UDP

1、聊天实现

发送方:

public class UdpSenderDemo01 { public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(8888); //准备数据,控制台读取 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while (true){ String data = reader.readLine(); byte[] datas = data.getBytes(); DatagramPacket packet = new DatagramPacket(datas, 0, datas.length,new InetSocketAddress("localhost",6666)); socket.send(packet); if (data.equals("bye")){ break; } } socket.close(); } }

接收方:

public class UdpReceiveDemo01 { public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(6666); while (true){ //准备接受包裹 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(receiveData); if (receiveData.equals("bye")){ break; } } socket.close(); } }

2、在线咨询:两个人都可以是发送方,也都可以是接收方!

Receive:

public class TalkReceive implements Runnable{ DatagramSocket socket =null; private int port; private String msgFrom; public TalkReceive(int port,String msgFrom) { this.port = port; this.socket = socket; this.msgFrom = msgFrom; try { socket = new DatagramSocket(port); } catch (SocketException e) { e.printStackTrace(); } } @Override public void run() { while (true){ try { //准备接受包裹 byte[] container = new byte[1024]; DatagramPacket packet = new DatagramPacket(container,0,container.length); try { socket.receive(packet);//接收包裹 } catch (IOException e) { e.printStackTrace(); } //断开连接 byte[] data = packet.getData(); String receiveData = new String(data, 0, data.length); System.out.println(msgFrom+":"+receiveData); if (receiveData.equals("bye")){ break; } } catch (Exception e) { e.printStackTrace(); } } socket.close(); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzpgxf.html