Java 通过Socket监听指定服务器(IP)的指定端口,及向指定服务器的指定端口发送信息

客户端:向指定端口发送信息

package com.jszc.lottery.modules.longpay.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import com.jszc.lottery.common.utils.MakeOrderNumUtil;

public class RECV_UBANKNO {

    public String go(String xmlInfo) throws UnknownHostException, IOException {

        // 向服务器端发送请求,服务器IP地址和服务器监听的端口号
        Socket client = new Socket("192.168.1.125", 12345);

        // 通过printWriter 来向服务器发送消息
        PrintWriter printWriter = new PrintWriter(client.getOutputStream());
        System.out.println("连接已建立...");

        // 发送消息
        printWriter.println(xmlInfo);

        printWriter.flush();

        // InputStreamReader是低层和高层串流之间的桥梁
        // client.getInputStream()从Socket取得输入串流
        InputStreamReader streamReader = new InputStreamReader(client.getInputStream());

        // 链接数据串流,建立BufferedReader来读取,将
        //BufferReader链接到InputStreamReder
        BufferedReader reader = new BufferedReader(streamReader);
        String advice = reader.readLine();

        reader.close();
        return advice;
    }

    public static void main(String[] args) throws UnknownHostException,IOException {

        StringBuilder sb = new StringBuilder();
        sb.append("balabala");
        // 提交请求

        String utf8 = new String(sb.toString().getBytes("UTF-8"));
        String unicode = new String(utf8.getBytes(), "UTF-8");
        String gbk = new String(unicode.getBytes("GB2312"));
        // String xml = new String();//GB18030
        RECV_UBANKNO c = new RECV_UBANKNO();
        String advice = c.go(gbk);
        System.out.println("接收到服务器的消息 :" + advice);
    }
}

package com.jszc.lottery.modules.longpay.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import com.jszc.lottery.common.utils.MakeOrderNumUtil;

public class RECV_UBANKNO {

public String go(String xmlInfo) throws UnknownHostException, IOException {

    // 向服务器端发送请求,服务器IP地址和服务器监听的端口号
    Socket client = new Socket("192.168.1.125", 12345);

    // 通过printWriter 来向服务器发送消息
    PrintWriter printWriter = new PrintWriter(client.getOutputStream());
    System.out.println("连接已建立...");

    // 发送消息
    printWriter.println(xmlInfo);

    printWriter.flush();

    // InputStreamReader是低层和高层串流之间的桥梁
    // client.getInputStream()从Socket取得输入串流
    InputStreamReader streamReader = new InputStreamReader(
            client.getInputStream());

    // 链接数据串流,建立BufferedReader来读取,将BufferReader链接到InputStreamReder
    BufferedReader reader = new BufferedReader(streamReader);
    String advice = reader.readLine();

    reader.close();
    return advice;
}

public static void main(String[] args) throws UnknownHostException,
        IOException {
    // 拼接xml报文
    /*
     *  
     * 请求序列码 客户号
     * 操作员号 密码
     * 6W0201 CN 
     *  
     */
    StringBuilder sb = new StringBuilder();
    MakeOrderNumUtil makeOrderNumUtil = new MakeOrderNumUtil();
    makeOrderNumUtil.makeOrderNum();
    String num = makeOrderNumUtil.makeOrderNum();
    sb.append("  ");
    sb.append("");
    sb.append("" + num + "");// 请求序列码
    sb.append("ZJ33000009175763101");// 客户号
    sb.append("WLPT01");// 操作员号
    sb.append("123456");// 密码
    sb.append("6W0201");// 交易请求码
    sb.append("CN ");// 语言
    sb.append("");
    sb.append("");
    sb.append("");
    // 提交请求
    /*
     * 4、发送报文兼容GB2312和GB18030编码格式,返回报文为GB18030编码格式,
     * 另需要测试一下发送报文编码格式为GB18030的情况下交易是否OK。
     */
    String utf8 = new String(sb.toString().getBytes("UTF-8"));
    String unicode = new String(utf8.getBytes(), "UTF-8");
    String gbk = new String(unicode.getBytes("GB2312"));
    // String xml = new String();//GB18030
    RECV_UBANKNO c = new RECV_UBANKNO();
    String advice = c.go(gbk);
    System.out.println("接收到服务器的消息 :" + advice);
}

}
服务器端,监听指定端口:


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketService {
    //搭建服务器端
    public static void main(String[] args) throws IOException{
        SocketService socketService = new SocketService();
        //1、a)创建一个服务器端Socket,即SocketService 
        socketService.oneServer();
    }
    public  void oneServer(){
        try{
            ServerSocket server=null;
            try{
                server=new ServerSocket(12347);
                //b)指定绑定的端口,并监听此端口。
                System.out.println("服务器启动成功");
                //创建一个ServerSocket在端口5209监听客户请求
            }catch(Exception e) {
                    System.out.println("没有启动监听:"+e);
                    //出错,打印出错信息
            }
            Socket socket=null;
            try{
                socket=server.accept();
                //2、调用accept()方法开始监听,等待客户端的连接 
                //使用accept()阻塞等待客户请求,有客户
                //请求到来则产生一个Socket对象,并继续执行
            }catch(Exception e) {
                System.out.println("Error."+e);
                //出错,打印出错信息
            }
            //3、获取输入流,并读取客户端信息 
            String line;
            BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //由Socket对象得到输入流,并构造相应的BufferedReader对象
            PrintWriter writer=new PrintWriter(socket.getOutputStream());
            //由Socket对象得到输出流,并构造PrintWriter对象
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            //由系统标准输入设备构造BufferedReader对象
            System.out.println("Client:"+in.readLine());
            //在标准输出上打印从客户端读入的字符串
            line=br.readLine();
            //从标准输入读入一字符串
            //4、获取输出流,响应客户端的请求 
            while(!line.equals("end")){
            //如果该字符串为 "bye",则停止循环
                writer.println(line);
                //向客户端输出该字符串
                writer.flush();
                //刷新输出流,使Client马上收到该字符串
                System.out.println("Server:"+line);
                //在系统标准输出上打印读入的字符串
                System.out.println("Client:"+in.readLine());
                //从Client读入一字符串,并打印到标准输出上
                line=br.readLine();
                //从系统标准输入读入一字符串
            } //继续循环

            //5、关闭资源 
            writer.close(); //关闭Socket输出流
            in.close(); //关闭Socket输入流
            socket.close(); //关闭Socket
            server.close(); //关闭ServerSocket
        }catch(Exception e) {//出错,打印出错信息
            System.out.println("Error."+e);
        }
    }
}

你可能感兴趣的:(java)