2 fangfangloveit fangfangloveit 于 2016.04.29 17:01 提问

client发的请求接收返回信息时用send来接收,其余的由receive来接收,怎么改?求大神帮忙

sever的请求都友client的receive来接收,client发的请求接收返回信息时用send来接收,怎么改下?求大神帮忙,我只会大概写了下代码,菜鸟急需用!


 package test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

import org.json.JSONArray;
import org.json.JSONObject;

public class Client {
    private Socket client;

    public Client() {
        try {
            client = new Socket("localhost", 8080);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void send() {
        try {
            OutputStream out = client.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8");
            PrintWriter pw = new PrintWriter(osw, true);

            JSONObject message = new JSONObject();
            JSONObject header = new JSONObject();
            header.put("cposId", "A00C0001");
            header.put("timestamp", "1432199344");
            header.put("hash", "c83c2c7057e377aa68bf736ab0107e45");
            message.put("header", header);
            message.put("functionName", "addOrder");
            JSONObject orderData = new JSONObject();
            orderData.put("transCode", "TC12345");
            orderData.put("transAmount", 40);
            orderData.put("member",
                    (new JSONObject()).put("cardId", "12345").put("cellphone", "11111111111").put("balance", 1000));
            orderData.put("ticketDetails", (new JSONArray()).put((new JSONObject()).put("movieName", "My Movie")
                    .put("schedule", "2015-10-10 05:00:00").put("amount", 40).put("vipAmount", 30).put("quantity", 2)));
            message.put("data", orderData);

            pw.println(message);
            pw.flush();

            InputStream is = client.getInputStream();
            System.out.println("服务端已连接!");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

            while (true) {
                String info = reader.readLine();
                if (info != null) {
                    System.out.println("来自服务器的消息:" + info);
                    Scanner scanner = new Scanner(System.in);
                    String s = scanner.nextLine();
                    pw.println(s);
                    pw.flush();

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public void receive() {
        try {
            InputStream is = client.getInputStream();
            System.out.println("服务端已连接!");

            OutputStream out;
            out = client.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8");
            PrintWriter pw = new PrintWriter(osw, true);
            pw.println("可以随时接收请求了!");

            pw.flush();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            while (true) {
                String info = reader.readLine();
                if (info != null) {
                    System.out.println("来自服务器的消息:" + info);
                    String s = "";
                    switch (info) {
                    case "A":
                        s = "卡余额89.00元";
                    case "B":
                        s = "nothing";
                    }
                    pw.println(s);
                    pw.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.send();
        client.receive();
    }
}

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
    private ServerSocket serverSocket;

    public Server() {
        try {
            serverSocket = new ServerSocket(8080);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void receive() {
        try {
            System.out.println("等待客户端连接。。。");
            Socket socket = serverSocket.accept();
            System.out.println("客户端已连接!");
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            BufferedReader br = new BufferedReader(isr);

            OutputStream out = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "UTF-8"), true);

            while (true) {
                String info = br.readLine();
                if (info.indexOf("addOrder") != -1) {
                    System.out.println("来自客户端的消息:" + info);
                    writer.println("。。。订单已添加成功!");
                    writer.flush();
                } else if (info.indexOf("a") != -1) {
                    writer.println("a会员充值");
                    System.out.println("来自客户端的消息:" + info);
                    writer.flush();
                } else {
                    writer.println("查询会员卡");
                    Scanner scanner = new Scanner(System.in);
                    String s = scanner.nextLine();
                    System.out.println("来自客户端的消息:" + info);
                    writer.flush();
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void send() {
        try {
            System.out.println("等待客户端连接。。。");
            Socket socket = serverSocket.accept();
            System.out.println("客户端已连接!");
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            BufferedReader br = new BufferedReader(isr);

            OutputStream out = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "UTF-8"), true);

            while (true) {
                String info = br.readLine();
                Scanner scanner = new Scanner(System.in);
                String s = scanner.nextLine();
                writer.println(s);
                System.out.println("来自客户端的消息:" + info);
                writer.flush();

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        server.receive();
        server.send();
    }
}

2个回答

aiynmimi
aiynmimi   2016.04.29 18:38

这是最简单的网络编程的问题!
可以参考:http://blog.csdn.net/aiynmimi/article/details/47303035

aiynmimi
aiynmimi   2016.04.29 18:37

这是最简单的网络编程的问题!
可以参考:http://blog.csdn.net/aiynmimi/article/details/47303035

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!