简体   繁体   English

在C#套接字中发送字符串数组并在Java套接字中接收

[英]Sending a string array in a C# socket and receive in a Java socket

As I mentioned in the title my problem is the following: I would like to send a string array in C# using sockets to a Java application which also uses sockets. 正如我在标题中提到的那样,我的问题如下:我想使用套接字将C#中的字符串数组发送到也使用套接字的Java应用程序中。 I have tried to send the first and the second item of the array but when I tried to add them to an array in the Java app, the two item sticked together so I couldn't handle them as items of an array. 我曾尝试发送数组的第一项和第二项,但是当我尝试将它们添加到Java应用程序中的数组时,这两项粘在一起,因此我无法将它们作为数组项来处理。 I'm new in socket programming, so please help me how can I receive and handle the sent array in the Java app and maybe how to send them in the C# app correctly. 我是套接字编程的新手,所以请帮助我如何在Java应用程序中接收和处理发送的数组,以及如何在C#应用程序中正确发送它们。 Thank you for your help! 谢谢您的帮助!

Regrads, Stanley. 斯坦利,改制。

EDIT: 编辑:

The connecting parts are OK so I just post the sending parts. 连接部分还可以,所以我只发布发送部分。 Probably not too professional because I was just trying: 可能不是太专业,因为我只是在尝试:

Server: 服务器:

String[] texts = new String[2];
texts[0] = "hello";
texts[1] = "world";

for (int i = 0; i < texts.Length; i++)
{
            buffer = Encoding.UTF8.GetBytes(texts[i].ToCharArray(), 0, texts[i].Length);
            nwStream.Write(buffer, 0, texts[i].Length);
}

Client: (and this is where I'm not too confident) 客户:(这是我不太自信的地方)

ArrayList<String> texts = new ArrayList<String>();
BufferedReader in;

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
texts.add(0, in.readLine());

Maybe it's beacuse of the readLine but I'm not sure. 也许是由于readLine引起的,但我不确定。

Really this is more about how you serialize and deserialize than it is about sockets itself. 确实,这与套接字本身有关,更多的是有关序列化和反序列化的方式。 The hard part, connecting to a socket and sending/receiving data is taken care of. 硬部分(连接到套接字并发送/接收数据)已得到处理。

You have to decide on the format of your data. 您必须决定数据的格式。 That isn't done for you. 那不是为你做的。 In your case, you can use a simple line terminator like '\\n' to separate the lines of data for you. 在您的情况下,您可以使用简单的行终止符(例如'\\n'为您分隔数据行。 On the C# side, your formatting code would look like this: 在C#端,格式代码如下所示:

// assumption: socket is your C# socket
using(NetworkStream str = new NetworkStream(socket))
using(StreamWriter writer = new StreamWriter(str))
{
    foreach (string line in arrayOfStrings)
    {
        // This automatically appends a new line character to the end
        // of the line
        writer.WriteLine(line);
    }
}

On the Java side you would use a similar construct. 在Java方面,您将使用类似的构造。 In Java, the equivalent to a StreamReader would be a BufferedReader . 在Java中,相当于StreamReaderBufferedReader

// socket is your Java socket object
InputStreamReader charReader = new InputStreamReader(socket.getInputStream());
BufferedReader lineReader = new BufferedReader(charReader);

String line;
ArrayList<String> lines = new ArrayList<String>();

// readLine() reads to the first new line character or end of file
// and returns the string up to that point

while ((line = lineReader.readLine()) != null)
{
    lines.add(line);
}

// Converting to an array of strings is simple Java from here:
String[] arrayOfLines = lines.ToArray(new String[lines.size()]);

Of course things can get slightly more complicated if you want to use JSON, or some other means of sending formatted data. 当然,如果要使用JSON或其他发送格式化数据的方法,事情可能会变得稍微复杂一些。 Thankfully both Java and C# have reliable a reliably serializer/deserializer library for those standard formats. 幸运的是,对于这些标准格式,Java和C#都具有可靠的可靠序列化器/反序列化器库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM