本文共 9911 字,大约阅读时间需要 33 分钟。
代码示例1:
因为只自己的电脑,所以都是自己连自己
使用is.close()、os.close()、br.close()等都会造成socket被关闭 */public class TCPTest1 { @Test public void client(){ Socket socket = null; OutputStream os = null; try { //1.创建Socket对象,指明服务器端的ip和端口号 socket = new Socket("127.0.0.1",8866); //2.获取一个输出流,用于输出数据 os = socket.getOutputStream(); //3.写出数据的操作 os.write("我是客户端!".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { if(os != null){ //4.资源的关闭 try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void server(){ ServerSocket ss = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1.创建服务器端的ServerSocket,指明自己的端口号 ss = new ServerSocket(8866); //2.调用accept()表示接收来自于客户端的socket socket = ss.accept(); //3.获取输入流 is = socket.getInputStream(); //4.读取输入流中的数据 //不建议这样写,可能会乱码,字节流传输中文可能出现乱码// byte[] buffer = new byte[5];// int len;// while((len = is.read(buffer)) != null){ // String str = new String(buffer,0,len);// System.out.println(str);// } baos = new ByteArrayOutputStream(); byte[] buffer = new byte[5]; int len; while((len = is.read(buffer)) != -1){ baos.write(buffer,0,len); } System.out.println(baos.toString()); System.out.println("收到了来自于:" + socket.getInetAddress().getHostAddress() + "的数据"); } catch (IOException e) { e.printStackTrace(); } finally { //5.关闭资源 if(baos != null){ try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if(is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if(ss != null){ try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
代码示例2:
实现TCP的网络编程public class TCPTest1 { @Test public void client(){ Socket socket = null; OutputStream os = null; try { //1.创建Socket对象,指明服务器端的ip和端口号 socket = new Socket("127.0.0.1",8866); //2.获取一个输出流,用于输出数据 os = socket.getOutputStream(); //3.写出数据的操作 os.write("我是客户端!".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { if(os != null){ //4.资源的关闭 try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void server(){ ServerSocket ss = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1.创建服务器端的ServerSocket,指明自己的端口号 ss = new ServerSocket(8866); //2.调用accept()表示接收来自于客户端的socket socket = ss.accept(); //3.获取输入流 is = socket.getInputStream(); //4.读取输入流中的数据 //不建议这样写,可能会乱码,字节流传输中文可能出现乱码// byte[] buffer = new byte[5];// int len;// while((len = is.read(buffer)) != null){ // String str = new String(buffer,0,len);// System.out.println(str);// } baos = new ByteArrayOutputStream(); byte[] buffer = new byte[5]; int len; while((len = is.read(buffer)) != -1){ baos.write(buffer,0,len); } System.out.println(baos.toString()); System.out.println("收到了来自于:" + socket.getInetAddress().getHostAddress() + "的数据"); } catch (IOException e) { e.printStackTrace(); } finally { //5.关闭资源 if(baos != null){ try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if(is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if(ss != null){ try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
代码示例3:
实现TCP的网络编程public class TCPTest3 { @Test public void client(){ Socket socket = null; OutputStream os = null; FileInputStream fis = null; InputStream is = null; ByteArrayOutputStream baos = null; try { socket = new Socket("127.0.0.1",4455); os = socket.getOutputStream(); fis = new FileInputStream("灿烈.jpg"); byte[] buffer = new byte[20]; int len; while((len = fis.read(buffer)) != -1){ os.write(buffer,0,len); } //关闭数据的输出 socket.shutdownOutput(); //5.接收来自于服务器端的数据,并显示到控制台上 is = socket.getInputStream(); baos = new ByteArrayOutputStream(); byte[] buffer2 = new byte[5]; int len2; while((len2 = is.read(buffer2)) != -1){ baos.write(buffer2,0,len2); } System.out.println(baos.toString()); } catch (IOException e) { e.printStackTrace(); } finally { if(fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if(os != null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if(is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(baos != null){ try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void server(){ ServerSocket ss = null; Socket socket = null; InputStream is = null; FileOutputStream fos = null; OutputStream os = null; try { ss = new ServerSocket(4455); socket = ss.accept(); is = socket.getInputStream(); fos = new FileOutputStream("灿烈4.jpg"); byte[] buffer = new byte[20]; int len; while((len = is.read(buffer)) != -1){ fos.write(buffer,0,len); } //6.服务器端给予客户端反馈 os = socket.getOutputStream(); os.write("你好,我已经接收到了你的消息!".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { if(fos != null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if(is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if(ss != null){ try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } if(os != null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
转载地址:http://lvuki.baihongyu.com/