C
C#2y ago
Fabio

✅ Send Integer over TCP from Java to C#

Im trying to send an integer value from a java program to my c# program, for some reason the received byte[] is always empty?
12 Replies
Fabio
FabioOP2y ago
I have also tried to do it with a binary reader like this but the integer is still 0
jcotton42
jcotton422y ago
how are outputStream in the Java and client in the C# created?
Fabio
FabioOP2y ago
Fabio
FabioOP2y ago
jcotton42
jcotton422y ago
I'm not familiar with Java sockets Do they require you to explicitly connect? (Sorry for the delay, was eating lunch)
Fabio
FabioOP2y ago
as far as i know creating the socket in java is the connect itself
jcotton42
jcotton422y ago
oh it does (ew) so hm that looks like it should work... so, TcpListener/TcpClient are kinda notoriously janky, does it work if you just use sockets directly?
var ep = new IPEndPoint(IPAddress.Any, 5020);
var listen = new Socket(SocketType.Stream, ProtocolType.Tcp);
listen.Bind(ep);
listen.Listen();
Socket client = await listen.AcceptAsync();
using var stream = new NetworkStream(client, ownsSocket: true);
var ep = new IPEndPoint(IPAddress.Any, 5020);
var listen = new Socket(SocketType.Stream, ProtocolType.Tcp);
listen.Bind(ep);
listen.Listen();
Socket client = await listen.AcceptAsync();
using var stream = new NetworkStream(client, ownsSocket: true);
Fabio
FabioOP2y ago
i could try that but i dont think the socket / stream creation itself is the problem, i have already transferred other data with them but when using the DataOutputStream the writeInt method doesnt seem to do anything
jcotton42
jcotton422y ago
hm so on the java side
Fabio
FabioOP2y ago
im not sure if the problem is on the java side thats the thing xd maybe it is being sent correctly but im just receiving wrong
jcotton42
jcotton422y ago
hm are you sure the C# side is listening before the Java side tries to connect?
Fabio
FabioOP2y ago
yes i have debugged it several times, i have also tried sending the int as bytes but the value i get after converting the received bytes it into an integer is way off from what it should be
Want results from more Discord servers?
Add your server