栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我可以仅使用套接字在Java和C#之间进行通信吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我可以仅使用套接字在Java和C#之间进行通信吗?

主要问题是您需要非常小心发送和接收的数据的编码。这是一对可以协同工作的程序。C#客户端发送字符串,方法是首先发送其长度为整数,然后发送字符串本身的字节。Java服务器读取长度,然后读取消息并将输出打印到控制台。然后编写回显消息,计算其长度,提取字节并将其发送回C#客户端。客户端读取长度,消息并打印输出。应该有一种方法来避免所有按位排列的东西,但是老实说,我对此东西有些不满意,尤其是在Java方面。

Java服务器:

import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class JavaSocket {    public static void main(String[] args) throws IOException {        ServerSocket serverSocket = new ServerSocket(4343, 10);        Socket socket = serverSocket.accept();        InputStream is = socket.getInputStream();        OutputStream os = socket.getOutputStream();        // Receiving        byte[] lenBytes = new byte[4];        is.read(lenBytes, 0, 4);        int len = (((lenBytes[3] & 0xff) << 24) | ((lenBytes[2] & 0xff) << 16) |       ((lenBytes[1] & 0xff) << 8) | (lenBytes[0] & 0xff));        byte[] receivedBytes = new byte[len];        is.read(receivedBytes, 0, len);        String received = new String(receivedBytes, 0, len);        System.out.println("Server received: " + received);        // Sending        String toSend = "Echo: " + received;        byte[] toSendBytes = toSend.getBytes();        int toSendLen = toSendBytes.length;        byte[] toSendLenBytes = new byte[4];        toSendLenBytes[0] = (byte)(toSendLen & 0xff);        toSendLenBytes[1] = (byte)((toSendLen >> 8) & 0xff);        toSendLenBytes[2] = (byte)((toSendLen >> 16) & 0xff);        toSendLenBytes[3] = (byte)((toSendLen >> 24) & 0xff);        os.write(toSendLenBytes);        os.write(toSendBytes);        socket.close();        serverSocket.close();    }}

AC#客户端:

using System;using System.Net;using System.Net.Sockets;namespace CSharpSocket{    class MainClass    {        public static void Main (string[] args)        { string toSend = "Hello!"; IPEndPoint serverAddress = new IPEndPoint(IPAddress.Parse("192.168.0.6"), 4343); Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); clientSocket.Connect(serverAddress); // Sending int toSendLen = System.Text.Encoding.ASCII.GetByteCount(toSend); byte[] toSendBytes = System.Text.Encoding.ASCII.GetBytes(toSend); byte[] toSendLenBytes = System.BitConverter.GetBytes(toSendLen); clientSocket.Send(toSendLenBytes); clientSocket.Send(toSendBytes); // Receiving byte[] rcvLenBytes = new byte[4]; clientSocket.Receive(rcvLenBytes); int rcvLen = System.BitConverter.ToInt32(rcvLenBytes, 0); byte[] rcvBytes = new byte[rcvLen]; clientSocket.Receive(rcvBytes); String rcv = System.Text.Encoding.ASCII.GetString(rcvBytes); Console.WriteLine("Client received: " + rcv); clientSocket.Close();        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430862.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号