temz 2023-03-23 15:27 采纳率: 100%
浏览 59
已结题

java 和C# 通过Socket 可以互传hashmap对象吗?

java服务端 和C#客户端 通过Socket和Json序列化互传对象,可以互传hashmap对象吗?如果可以请发下socket的代码,谢谢

  • 写回答

7条回答 默认 最新

  • Leodong. 2023-03-23 16:30
    关注

    该回答引用GPTᴼᴾᴱᴺᴬᴵ以及结合自己思路,具体如下:

    是可以通过Socket和Json序列化互传HashMap对象。以下是Java服务端和C#客户端的示例代码:
    1、Java服务端代码:

    import java.io.*;
    import java.net.*;
    import java.util.HashMap;
    import com.google.gson.Gson;
    
    public class Server {
        private static final int PORT = 8888;
    
        public static void main(String[] args) {
            try {
                ServerSocket serverSocket = new ServerSocket(PORT);
                System.out.println("Server started.");
    
                while (true) {
                    Socket socket = serverSocket.accept();
                    System.out.println("Client connected.");
    
                    // Read the HashMap object from the client
                    InputStream is = socket.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String json = br.readLine();
                    Gson gson = new Gson();
                    HashMap<String, String> hashMap = gson.fromJson(json, HashMap.class);
                    System.out.println("Received HashMap from client: " + hashMap.toString());
    
                    // Send the HashMap object back to the client
                    OutputStream os = socket.getOutputStream();
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
                    bw.write(gson.toJson(hashMap));
                    bw.newLine();
                    bw.flush();
    
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    2、C#客户端代码:

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Collections.Generic;
    using Newtonsoft.Json;
    
    class Client {
        static void Main(string[] args) {
            try {
                TcpClient tcpClient = new TcpClient("localhost", 8888);
                Console.WriteLine("Connected to server.");
    
                // Send a HashMap object to the server
                NetworkStream ns = tcpClient.GetStream();
                Dictionary<string, string> hashMap = new Dictionary<string, string>();
                hashMap.Add("name", "John");
                hashMap.Add("age", "30");
                string json = JsonConvert.SerializeObject(hashMap);
                byte[] bytesToSend = Encoding.UTF8.GetBytes(json);
                ns.Write(bytesToSend, 0, bytesToSend.Length);
                ns.Flush();
                Console.WriteLine("Sent HashMap to server: " + json);
    
                // Receive the HashMap object from the server
                byte[] bytesToRead = new byte[tcpClient.ReceiveBufferSize];
                int bytesRead = ns.Read(bytesToRead, 0, tcpClient.ReceiveBufferSize);
                string receivedJson = Encoding.UTF8.GetString(bytesToRead, 0, bytesRead);
                Dictionary<string, string> receivedHashMap = JsonConvert.DeserializeObject<Dictionary<string, string>>(receivedJson);
                Console.WriteLine("Received HashMap from server: " + receivedJson);
    
                tcpClient.Close();
            } catch (Exception e) {
                Console.WriteLine("Exception: " + e.ToString());
            }
        }
    }
    

    如果以上回答对您有所帮助,望采纳~谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月27日
  • 已采纳回答 3月24日
  • 创建了问题 3月23日

悬赏问题

  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理