1.socket的简介
【1】什么是socket
Socket,又称为套接字,Socket是计算机网络通信的基本的技术之一。如今大多数基于网络的软件,如浏览器,即时通讯工具甚至是P2P下载都是基于Socket实现的。本文会介绍一下基于TCP/IP的Socket编程,并且如何写一个客户端/服务器程序。
2.什么是TCP协议
2.1 TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层协议。
3.如何运用Socket实现简单的客服端和服务端
实现效果:客服端可以一直向服务端发送消息,服务端一直响应客服端。
客服端:
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Scanner; //Socket实现客服端 public class SocketClient { public static void main(String[] args) { while (true){ System.out.println("请输入你要发送的内容:"); Scanner scanner=new Scanner(System.in); String context=scanner.next(); senMessage(context); } } public static void senMessage(String context){ OutputStream os=null; Socket sk=null; InputStream is=null; try { //创建Socket客服端,绑定服务器的IP和端口号 sk = new Socket("localhost",888); //获取输出流 os = sk.getOutputStream(); //向输出内容 //以字节流的形式写出 os.write(context.getBytes());
os.flush(); //关闭输出流,接收服务器端的响应 sk.shutdownOutput(); is=sk.getInputStream(); byte[] bytes=new byte[1024]; int len=-1; while ((len=is.read(bytes))!=-1){ String str=new String(bytes,0,len); System.out.println(str); } } catch (IOException e) { e.printStackTrace(); }finally { if (os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); }finally { if(sk!=null){ try { sk.close(); } catch (IOException e) { e.printStackTrace(); }finally { if (is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } } }
服务端:
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class SocketServe { public static void main(String[] args) { while (true){ acceptMessage(); } } public static void acceptMessage(){ //ServerSocket类实现服务器套接字。服务器套接字等待请求通过网络传入。 // 它基于该请求执行某些操作,然后可能向请求者返回结果。 ServerSocket serverSocket=null; InputStream is=null; Socket client=null; OutputStream out=null; try { serverSocket = new ServerSocket(888); /** * Socket accept() :侦听并接受到此套接字的连接。 * 返回Socket对象,该对象封装了接收到数据包的相关信息, * 操作该对象,可以处理正文数据,及端口信息和IP信息等 */ // System.out.println("acept-start"); client=serverSocket.accept(); // System.out.println("accept-end"); //读取源IP // System.out.println(client.getLocalAddress()); //读取客服端发送的数据(接收客服端发送的数据) //获取输入流 is=client.getInputStream(); byte[] bytes=new byte[1024]; //读取内容长度大小:-1 int len =-1; while ((len=is.read(bytes))!=-1){ //操作读取数据 String str=new String(bytes,0,len); System.out.println(str); } //响应客服端 out= client.getOutputStream(); String context="你好,我是服务器端"; out.write(context.getBytes());
out.flush(); } catch (IOException e) { e.printStackTrace(); }finally { //从下向上释放资源 if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); }finally { if (client!=null){ try { client.close(); } catch (IOException e) { e.printStackTrace(); }finally { if (serverSocket!=null){ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); }finally { if (out!=null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } } } } }