Simple TCP utility failing right out the gate
I'm trying to set up TCP communications between two machines, but for now (in the initial/testing phase) am using localhost.
Can anybody see why I'm getting these msgs in the Console (all I'm doing is running the app, not even pressing the button:
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.jcp.tds.RTTCPSortSimMain.setUpNetworking(RTTCP SortSimMain.java:104)
at com.jcp.tds.RTTCPSortSimMain.go(RTTCPSortSimMain.j ava:91)
at com.jcp.tds.RTTCPSortSimMain.main(RTTCPSortSimMain .java:34)
Exception in thread "Thread-2" java.lang.NullPointerException
at com.jcp.tds.RTTCPSortSimMain$IncomingReader.run(RT TCPSortSimMain.java:138)
at java.lang.Thread.run(Unknown Source)
with the following code (which is basically ripped straight from "Head First Java"):
Code Java:
public class RTTCPSortSimMain {
...
BufferedReader reader;
PrintWriter writer;
Socket sock;
public static void main(String[] args) {
new RTTCPSortSimMain().go();
}
public void go() {
... (GUI code)
JButton sendButton = new JButton("Send and Receive");
sendButton.addActionListener(new SendButtonListener());
tfHost = new JTextField(16);
tfHost.setText("127.0.0.1");
tfPort = new JTextField(4);
tfPort.setText("4211");
... (more GUI code)
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
... (more GUI code)
}
private void setUpNetworking() {
try {
// for testing (localhost)
sock = new Socket("127.0.0.1", 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("networking established");
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
public class SendButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev) {
try {
writer.println(outgoing.getText());
writer.flush();
}
catch (Exception ex) {
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}
}
class IncomingReader implements Runnable {
@Override
public void run() {
String message;
try {
while ((message = reader.readLine()) != null) {
System.out.println("client read " + message);
incoming.append(message + "\n");
}
} catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
?
Re: Simple TCP utility failing right out the gate
This thread has been cross posted here:
http://www.java-forums.org/networking/46210-simple-tcp-utility-failing-right-out-gate.html#post220720
Although cross posting is allowed,
for everyone's benefit, please read:
Java Programming Forums Cross Posting Rules
The Problems With Cross Posting
When posting on > 1 forum, please provide links to the other forums.
Re: Simple TCP utility failing right out the gate
Was this solved on the other forum?