user366312
Gold Member
- 88
- 3
- TL;DR Summary
- I have a C# server. I need to connect a Java client to it, and make them interact.
The following is a client-side C# code:
[CODE lang="csharp" title="C# client" highlight="10,11"]string Host = "localhost";
int Port = 2000;
TcpClient Tcp = new TcpClient(Host, Port);
NetworkStream stream = Tcp.GetStream();
reader = new BinaryReader(stream);
writer = new BinaryWriter(stream);
writer.Write("Hello");
string str = reader.ReadString();[/CODE]
I have written the following Java code:
[CODE lang="java" title="Java Client" highlight="10,11"]InetAddress ip = InetAddress.getByName("localhost");
int PORT_NO = 2000;
Socket socket = new Socket(ip, PORT_NO);
// obtaining input and out streams
DataInputStream reader = new DataInputStream(socket.getInputStream());
DataOutputStream writer = new DataOutputStream(socket.getOutputStream());
writer.writeChars("Hello");
String str = reader.readUTF();[/CODE]
But, my java code isn't working.
C#-Server and C#-clients are running okay. The C#-server seems to be not receiving the string sent by Java client.
How can I do what I need? What would be the Java equivalent of this code?
[CODE lang="csharp" title="C# client" highlight="10,11"]string Host = "localhost";
int Port = 2000;
TcpClient Tcp = new TcpClient(Host, Port);
NetworkStream stream = Tcp.GetStream();
reader = new BinaryReader(stream);
writer = new BinaryWriter(stream);
writer.Write("Hello");
string str = reader.ReadString();[/CODE]
I have written the following Java code:
[CODE lang="java" title="Java Client" highlight="10,11"]InetAddress ip = InetAddress.getByName("localhost");
int PORT_NO = 2000;
Socket socket = new Socket(ip, PORT_NO);
// obtaining input and out streams
DataInputStream reader = new DataInputStream(socket.getInputStream());
DataOutputStream writer = new DataOutputStream(socket.getOutputStream());
writer.writeChars("Hello");
String str = reader.readUTF();[/CODE]
But, my java code isn't working.
C#-Server and C#-clients are running okay. The C#-server seems to be not receiving the string sent by Java client.
How can I do what I need? What would be the Java equivalent of this code?