基本的Socket编程
Posted celine
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基本的Socket编程相关的知识,希望对你有一定的参考价值。
import java.net.ServerSocket; import java.net.Socket; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.IOException; import java.io.BufferedReader; import java.io.PrintStream; /** ServerSocket服务端 */ public class SimpleServer{ //服务端 ServerSocket serverSkt = null; //服务端accept返回的客户端对象 Socket clientSkt = null; //服务端Socket的输入流和输出流,客户端和服务端的输入流、输出流都是以对方为对象,都需要转换成可打印的字符流 BufferedReader in = null; PrintStream out = null; //Socker服务端是基于Socket类型的端口,所以在创建对象时需要参数:端口,还需要接收客户端请求和返回响应 public SimpleServer(int port){ System.out.println("服务端正在监听端口:"+port); try{ serverSkt = new ServerSocket(port); }catch(IOException e){ System.out.println("服务端端口"+port+"监听失败..."); e.printStackTrace(); } try{ //服务端返回的Socket对象 clientSkt = serverSkt.accept(); System.out.println("连接成功"); }catch(IOException e){ System.out.println("连接失败..."); e.printStackTrace(); } try{ in = new BufferedReader(new InputStreamReader(clientSkt.getInputStream())); out = new PrintStream(clientSkt.getOutputStream()); System.out.println("初始化成功"); }catch(IOException e){ e.printStackTrace(); } } //通过服务端Socket对象,获取客户端的请求内容 public String getRequest(){ String frmClt = null; try{ frmClt = in.readLine(); System.out.println("server收到的请求:"+frmClt); }catch(IOException e){ System.out.println("无法获取端口"); e.printStackTrace(); System.exit(1); } return frmClt; } //通过服务端socket对象,返回响应给客户端,响应内容自定义 public void sendResponse(String response){ //Prints a String and then terminate the line. System.out.println("服务端响应:"+response); out.println(response); } public static void main(String[] args){ SimpleServer sa = new SimpleServer(8888); //自动返回响应 while(true){ sa.sendResponse(sa.getRequest()); } } }
import java.net.Socket; import java.io.PrintStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; /** Socket客户端 */ public class SimpleClient{ Socket clientSkt = null; //客户端Socket的输入流和输出流,客户端和服务端的输入流、输出流都是以对方为对象 PrintStream out = null; BufferedReader in = null; //通过服务端IP和监听的端口进行连接 public SimpleClient(String host, int port){ try{ clientSkt = new Socket(host, port); System.out.println("创建客户端实例对象"); in = new BufferedReader(new InputStreamReader(clientSkt.getInputStream())); out = new PrintStream(clientSkt.getOutputStream()); }catch(IOException e){ System.out.println("无法连接服务器"); e.printStackTrace(); } } //发送请求 public void sendRequest(String request){ out.println(request); System.out.println("向服务端发送请求:"+request); } //接收响应 public String getResponse(){ String frmSer = null; try{ frmSer = in.readLine(); System.out.println("接收服务端响应:"+frmSer); }catch(IOException e){ e.printStackTrace(); } return frmSer; } public static void main(String[] args){ SimpleClient client = new SimpleClient("127.0.0.1",8888); //回车符" "、换行符" "可作为多请求间隔符 client.sendRequest("client request1!"+" "+"client request2!"); // client.sendRequest("client request1!"+" "+"client request2!"); //自动获取响应 while(true){ client.getResponse(); } } }
客户端执行结果:
G:maul keyboard etwork programmingase socket programming>javac SimpleClient.java
G:maul keyboard
etwork programmingase socket programming>java SimpleClient
创建客户端实例对象
client request2!:client request1!
接收服务端响应:client request1!
接收服务端响应:client request2!
服务端执行结果:
G:maul keyboard etwork programmingase socket programming>javac SimpleServer.java
G:maul keyboard
etwork programmingase socket programming>java SimpleServer
服务端正在监听端口:8888
连接成功
初始化成功
server收到的请求:client request1!
服务端响应:client request1!
server收到的请求:client request2!
服务端响应:client request2!
以上是关于基本的Socket编程的主要内容,如果未能解决你的问题,请参考以下文章