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();
}
}

