Click here to Skip to main content
15,908,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have C# server implemented with sockets in this way:
I have a listener:
TcpListener listener = new TcpListener(IPAddress.Any, 33333);

I created a new client every time a client connects
TcpClient client = listener.AcceptTcpClient();

And a BinaryReader and BinaryWriter:
NetworkStream stream = client.GetStream();
w = new BinaryWriter(stream);
r = new BinaryReader(stream);

If I want to write or read something to connected client i am using:
w.Write(MESSAGE);//for writing
msg=r.ReadString();//for reading something that client sends

The problem:
How can I create a java program to comunicate with this server, I only managed to connect to the server(create the socket) but i didn't find anything on google about how to send and receive messages, I tried different ways but it didn't write or read anything. Any suggestions? or a very simple java program would be just enough. Thanks

Sas Gabriel
Updated 14-Dec-11 21:55pm
Nagy Vilmos 15-Dec-11 3:55am    
Added some formatting

Further to Richard's excellent answer, look here[^] for some easy examples of writing a Java TCP/IP client.
Share this answer
Richard MacCutchan 15-Dec-11 4:10am    
Very useful links, filed for future reference.
Use Google to find source code for sockets in Java. Once you have created the socket the language has nothing to do with it. The only issue is that the data you transfer between client and server must be in a form that both ends agree and understand. Simple ASCII bytes is probably the best form, with maybe a header byte giving the length of the message.
Share this answer
Nagy Vilmos 15-Dec-11 3:57am    
Excellent answer Senor Ricardo!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900