码迷,mamicode.com
首页 > 其他好文 > 详细

基本的Socket编程

时间:2018-11-18 21:08:27      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:通过   cep   服务   执行   tac   失败   stream   发送请求   class   

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);
        //回车符"\r"、换行符"\n"可作为多请求间隔符
        client.sendRequest("client request1!"+"\r"+"client request2!");
//        client.sendRequest("client request1!"+"\n"+"client request2!");
        //自动获取响应
        while(true){
            client.getResponse();
            }
        }    
    }

客户端执行结果:

G:\maul keyboard\network programming\base socket programming>javac SimpleClient.java

G:\maul keyboard\network programming\base socket programming>java SimpleClient
创建客户端实例对象
client request2!:client request1!
接收服务端响应:client request1!
接收服务端响应:client request2!

 

服务端执行结果:

G:\maul keyboard\network programming\base socket programming>javac SimpleServer.java

G:\maul keyboard\network programming\base socket programming>java SimpleServer
服务端正在监听端口:8888
连接成功
初始化成功
server收到的请求:client request1!
服务端响应:client request1!
server收到的请求:client request2!
服务端响应:client request2!

 

基本的Socket编程

标签:通过   cep   服务   执行   tac   失败   stream   发送请求   class   

原文地址:https://www.cnblogs.com/celine/p/9979301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!