Allan Bruce
2006-01-12 23:13:01 UTC
I am starting Java network programming for the first time although I have
experience in Java itself and socket programming in C++. I have an IM
server already running which I use to communicate with C++ clients and that
all runs fine. Now I want to develop a Java client but am having problems.
When I connect the socket then try to get the input stream my program just
hangs. The code is below, if anyone can tell me why this might happen, I
would appreciate it.
Another point is that, I want to send actual bytes on the line and receive
actual bytes, what type of input/output stream should I be using?
Thanks
Allan
private boolean connectToAServer()
{
System.out.println("Attempting to connect to a server...");
try
{
mSocket = new Socket(Globals.gPrefs.mainServer(),
Globals.gPrefs.mainServerPort());
}
catch (Exception e)
{
System.err.println("Could not connect to main server: " + e);
}
if (mSocket == null)
{
try
{
mSocket = new Socket(Globals.gPrefs.backupServer(),
Globals.gPrefs.backupServerPort());
}
catch (Exception e)
{
System.err.println("Could not connect to backup server: " +
e);
}
}
if (mSocket == null)
return ERROR;
System.out.println("Connection established on " +
mSocket.getInetAddress());
try
{
mOutput = new ObjectOutputStream( mSocket.getOutputStream());
mOutput.flush();
mInput = new ObjectInputStream(mSocket.getInputStream()); //
hangs here
}
catch (Exception e)
{
closeSocket();
return ERROR;
}
return OK;
}
experience in Java itself and socket programming in C++. I have an IM
server already running which I use to communicate with C++ clients and that
all runs fine. Now I want to develop a Java client but am having problems.
When I connect the socket then try to get the input stream my program just
hangs. The code is below, if anyone can tell me why this might happen, I
would appreciate it.
Another point is that, I want to send actual bytes on the line and receive
actual bytes, what type of input/output stream should I be using?
Thanks
Allan
private boolean connectToAServer()
{
System.out.println("Attempting to connect to a server...");
try
{
mSocket = new Socket(Globals.gPrefs.mainServer(),
Globals.gPrefs.mainServerPort());
}
catch (Exception e)
{
System.err.println("Could not connect to main server: " + e);
}
if (mSocket == null)
{
try
{
mSocket = new Socket(Globals.gPrefs.backupServer(),
Globals.gPrefs.backupServerPort());
}
catch (Exception e)
{
System.err.println("Could not connect to backup server: " +
e);
}
}
if (mSocket == null)
return ERROR;
System.out.println("Connection established on " +
mSocket.getInetAddress());
try
{
mOutput = new ObjectOutputStream( mSocket.getOutputStream());
mOutput.flush();
mInput = new ObjectInputStream(mSocket.getInputStream()); //
hangs here
}
catch (Exception e)
{
closeSocket();
return ERROR;
}
return OK;
}