Having trouble listening and writing to a socket

I might as well just post my classes. What I really need is a comprehensive guide to reading and writing to a socket. The sockets aren't being printed, and I know it's because they hang on the "readline()" statements in the netcheck functions. Why do they hang?

===================================================================

package TerminalChat;

import java.io.*;

import java.net.*;

public class QOTDServer {

private ServerSocket serversock;

private Socket spawnsock;

public QOTDServer()

{

// Establishes a new server socket on port 5005

try

{

serversock = new ServerSocket(5005);

}

catch (IOException e01)

{

System.out.println("Error: " + e01);

System.out.println("server socket establishment stage error");

}

// Untill the program is closed it will always wait untill the "ServerSocket" is accepted.

// Once accepted, it will create a "Socket".

while(true)

{

try

{

System.out.println("Before connect");

spawnsock = serversock.accept();

System.out.println("After connect");

new Thread(new ConnectionSpawn(spawnsock)).start();

}

catch(IOException e02)

{

System.out.println("Error: " + e02);

System.out.println("socket accept stage error");

}

}

}

public static void main(String[] args)

{

// TODO Auto-generated method stub

new QOTDServer();

}

}

===================================================================

package TerminalChat;

import java.io.*;

import java.net.*;

public class ConnectionSpawn implements Runnable {

Socket connSpawnSocket;

BufferedWriter buffwrite;

BufferedReader buffread;

public ConnectionSpawn(Socket sock)

{

connSpawnSocket = sock;

}

public void run()

{

try

{

buffread = new BufferedReader(new InputStreamReader(connSpawnSocket.getInputStream()));

buffwrite = new BufferedWriter(new OutputStreamWriter(connSpawnSocket.getOutputStream()));

}

catch(IOException e01)

{

System.out.println("Error: " + e01);

System.out.println("bufferedread/bufferedwrite reference to member stage error");

}

this.broadcast("hello Client");

System.out.println("Testclient: " + netcheck());

}

public String netcheck()

{

String msg = "No messages";

try

{

msg = buffread.readLine();

System.out.println("post readline check");

}

catch(IOException e02)

{

System.out.println("Error: " + e02);

System.out.println("netcheck stage error");

}

return msg;

}

public void broadcast(String message)

{

try

{

buffwrite.write(message);

}

catch(IOException e03)

{

System.out.println("Error: " + e03);

System.out.println("ConnectionSpawn write error stage error will this make it to the terminal?");

}

}

}

===================================================================

package TerminalChat;

import java.net.*;

import java.io.*;

public class Client {

Socket clientSocket;

BufferedWriter buffwrite;

BufferedReader buffread;

public Client()

{

try

{

clientSocket = new Socket("127.0.0.1", 5005);

}

catch(Exception e01){}

try

{

buffread = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

buffwrite = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

}

catch (Exception e02)

{

System.out.println("Error: " + e02);

System.out.println("bufferedread/bufferedwrite reference to member stage error");

}

this.broadcast("Hello Server");

System.out.println("Testclient: " + netcheck());

}

public String netcheck()

{

String msg = "No messages";

try

{

msg = buffread.readLine();

System.out.println("post readline check");

}

catch(IOException e02)

{

System.out.println("Error: " + e02);

System.out.println("netcheck stage error");

}

return msg;

}

public void broadcast(String message)

{

try

{

buffwrite.write(message);

}

catch(IOException e03)

{

System.out.println("Error: " + e03);

System.out.println("broadcast check error");

}

}

public static void main(String[] args)

{

// TODO Auto-generated method stub

new Client();

}

}

[4722 byte] By [zensunnia] at [2007-11-26 17:43:08]
# 1
You're reading lines and you're writing strings.You're not writing lines, which have a \n terminator, so the readLine() calls never return.
ejpa at 2007-7-9 0:11:19 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks.... but now I think I'm going about this the wrong way. I just changed"this.broadcast("Hello Server");" to "this.broadcast("Hello Server \n");"I don't think java recognizes the "\n" statements. How do I indicate the line break?
zensunnia at 2007-7-9 0:11:19 > top of Java-index,Java Essentials,New To Java...
# 3
That should work, or you could use BufferedWriter.newLine(), or PrintWriter.println(), or PrintStream.println().However you need to flush() the BufferedWriter after the writes.
ejpa at 2007-7-9 0:11:19 > top of Java-index,Java Essentials,New To Java...
# 4
Oh yea, I just forgot to flush :)Everything's fine now - works great. BTW, do you guys reccomend keeping a socket, or just creating a new one every time you want to send information?
zensunnia at 2007-7-9 0:11:19 > top of Java-index,Java Essentials,New To Java...
# 5

> Oh yea, I just forgot to flush :)

>

> Everything's fine now - works great. BTW, do you guys

> reccomend keeping a socket, or just creating a new

> one every time you want to send information?

Now why would you want to create a new Socket everytime.From the server once a Sockets bounds to the port you stick by it receive the desired clients.From the client side once a socket establishes a connection with the other host you can keep that socket alive till you carry out the task that you want to and then once you are through close the connection.

qUesT_foR_knOwLeDgea at 2007-7-9 0:11:19 > top of Java-index,Java Essentials,New To Java...
# 6

> That should work, or you could use

> BufferedWriter.newLine(), or PrintWriter.println(),

> or PrintStream.println().

>

> However you need to flush() the BufferedWriter after

> the writes.

You can use a PrintWriter which has been set to flush the contents rather than you manually flush it.

PrintWriter out=new PrintWriter(OutputStream os,true);

qUesT_foR_knOwLeDgea at 2007-7-9 0:11:19 > top of Java-index,Java Essentials,New To Java...
# 7
As long as you don't mind all exceptions being swallowed, or as long as you remember to check it for exceptions yourself with checkError() and you don't mind not knowing what the exception actually was if there was one ... Not a great way to write to a network IMO
ejpa at 2007-7-9 0:11:19 > top of Java-index,Java Essentials,New To Java...