1

I have 02 applications, one is written in java and the another one is written in C#. I need to exchange data (Strings) between them thus I use a socket connection.


Currently the C# application acts as server and java application acts as client. The C# application will continuously listen on port "7777" and the Java application will make a socket to "localhost:7777" as start up and use that socket to send data to C# server. I don't close the connection between them since I need to exchange data frequently.


Everything is great until my C# server application sometimes needs to send some Strings to the Java client application. I think about using another port to make my java application becomes a server and the C# application will also become a client. Yet I don't feel that it is a good solution.


P/S: Yes, socket is Bi-direction, in my current application I can send string data from my C# application back to Java application, but only when the Java application first sent a string to the C# application and now it is waiting for the respond data from the C# application.


Thus I want to ask if there is another better solution ? Is it possible if we only use 1 port in total ? Thank you very much.

6
  • 1
    as soon as a connection is establised, the servicing socket and the active connect socket on each end are equal and bidirectional. Which means client and server is not really relevant anymore. It is not clear why you would need another socket just for sending strings. Or why you want to reverse the server becoming client and vice versa. Commented Mar 25, 2015 at 20:04
  • 1
    I was thinking of mentioning this in comments to your previous question. Sockets go both ways. You don't need two of them. Commented Mar 25, 2015 at 20:06
  • sorry guys, I made a mistake, what i meant is sometimes, the C# server application needs to send some data to the Java client application, but this is not possible since my Java application can't listening to that port anymore since it is already occupied by the C# application. Commented Mar 25, 2015 at 20:17
  • sockets are bidirectional so now I know for sure you don't need an extra socket and because your comment comes after already mentioning this fact shows you probably don't understand what we mean. The sockets can be used for both receiving and sending using the same socket. Commented Mar 25, 2015 at 20:41
  • 1
    You need to get your concepts clear here, because it sounds like you're a bit confused. Think about a web request. The browser connects to the server and sends a request to the server. The server replies. All on the same single socket connection. No new socket is opened up. No new port is required. Once connected, the client can send data to the server and the server can send data back to the client. Sockets allow both reading from and writing to the same socket instance. Once the socket is connected, both the client and the server can both read and write. Commented Mar 25, 2015 at 20:44

1 Answer 1

2

Your socket works both ways (bi-directional), so you won't need to create another. You can just get the output stream and input stream of the socket, on both sides of the connection, and use those for sending/receiving.

To send a string, the easiest way would probably be to use a DataOutputStream as you can write UTF-8 strings with a simple function.

Java side:

String blah = "hey";
DataOutputStream dataOs = new DataOutputStream(socket.getOutputStream());
dataOs.writeUTF(blah);

The C# side is slightly trickier as you need to account for the fact that the first two bytes sent from the java function writeUTF() will actually be the length (in bytes) of the string that follows. You can grab those 2 bytes first and then throw them into an int, shifting the bits as you go along. Then you can use that int as the size of the buffer when you request the string from the socket. Should look something like this.

C# side:

int length = 0;
byte[] sizeBuffer = new byte[2];
socket.Receive(sizeBuffer);
for (int i = 0; i < sizeBuffer.Length; i += 1)
{
    length = length << 8;
    length += sizeBuffer[i];
}

byte[] stringBuffer = new byte[length];
socket.Receive(stringBuffer);

string myString = Encoding.UTF8.GetString(stringBuffer, 0, stringBuffer.Length);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, in my current situation, I can handle this since my C# application is a server and my java application is a client, what i need is sending data from my c# application back to the java application.
Once the connection establishes, the notion of client and server is pretty much gone. You then just have two endpoints that each can send/receive. If you want to send data to the Java app from the C# app, just grab the output stream on your C# socket and use that. Then grab the input stream on the Java side and read from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.