I'm trying to get 2 computers in a Local network (home network) to communicate via Sockets in order for one to request an object and the other send the object to the requesting process. I had a bit of a hard time finding how to do this with objects instead of just strings, but I thought I found a solution with
However, I am receiving the following error message when I start the listener:
I've scoured google and the only answers I've seen so far are ones that involve the port being in use already.
The problem is that I've looked at TCPView and netstat and both are reporting that the port number I am using is NOT in use. The port is FREE. I've also made sure that the Windows Firewall is allowing Java through and it is. What could be causing this? Please see my listener and client code below:
Listener
Client Code:
Additionally, this isn't a network issue as I am able to have the 2 computers communicate across the network when passing Strings (without using ServerSocketChannel).
Why is the bind failing? Is there a better way to do this without ServerSocketChannel and binding? Is there a way to pass objects through sockets like strings?
ServerSocketChannel.
However, I am receiving the following error message when I start the listener:
Quote
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source)
at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source)
at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source)
at clock.ClockListener.run(ClockListener.java:35)
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source)
at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source)
at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source)
at clock.ClockListener.run(ClockListener.java:35)
I've scoured google and the only answers I've seen so far are ones that involve the port being in use already.
The problem is that I've looked at TCPView and netstat and both are reporting that the port number I am using is NOT in use. The port is FREE. I've also made sure that the Windows Firewall is allowing Java through and it is. What could be causing this? Please see my listener and client code below:
Listener
import java.awt.Toolkit;
import java.io.*;
import java.net.*;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Calendar;
public class ClockListener extends Thread{
private ServerSocket welcomeSocket;
private ServerSocketChannel ssChannel;
private BufferedReader readerIn;
private ObjectOutputStream outStream;
private AllEmployees employeeList;
private String inputString;
private static final int portNumber = 9595;
public ClockListener(AllEmployees eL){
try {
welcomeSocket = new ServerSocket(portNumber);
welcomeSocket.setReuseAddress(true);
} catch (IOException e) {
e.printStackTrace();
}
employeeList = eL;
}
@Override
public void run() {
try{
ssChannel = ServerSocketChannel.open();
ssChannel.configureBlocking(true);
ssChannel.socket().bind(new InetSocketAddress(portNumber));
}catch(IOException e){
e.printStackTrace();
}
while(true){
try {
Socket serverSocket;
SocketChannel sChannel = ssChannel.accept();
ObjectOutputStream oos = new ObjectOutputStream(sChannel.socket().getOutputStream());
oos.writeObject(employeeList);
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Client Code:
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.channels.SocketChannel;
public class ClockConnection extends Thread{
private AllEmployees employeeList;
private Socket clientSocket;
private InetSocketAddress iNetAddress;
private static final int portNumber = 9595;
public void run(){
try {
iNetAddress = new InetSocketAddress("192.168.1.195", portNumber);
clientSocket = new Socket("192.168.1.195", portNumber);
DataOutputStream sendToServer = new DataOutputStream(clientSocket.getOutputStream());
String request = "GetEmployees";
sendToServer.writeBytes(request);
SocketChannel sChannel = SocketChannel.open();
sChannel.configureBlocking(true);
if (sChannel.connect(iNetAddress)){
ObjectInputStream inStream = new ObjectInputStream(sChannel.socket().getInputStream());
try {
employeeList = (AllEmployees)inStream.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public AllEmployees getEmployeeList(){
return employeeList;
}
}
Additionally, this isn't a network issue as I am able to have the 2 computers communicate across the network when passing Strings (without using ServerSocketChannel).
Why is the bind failing? Is there a better way to do this without ServerSocketChannel and binding? Is there a way to pass objects through sockets like strings?