• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Client C server  RSS feed

 
Dave Roberts
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to write a client that will send a hex value to a server and the server will spit back two chars, but I'm not having success
Anyone see where I'm going wrong?
import java.io.*;
import java.net.*;
public class EchoBW
{
public static void main(String[] args) throws IOException
{
final int loginPort = 4038;
Socket nexusSocket = null;
try
{
InetAddress nexus = InetAddress.getByName("login.bitwisechat.com");
nexusSocket = new Socket(nexus, loginPort);
BufferedReader br = new BufferedReader(new InputStreamReader(nexusSocket.getInputStream()));
PrintWriter printWriter = new PrintWriter(nexusSocket.getOutputStream(), true);
printWriter.write(0x612);
System.out.println("0x612 written");
String line;
line = br.readLine();
while(!line.equals(null))
{
System.out.println(line);
}
System.out.println("Thread after while loop");
nexusSocket.close();
}
catch (UnknownHostException e)
{
System.err.println("Host not known. Check your connection.");
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: nexus");
System.exit(1);
}
}
}
 
Donal Lynch
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some things to try:
* Confirm that the encoding performed by the PrintWriter produces the expected sequence of bytes: something along the lines of:

* Confirm that the bytes are going across the network and not getting buffered or something. The most controllable way of doing this is to write a tiny server that sits on a socket and prints out every byte it receives.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't flushing the PrintWriter before reading from the BufferedReader. Even though you passed autoflush=true to the PrintWriter constructor, that does you no good since you aren't using println() to do your writing.
Also, check to make sure you and the server agree about byte ordering.
[ October 04, 2002: Message edited by: Ron Newman ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!