简体   繁体   中英

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. 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. 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. 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.

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. On the C# side, your formatting code would look like this:

// 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. In Java, the equivalent to a StreamReader would be a BufferedReader .

// 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. Thankfully both Java and C# have reliable a reliably serializer/deserializer library for those standard formats.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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